Wednesday, 5 June 2019

Mass Find and Replace for Pictures Macro - Microsoft Word 2013

I have this macro that is able to do a mass find and replace across multiple word documents. I am able to do a find and replace for pictures (within a header) but when I implemented it within my macro code it seems to just ignore it and not find and replace any pictures. I am unsure as to why this is the case since there are no errors. Any tips would be greatly appreciated. Thanks!!


Sub FindandReplaceTextPic()
Dim Directory As String
Dim FType As String
Dim FName As String

Directory = "C:\Users\pieria\Desktop\TempPics"
FType = "*.docx"

ChDir Directory
FName = Dir(FType)
' for each file you find, run this loop
Do While FName <> ""
' open the file
Documents.Open FileName:=Directory & "\" & FName

' search and replace the company name
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "CompanyA"
.MatchCase = True
.Replacement.Text = "CompanyB"
End With
Selection.Find.Execute Replace:=wdReplaceAll
'search and replace picture from clipboard
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^g"
.Replacement.Text = "^c"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' save and close the current document
ActiveDocument.Close wdSaveChanges

' look for next matching file
FName = Dir
Loop
End Sub

No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...