如何使用BatchReplaceAll替换页脚中的字体 [英] How to Use BatchReplaceAll to Replace Font in Footer

查看:97
本文介绍了如何使用BatchReplaceAll替换页脚中的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道如何编写visual basic,但在阅读时理解大多数代码。  我需要更换eras book和eras demi以便在超过500个Word文档中删除本书和eras lt demi。


在网上找到组合的代码片段(见下文)在多个Word文档中替换字体,但无法使其适用于页脚。  任何有助于使此代码适用于页脚的帮助表示赞赏。



Sub BatchReplaceAll()


 


Dim FirstLoop As Boolean


Dim myFile As String


Dim PathToUse As String


Dim myDoc As Document


Dim Response As Long


 


'对Word版本2002-7使用此文件夹选择


Dim fDialog As FileDialog


设置fDialog = Application.FileDialog(msoFileDialogFolderPicker)


 


'获取包含文件的文件夹


使用fDialog


      .Title ="选择包含要修改的文档的文件夹,然后点击确定"


      .AllowMultiSelect = False


      .InitialView = msoFileDialogViewList


  &NBSP;  如果.Show<> -1然后


  &NBSP; &NBSP; &NBSP; &NBSP; MsgBox"已取消用户","批量替换任何地方"


  &NBSP; &NBSP; &NBSP; &NBSP;退出子


  &NBSP;  结束如果


  &NBSP;   PathToUse = fDialog.SelectedItems.Item(1)


  &NBSP;  如果正确(PathToUse,1)<> " \"然后PathToUse = PathToUse +" \"


结束


 


'关闭任何可能打开的文件


如果Documents.Count> 0然后


  &NBSP;   Documents.Close Savechanges:= wdPromptToSaveChanges


结束如果


FirstLoop = True


 


'*************************************** ****************


'文件夹结束选择


 


 


'错误处理程序,用于处理


'时生成的错误,FindReplace对话框已关闭


 


On Error Resume Next


 


'全部关闭开始前的文件


 


Documents.Close Savechanges:= wdPromptToSaveChanges


 


'布尔表达式,用于测试是否第一个循环


'使用此方法,以便FindReplace对话框仅


'显示第一个文件


 


FirstLoop = True


 


'将文件的目录和类型设置为批处理


 


myFile = Dir $(PathToUse& " *。doc的x")


 


myFile<> ""


 


  &NBSP; '打开文件


  &NBSP;设置myDoc = Documents.Open(PathToUse& myFile)


 


  &NBSP;如果是FirstLoop那么


 


  &NBSP; &NBSP; &NBSP; '仅在第一次循环时显示对话框


 


  &NBSP; &NBSP; &NBSP;对话框(wdDialogEditReplace)。显示


 


  &NBSP; &NBSP; &NBSP; FirstLoop = False


 


  &NBSP; &NBSP; &NBSP;响应= MsgBox("您想要处理"& _


    """<<  """ / p>

  &NBSP; &NBSP; &NBSP;如果Response = vbNo Then Exit Sub


 


  &NBSP;否则


 


  &NBSP; &NBSP; &NBSP; '在后续循环(文件)中,ReplaceAll是


  &NBSP; &NBSP; &NBSP; '使用原始设置执行且不使用


  &NBSP; &NBSP; &NBSP; '再次显示对话框


 


  &NBSP; &NBSP; &NBSP;使用对话框(wdDialogEditReplace)


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; .ReplaceAll = 1


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; .Execute


  &NBSP; &NBSP; &NBSP;结束


 


  &NBSP;结束如果


 


  &NBSP;保存更改后关闭修改后的文档


 


  &NBSP; myDoc.Close Savechanges:= wdSaveChanges


 


  &NBSP; '文件夹中的下一个文件


 


  &NBSP; myFile = Dir $()


 


Wend


 


End Sub


解决方案

这是VSN中VB的论坛,尝试使用VBA论坛代替此代码。


http://social.msdn.microsoft.com/Forums/en-US/isvvba/threads


don't know how to write visual basic but understand most code when reading.  i need to replace eras book and eras demi to eras lt book and eras lt demi in over 500 Word documents.

have combined pieces of code (see below) found online to find / replace a font  in multiple Word documents, but can't get it to work for footers.  any help to make this code work for  footers is appreciated.

Sub BatchReplaceAll()

 

Dim FirstLoop As Boolean

Dim myFile As String

Dim PathToUse As String

Dim myDoc As Document

Dim Response As Long

 

' Use this folder selection for Word versions 2002-7

Dim fDialog As FileDialog

Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

 

' Get the folder containing the files

With fDialog

     .Title = "Select Folder containing the documents to be modified and click OK"

     .AllowMultiSelect = False

     .InitialView = msoFileDialogViewList

     If .Show <> -1 Then

          MsgBox "Cancelled By User", , "Batch Replace Anywhere"

          Exit Sub

     End If

     PathToUse = fDialog.SelectedItems.Item(1)

     If Right(PathToUse, 1) <> "\" Then PathToUse = PathToUse + "\"

End With

 

'Close any documents that may be open

If Documents.Count > 0 Then

     Documents.Close Savechanges:=wdPromptToSaveChanges

End If

FirstLoop = True

 

'*******************************************************

'End of Folder selection

 

 

'Error handler to handle error generated whenever

'the FindReplace dialog is closed

 

On Error Resume Next

 

'Close all open documents before beginning

 

Documents.Close Savechanges:=wdPromptToSaveChanges

 

'Boolean expression to test whether first loop

'This is used so that the FindReplace dialog will

'only be displayed for the first document

 

FirstLoop = True

 

'Set the directory and type of file to batch process

 

myFile = Dir$(PathToUse & "*.docx")

 

While myFile <> ""

 

    'Open document

    Set myDoc = Documents.Open(PathToUse & myFile)

 

    If FirstLoop Then

 

        'Display dialog on first loop only

 

        Dialogs(wdDialogEditReplace).Show

 

        FirstLoop = False

 

        Response = MsgBox("Do you want to process " & _

        "the rest of the files in this folder", vbYesNo)

        If Response = vbNo Then Exit Sub

 

    Else

 

        'On subsequent loops (files), a ReplaceAll is

        'executed with the original settings and without

        'displaying the dialog box again

 

        With Dialogs(wdDialogEditReplace)

            .ReplaceAll = 1

            .Execute

        End With

 

    End If

 

    'Close the modified document after saving changes

 

    myDoc.Close Savechanges:=wdSaveChanges

 

    'Next file in folder

 

    myFile = Dir$()

 

Wend

 

End Sub

解决方案

This is the forum for VB in VSN, try the VBA forum instead of this for your code.

http://social.msdn.microsoft.com/Forums/en-US/isvvba/threads


这篇关于如何使用BatchReplaceAll替换页脚中的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆