2

我有一个宏可以在给定文件夹中的所有文件中运行。我的Normal.dotm全局模板具有宏使用的 3 种字体样式,但是宏停止,因为它反复找不到宏调用的样式。最简单的方法是创建一个宏(或添加到我的当前),它会自动将这 3 种样式从全局模板导入到活动文档中。

这是我到目前为止所拥有的:

Application.OrganizerCopy Source:= _
    "C:\Users\Inu\AppData\Roaming\Microsoft\Templates\Normal.dotm", _
    Destination:= _
    ActiveDocument _
    , Name:="DO_NOT_TRANSLATE", Object:=wdOrganizerObjectStyles
Application.OrganizerCopy Source:= _
    "C:\Users\Inu\AppData\Roaming\Microsoft\Templates\Normal.dotm", _
    Destination:= _
    ActiveDocument _
    , Name:="tw4winExternal", Object:=wdOrganizerObjectStyles
Application.OrganizerCopy Source:= _
    "C:\Users\Inu\AppData\Roaming\Microsoft\Templates\Normal.dotm", _
    Destination:= _
    ActiveDocument _
    , Name:="tw4winInternal", Object:=wdOrganizerObjectStyles

有什么帮助吗?我有大约一百个需要格式化的文件,所以单独导入是不可能的。

4

4 回答 4

1

如果您准备了具有所需样式的模板,则可以使用以下命令将它们全部复制:

ActiveDocument.CopyStylesFromTemplate("C:\Temp\FullPathToTemplate.dotx")

还是您已经以其他方式解决了它?

于 2012-08-16T09:23:22.560 回答
1

原始代码的问题在于 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
于 2014-12-04T20:10:36.747 回答
1

为什么不将样式从 复制Normal.dotm到您的文件中。

流程如下:

  1. 打开样式窗口 9Alt + Ctrl + Shift + S)。

  2. 选择“管理样式”(在窗口底部,左起第三个图标)。

  3. 选择导入/导出。

  4. 将您Normal.dotm的加载到窗口的左侧。

  5. 将您的加载SomeDoc.dotm到窗口的右侧。

  6. 从左侧窗口(在)中选择Styles要复制的内容。Normal.dotm

  7. 选择Copy

这些Styles现在已被导入到您的文档中。确保将文档保存为启用宏的模板.dotm

在此之后应该不是问题。

于 2015-11-09T07:15:12.937 回答
0

我不知道您是否对上述解决方案感到满意,但由于该问题仍被列为“未回答”,因此我在此处使用了一些简化的代码来解决此问题:

Sub Test()
    Const C_St1 = "style-1"
    Const C_St2 = "style-2"
    Const C_St3 = "style-3"

    A_Styles = Array(C_St1, C_St2, C_St3)
    Call VerifyExistenceOfStyles(A_Styles, V_Errors)
    Debug.Print V_Errors
End Sub

Sub VerifyExistenceOfStyles(A_Styles, Optional V_Errors)
    On Error Resume Next
    For Each V_Style In A_Styles
        If V_Style = "" Then
            'do nothing
        Else
            Err.Clear
            tmp = ActiveDocument.Styles(V_Style).Font.Size 'checking whether style exists
            V_ErrNumber = Err.Number
            If V_ErrNumber = 5941 Then Call AddMissingStyleFromTemplate(V_Style, V_Error) Else V_Error = ""
        End If
        V_Errors = V_Errors & IIf(V_Error = "", "", V_Error & vbCr)
    Next
    On Error GoTo 0
End Sub

Sub AddMissingStyleFromTemplate(V_Style, Optional V_Error)
    V_Template = ActiveDocument.AttachedTemplate.Path & Application.PathSeparator & ActiveDocument.AttachedTemplate.Name
    V_File = ActiveDocument.FullName

    On Error Resume Next
    Application.OrganizerCopy _
      Source:=V_Template, _
      Destination:=V_File, _
      Name:=V_Style, _
      Object:=wdOrganizerObjectStyles
    If Err.Number = 0 Then  'no error, style found in template
        V_Error = ""
    ElseIf Err.Number = 5608 Then 'is no style name
        V_Error = "|" & V_Style & "| is no valid style name, neither in the document nor in the template!"
    Else
        V_Error = "|" & V_Style & "| produces the unidentified error " & Err.Number
    End If
End Sub
于 2015-11-09T02:04:32.047 回答