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

これで何か助けはありますか?フォーマットが必要なファイルが約100あるので、個別にインポートするのは問題外です。

4

4 に答える 4

1

元のコードの問題は、宛先パラメーターが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

必要なスタイルを含むテンプレートを準備すると、次のコマンドでそれらをすべてコピーできます。

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

または、すでに別の方法で解決しましたか?

于 2012-08-16T09:23:22.560 に答える
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 に答える