除非关闭限制,否则无法从 Excel 复制并粘贴到受表单编辑限制的 Word 文档
Can not copy from Excel and paste to a Form Edit restricted Word document unless restriction is turned off
我正在将 Excel 单元格内容复制到表单编辑受限 (2010) Word 文档的书签中,但它只会在保护关闭时粘贴。
我目前必须在错误之后再次打开保护的代码。正确的代码是什么?
有没有办法在不关闭保护的情况下进行复制粘贴?
第二个问题是当文本被粘贴到书签时字体是红色的(如果在文档上手动输入它是黑色的)。 Word 默认设置为黑色(我重置了默认值以备不时之需)。在新文档中键入是黑色的,但是,当 Word 打开时字体图标显示为红色,即使检查默认它仍然显示为黑色。我可以在 VBA 中定义字体颜色以覆盖此问题直到解决,或者您可以建议一种修复 Word 默认值的方法吗?
Sub Arzbericht_Brandstetter()
' x - Defined Cell Names - ARTBrandPATH , ARTBrandDOC
' Excel Word Bookmark
' x - Defined Cell Names - ARZKrankenhaus Text65
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = ActiveSheet
Dim Wd As Object
Dim wdDoc As Object
Dim BrandstetterDoc As Object
Dim BrandstetterPath As String
Dim f As Boolean
BrandstetterPath = ActiveSheet.Range("ARTBrandPATH").Value & ActiveSheet.Range("ARTBrandDOC").Value & ".doc" ' x
' On Error Resume Next
Set BrandstetterDoc = GetObject(BrandstetterPath)
If BrandstetterDoc Is Nothing Then
Set Wd = GetObject(, "Word.Application")
If Wd Is Nothing Then
Set Wd = CreateObject("Word.Application")
If Wd Is Nothing Then
MsgBox "Failed to start Word!", vbCritical
Exit Sub
End If
f = True
End If
Set BrandstetterDoc = Wd.Documents.Open(BrandstetterPath)
If BrandstetterDoc Is Nothing Then
MsgBox "Failed to open Brandstetter Document!" & vbNewLine & _
" Check File Directory is correct", vbCritical
If f Then
Wd.Quit
End If
Exit Sub
End If
Wd.Visible = True
Else
With BrandstetterDoc.Parent
.Visible = True
.Activate
' Turn Protection OFF
With ActiveDocument
.Unprotect "xxxxx"
.Protect wdAllowOnlyRevisions, , Password:="xxxxx"
End With
BrandstetterDoc.Bookmarks("Text65").Range.Text = ws.Range("ARZKrankenhaus").Value
' Turn Protection ON (Restricted Editing)
' ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End With
End If
End Sub
因为它是一个表单域而不仅仅是一个书签,所以我更改了如下代码,解决了奇怪的红色字体问题。我现在可以复制到受保护的文档了。
From
BrandstetterDoc.Bookmarks("Text65").Range.Text = ws.Range("ARZKrankenhaus").Value
To
ActiveDocument.FormFields("Text65").Result = ws.Range("ARZKrankenhaus").Value
我正在将 Excel 单元格内容复制到表单编辑受限 (2010) Word 文档的书签中,但它只会在保护关闭时粘贴。
我目前必须在错误之后再次打开保护的代码。正确的代码是什么?
有没有办法在不关闭保护的情况下进行复制粘贴?
第二个问题是当文本被粘贴到书签时字体是红色的(如果在文档上手动输入它是黑色的)。 Word 默认设置为黑色(我重置了默认值以备不时之需)。在新文档中键入是黑色的,但是,当 Word 打开时字体图标显示为红色,即使检查默认它仍然显示为黑色。我可以在 VBA 中定义字体颜色以覆盖此问题直到解决,或者您可以建议一种修复 Word 默认值的方法吗?
Sub Arzbericht_Brandstetter()
' x - Defined Cell Names - ARTBrandPATH , ARTBrandDOC
' Excel Word Bookmark
' x - Defined Cell Names - ARZKrankenhaus Text65
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = ActiveSheet
Dim Wd As Object
Dim wdDoc As Object
Dim BrandstetterDoc As Object
Dim BrandstetterPath As String
Dim f As Boolean
BrandstetterPath = ActiveSheet.Range("ARTBrandPATH").Value & ActiveSheet.Range("ARTBrandDOC").Value & ".doc" ' x
' On Error Resume Next
Set BrandstetterDoc = GetObject(BrandstetterPath)
If BrandstetterDoc Is Nothing Then
Set Wd = GetObject(, "Word.Application")
If Wd Is Nothing Then
Set Wd = CreateObject("Word.Application")
If Wd Is Nothing Then
MsgBox "Failed to start Word!", vbCritical
Exit Sub
End If
f = True
End If
Set BrandstetterDoc = Wd.Documents.Open(BrandstetterPath)
If BrandstetterDoc Is Nothing Then
MsgBox "Failed to open Brandstetter Document!" & vbNewLine & _
" Check File Directory is correct", vbCritical
If f Then
Wd.Quit
End If
Exit Sub
End If
Wd.Visible = True
Else
With BrandstetterDoc.Parent
.Visible = True
.Activate
' Turn Protection OFF
With ActiveDocument
.Unprotect "xxxxx"
.Protect wdAllowOnlyRevisions, , Password:="xxxxx"
End With
BrandstetterDoc.Bookmarks("Text65").Range.Text = ws.Range("ARZKrankenhaus").Value
' Turn Protection ON (Restricted Editing)
' ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End With
End If
End Sub
因为它是一个表单域而不仅仅是一个书签,所以我更改了如下代码,解决了奇怪的红色字体问题。我现在可以复制到受保护的文档了。
From
BrandstetterDoc.Bookmarks("Text65").Range.Text = ws.Range("ARZKrankenhaus").Value
To
ActiveDocument.FormFields("Text65").Result = ws.Range("ARZKrankenhaus").Value