VBA bulk replaces content in multiple Word documents

Source: Internet
Author: User

In order to replace the contents of many Word documents in a flash, we have to use the VBA method to achieve, the following is the methods, please see.

First, preliminary preparation

The following is a detailed procedure.

A, first place multiple Word documents that need to be bulk replaced under the same folder.

B, create a new blank Word document, right-click the blank toolbar, and click the Control Toolbox to see the Control Toolbox that is displayed on the screen.

C, on the Control Toolbox, click Command button, and a button is placed in the document.

D, double-click the button, enter the VB code writing mode, the following code to copy in.

The code of the command button

Private Sub CommandButton1_Click ()

application.screenupdating = False

Dim Mypas As String, MyPath as String, I as Integer, MyDoc as Document

With Application.filedialog (msoFileDialogFolderPicker)

. Title = "Select destination Folder"

If. Show =-1 Then

MyPath =. SelectedItems (1)

Else

Exit Sub

End If

End With

Mypas = InputBox ("Please enter Open password:")

With Application.FileSearch

. LookIn = MyPath

. FileType = msofiletypeworddocuments

If. Execute > 0 Then

For i = 1 to. FoundFiles.Count

Set MyDoc = Documents.Open (filename:=. FoundFiles (i), Passworddocument:=mypas)

Selection.Find.ClearFormatting

Selection.Find.Replacement.ClearFormatting

With Selection.Find

. Text = "Officestudy"

. Replacement.text = "Www.dzwebs.com"

. Forward = True

. Wrap = Wdfindask

. Format = False

. MatchCase = False

. MatchWholeWord = False

. MatchByte = True

. MatchWildcards = False

. MatchSoundsLike = False

. MatchAllWordForms = False

End With

Selection.Find.Execute Replace:=wdreplaceall

Mydoc.save

Mydoc.close

Set MyDoc = Nothing

Next

End If

End With

application.screenupdating = True

End Sub

Save the above code, exit VB edit mode, return to the Word document interface.

Click to select the button, and then click the First button in the Control Toolbox to exit design mode.

Finally, you can test: clicking the button again will reveal that "officestudy" in all Word documents underneath the folder is replaced with "www.dzwebs.com".

Note: In actual use, you can change the "" in the Above Code "" "in the quotation marks of the characters in the actual need to bulk replacement of the content. as shown below.

. Text = "Officestudy"

. Replacement.text = Www.dzwebs.net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.