使用Microsoft Word导入样式

时间:2012-04-27 19:22:17

标签: vba ms-word word-vba

我有一个宏,它运行在给定文件夹中的所有文件中。我的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 个答案:

答案 0 :(得分:1)

如果您准备一个包含所需样式的模板,可以使用以下命令将它们全部复制:

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

或者你已经用另一种方式解决了它?

答案 1 :(得分:1)

原始代码的问题是Application.OrganizerCopy中的Destination参数需要是一个字符串 - 目标文档的完整路径。我已经测试了以下代码,并发现它可以工作(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

答案 2 :(得分: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

答案 3 :(得分:0)

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

以下流程:

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

  2.   
  3. 选择管理样式(在窗口底部,它是左边的第三个图标)。

  4.   
  5. 选择导入/导出。

  6.   
  7. Normal.dotm加载到窗口的左侧。
  8.   
  9. SomeDoc.dotm加载到窗口右侧。
  10.   
  11. 从左侧窗口(Styles中)选择要复制的Normal.dotm
  12.   
  13. 选择Copy
  14.   

这些Styles现已导入您的文档。请务必将文档另存为启用宏的模板.dotm

此后不应该是一个问题。