0

我的文件夹中有很多 Word 文档,我想将自定义样式应用到这些文档中。

这是我的 VBA 代码。我希望 VBA 能够转到特定文件夹并将自定义样式应用于所有 word 文档。有任何想法吗?

Sub styleapply()
'
' styleapply Macro
'
'
    Selection.WholeStory
    ActiveDocument.UpdateStyles
    'WordBasic.ApplyQFSetTemplate
    Selection.Style = ActiveDocument.Styles("sam'style")
End Sub
4

1 回答 1

0

这应该让你大部分时间到达那里:

Sub OpenWordFolder()
    Dim fd As FileDialog
    Dim doc As Document
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    fd.AllowMultiSelect = True
    fd.Show
    For Each folderItem In fd.SelectedItems
        fileItem = Dir(folderItem & "\" & "*.docx")
        While fileItem <> ""
            Set doc = Documents.Open(FileName:=folderItem & "\" & fileItem)
            Selection.WholeStory
            Selection.Style = ActiveDocument.Styles("sam'style")
            doc.Close SaveChanges:=True
            fileItem = Dir
        Wend
    Next
End Sub

请注意,我不确定 ActiveDocument 是否具有您创建的自定义样式 - 您可能需要将具有自定义样式的原始文档设置为 Document 对象,然后使用该 Document 对象为每个文件设置样式我开了。

于 2013-03-25T22:29:09.413 回答