Solved

need help with macro that will automatically replace certain words and phrase with a picture

Posted on 2013-06-26
19
461 Views
Last Modified: 2013-06-26
I need to rapidly replace certain word or phrase found in a series of documents with pictures
How would I go about the replace word or phrase with picture part of the macro
0
Comment
Question by:Dov_B
  • 10
  • 6
  • 3
19 Comments
 
LVL 13

Expert Comment

by:Stacy Brown
ID: 39278998
You can use the advanced find dialog to do this.  You process will just need a tiny bit of setup.

Part 1 - Prep the Picture

Insert your picture into a blank Word Document
Save it for good measure
Select the picture
COPY

Part 2 - Advanced find
Open up the document you need to modify
Click the down arrow next to Find on the Home ribbon
Choose Advanced Find
Click the REPLACE Tab
Click on the Find line and type the word/phrase you want to replace
Click on the REPLACE line
Click the MORE button if necessary
Click the Special button
Select Clipboard Contents
This should place a ^c on the Replace line
Hit REPLACE ALL

That's it you're done!
0
 

Author Comment

by:Dov_B
ID: 39279005
how can I turn on the macro recorder and get those steps in vba script
0
 

Author Comment

by:Dov_B
ID: 39279025
well it does insert the picture from the clipboard but it puts at the beginning of the lina and not where the word to be replaced was
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39279030
Are you dealing with one picture?
0
 

Author Comment

by:Dov_B
ID: 39279040
B"H your here just in time!
No I am going to have a whole series of icons that replace a whole series of words in zillions of docs
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39279047
Is replacing one at a time acceptable?
0
 

Author Comment

by:Dov_B
ID: 39279053
yes
0
 
LVL 27

Accepted Solution

by:
MacroShadow earned 300 total points
ID: 39279100
In which case the following should work:
Option Explicit

Sub ProcessAll(strPath As String, strTxtToReplace As String, strImgPath As String)
    Dim WdDoc As Document, strFile As String

    strFile = Dir(strPath & "*.docx")
    'Loop through all .docx files in that path
    Do While strFile <> ""
        Set WdDoc = Application.Documents.Open(strPath & strFile)

        'Do your Search/Replace
        ReplaceTextWithImage strTxtToReplace, strImgPath
        
        'Save it
        WdDoc.Close wdSaveChanges
        strFile = Dir
    Loop
End Sub

Public Sub ReplaceTextWithImage(strTextToReplace As String, strImagePath As String)
  
    Application.ScreenUpdating = False
    With Selection
        .HomeKey Unit:=wdStory

        With .Find
            .ClearFormatting
            .Text = strTextToReplace
            ' Loop until Word can no longer
            ' find the search string, inserting the specified image at each location
            Do While .Execute
                Selection.MoveRight
                Selection.InlineShapes.AddPicture FileName:=strImagePath, LinkToFile:=False, SaveWithDocument:=True
            Loop
        End With
    End With

    'Delete the original text
    With Selection.Find
        .ClearFormatting
        .Text = strTextToReplace
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

    Application.ScreenUpdating = True

End Sub

Open in new window


Call it using the necessary parameters. i.e.
ProcessAll "C:\", "Whatever", "C:\Test\icon.jpg"

Open in new window

0
 
LVL 13

Assisted Solution

by:Stacy Brown
Stacy Brown earned 200 total points
ID: 39279109
if you do actually want VBA code.  This will do basically what I listed above.  You will need to do a couple things in order for this code to work

Create a word document that contains just the picture you wish to replace words with
Save it locally

Open your find/replace document.  Make sure this is the first document you open

Here is the code:

Sub ComboInsertPicReplaceWordWithPicCode()

Dim strPicFileName As String
Dim Message, Title, Default, MyValue

'Get the path to the file with the picture in it

Message = "What is the full path to the document containing the picture?"    ' Set prompt.
Title = "Picture File Name"    ' Set title.
Default = "C:\Users\[username]\Documents\PicDoc.docx"    ' Set default.
' Display message, title, and default value.
strPicFileName = InputBox(Message, Title, Default)

'Open the file with the picture in it.
'IMPORTANT:  The picture must be the only thing inserted in this file and must be inserted at thevery beginning of the file.
'There should only be one line (paragraph) in the document

Documents.Open FileName:=strPicFileName, ConfirmConversions:=False, _
        ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.Copy

Windows(1).Activate

Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "dolor" 'replace this with the text you want to find
        .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
    Selection.HomeKey Unit:=wdStory
End Sub

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Expert Comment

by:Stacy Brown
ID: 39279111
Wow...sorry folks...looks like I missed a bunch of the conversation.
0
 

Author Comment

by:Dov_B
ID: 39279160
Dear MacroShadow,
This has happened to me before when using inline shapes it errored out attached is a screan shot with the error what it says is

Run-time error '-2147467259(80004005)':
Method 'AddPicture' of object 'InlineShapes' failed
6-26-2013-2-37-03-PM.png
0
 

Author Comment

by:Dov_B
ID: 39279187
ok I fixed it with this
ActiveDocument.SetCompatibilityMode wdWord2007
but still it just stuck the pictures in the begiining of the doc not where the word to be replaced was found
0
 

Author Comment

by:Dov_B
ID: 39279198
waqit no that did not fix it
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39279218
Can you please upload the document, I just tried it again (Word 2010) and it works fine.
0
 

Author Comment

by:Dov_B
ID: 39279223
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39279265
Worked fine by me, see attached, guess which word was replaced.
THE-AVOS-NEZIKIN.zip
0
 

Author Comment

by:Dov_B
ID: 39279278
there is nothing in the zip! Sorry but if possible please resend. Thanks!
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39279328
There is something weird with your computer, I just downloaded the zip and it contains the THE-AVOS-NEZIKIN.docm file.

Anyway, here it is again.
THE-AVOS-NEZIKIN.zip
0
 

Author Comment

by:Dov_B
ID: 39279370
ok I unzipped it with a different unzipping program and the word is toldah Nice pic! but here is the "zach" number one dont u know no loshon hura is permitted so please dont call my computer names. 2nd of its all I got what do I do about that blessed (I practice what I preach) error any ideas on that. In any event I will award you all most of the points and post that as a speerate issue. Thanks for that macro its a work of art! May you be blessed again and again the zechus ot the tinokos shel bais raban is with you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now