vba - 从其他文档粘贴时,如何防止样式被引入目标文档?
问题描述
我已经为我的同事设置了一个 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
解决方案
我不会拦截标准粘贴命令。无法粘贴表格或字段等格式化内容。相反,请使用具有备用键组合(如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
推荐阅读
- flutter - 有什么方法可以添加 CircularProgressIndicator 以获取响应表单 dio 请求
- java - Swagger Schema - 模式不适用于字符串数组
- r - 基于 glmnet 包最能区分组的变量
- sql - 如何在 TypeORM 中按字符串长度排序?
- javascript - 未捕获的 ReferenceError:未定义 jQuery(Thymeleaf)
- sql - 连接具有不同数据类型的 2 个表
- php - 在不破坏 CakePHP 中的 buildRules() 表回调的情况下限制 beforeFind() 表回调的查询结果
- botframework - 在基于 ms 团队搜索的消息扩展中将“设置”更改为“注销”
- typescript - 缩小深度嵌套对象的类型
- api - 如何从场景大纲中获取一列的值作为空手道中的数组