2013-03-25 92 views
0

我在我想要应用我定制的样式的文件夹中有大量的文档文档。在Word文档中更新并应用自定义样式

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

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

请问您可以告诉我们您尝试过什么?有很多示例展示了如何遍历选定的单词文件。你有没有试过它们? – 2013-03-25 10:21:16

+0

我是vba的新手..根据我的要求,我记录了宏..并得到了问题中提到的代码,它的工作,但我有很多文档,所以通过所有文档循环代码..在那个目录中.. – 2013-03-25 14:06:41

回答

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对象为每个打开的文件设置样式。

相关问题