原始代码的问题在于 Destination 参数Application.OrganizerCopy
需要是一个字符串——目标文档的完整路径。我已经测试了以下代码,发现它可以工作(Word 2013):
Sub test_style_copy()
Dim B_failed As Boolean
Call add_style_from_Normal(ActiveDocument, "Orcamento", B_failed)
End Sub
' -------------------------------------------------------------------------------------
Sub add_style_from_Normal(destination_document As Word.Document, _
style_name As String, B_fail As Boolean)
' Adds the style "style_name" from the Normal template to the styles available
' in the document <destination_document>
Dim B_Normal As Boolean
Dim copy_style As Variant
Dim normal_template As Word.Document
B_fail = False
' test if style "style_name" is already present in <destination_document>
If style_exists(destination_document, style_name) Then Exit Sub
' open the Normal template as a document, and test if style "style_name" is
' present in Normal template
Set normal_template = Application.NormalTemplate.OpenAsDocument
B_Normal = style_exists(normal_template, style_name)
normal_template.Close
Set normal_template = Nothing
' Style "style_name" not in Normal template, exit:
If Not B_Normal Then
MsgBox "Cannot copy style """ & style_name & """ from Normal.dotm to " & _
vbCr & destination_document.Name & " :" & vbCr & vbCr & _
"Style """ & style_name & """ does not exist in Normal.dotm", vbCritical
B_fail = True
Exit Sub
End If
' copy style "style_name" from Normal template to <destination_document>
With Application
.OrganizerCopy Source:=.NormalTemplate.FullName, _
Destination:=destination_document.FullName, _
Name:=style_name, Object:=wdOrganizerObjectStyles
End With
' check that style successfully copied:
B_fail = Not style_exists(destination_document, style_name)
If B_fail Then MsgBox "Copy of style " & style_name & " to " & _
destination_document.Name & " failed", vbCritical
End Sub
' -------------------------------------------------------------------------------------
Function style_exists(test_document As Word.Document, style_name As String) As Boolean
' style_exists = TRUE Style "style_name" exists in document <test_document>
' = FALSE absent
style_exists = False
On Error Resume Next
style_exists = test_document.Styles(style_name).NameLocal = style_name
End Function