I'm trying to put together a console application that will
1) Delete the first image in the word doc and replace it w/another one.
2) Find a text string, and replace it with other text.
I had help from other members of this site to get the first part of this done, but the only problem w/it is that it wont replace the image in the header.
I tried to use the macro recorder to figure out how to delete and add images to the header, but once I start the recorder, it wont let me do anything to any image.
As for the second part, I used the macro recorder to get the part that replaces text, but it doesnt work quite right. It only replaces the first X with the text, and leaves the second two.
Any help would be greatly appreciated.
1) Delete the first image in the word doc and replace it w/another one.
2) Find a text string, and replace it with other text.
I had help from other members of this site to get the first part of this done, but the only problem w/it is that it wont replace the image in the header.
I tried to use the macro recorder to figure out how to delete and add images to the header, but once I start the recorder, it wont let me do anything to any image.
As for the second part, I used the macro recorder to get the part that replaces text, but it doesnt work quite right. It only replaces the first X with the text, and leaves the second two.
Any help would be greatly appreciated.
Code:
Dim oFile As String
oFile = Dir("c:\temp\*.doc")
Do While oFile <> ""
Dim WordApp As New Microsoft.Office.Interop.Word.ApplicationClass
Dim zzz As Microsoft.Office.Interop.Word.WdGoToItem = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToGraphic
WordApp.Documents.Open(FileName:="c:\temp\" & oFile)
'Swap graphic
With WordApp.Selection
.GoTo(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToGraphic, Count:=1)
.Delete()
.InlineShapes.AddPicture(FileName:="C:\temp\2.jpg")
End With
'Change text
WordApp.Selection.Find.ClearFormatting()
WordApp.Selection.Find.Replacement.ClearFormatting()
With WordApp.Selection.Find
.Text = "XXX"
.Replacement.Text = "Company Name"
.Forward = True
'.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
WordApp.Selection.Find.Execute()
If WordApp.ActiveDocument.Saved = False Then WordApp.ActiveDocument.Save()
WordApp.ActiveDocument.Close()
oFile = Dir()
Loop