首页 > 解决方案 > 从其他文档粘贴时,如何防止样式被引入目标文档?

问题描述

我已经为我的同事设置了一个 MS Word 文档 (.docx),以使用样板文本和整齐排序的样式,以便从样式库中进行选择。问题是,他们经常从其他文档中粘贴,从而将新样式引入模板。通过格式限制将它们限制为几种样式是没有选择的,因为这也会禁用字体格式。下面的代码(部分取自这个问题)有点像我想要的,但可能有更好的选择吗?

Sub EditPaste()
'PURPOSE: Prevent styles from other documents from being introduced into this document.
    
    Dim k As Long

    k = ActiveDocument.Styles.Count
    
    Selection.Range.Paste
    If k <> ActiveDocument.Styles.Count Then
        ActiveDocument.Undo
        On Error GoTo F
            Selection.PasteSpecial Link:=False, _
            DataType:=wdPasteText, Placement:=wdInLine, _
            DisplayAsIcon:=False
    Exit Sub

F:  MsgBox ("An error has occurred.")
    End If

End Sub

标签: vbams-word

解决方案


我不会拦截标准粘贴命令。无法粘贴表格或字段等格式化内容。相反,请使用具有备用键组合(如Alt + V)的宏来运行如下宏:

Sub PasteUnformattedText()
  On Error GoTo ClipboardNotText
    Selection.PasteSpecial DataType:=wdPasteText
    Exit Sub
ClipboardNotText:
  Err.Clear
  On Error GoTo ClipboardNotUnicodeText
  Selection.PasteSpecial DataType:=22 'Paste Unicode Text
  Exit Sub
ClipboardNotUnicodeText:
  Selection.Paste
  Err.Clear
End Sub

推荐阅读