Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help w/vb

Status
Not open for further replies.

gregmosu

Programmer
Jul 8, 2002
117
US
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.

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
 
Ok, I just ended up calling the macro that changes the text from the vb script, and it seems to work just fine. I just need to know how to edit the header and/or images while recording a macro. Is there some trick that I'm not aware of, cause the images and header seem to get locked....?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top