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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing text with an image

Status
Not open for further replies.

Graedon

IS-IT--Management
Sep 20, 2006
13
US
I must say, this is a great site...you guys are on the ball with the answers. Now, let's see if I can get 2 for 2!

Best case scenario, I would like to have Word automatically parse certain strings of characters to an inline shape. But, I've been working on a macro that will just parse all these strings after the document is complete. The macro searches for all text matching strings listed in an outside file. Here's the bit of the code that I am having a "ByRef argument type mismatch" error on.

I am a total newb to VBA, so please be kind! I'm a php guy...if only I could use php code! :-D

Code:
Sub ParseSmileys()

' Set options for the find-replace function for all of this type

With Selection.Find

   .ClearFormatting

   .Replacement.ClearFormatting

   .Forward = True

   .MatchCase = True

   .Wrap = wdFindStop

   .MatchWildcards = False

   .MatchWholeWord = True

End With

' Open the file containing the comma/quotation mark delimited list

Open "\\Server\IT Files\Macros\Emoticons\Smileys.txt" For Input As #1

' Loop through to the end of the file.

Do While Not EOF(1)

' Get the replacement words from the opened file.

   Input #1, fileChangeFrom, fileChangeTo
   
   EmotIcon = EmotImage(fileChangeTo, "Small")

' Replace all instances of the word to be replaced.

   Selection.Find.Execute Findtext:=fileChangeFrom, ReplaceWith:=EmotIcon, MatchWholeWord:=True, Replace:=wdReplaceAll

Loop

Close #1
    
End Sub

The delimited list looks something like this:
":)","Happy"

So, I have a sub EmotImage(strName, strSize) which inserts the correct image on the page.
here's the code for that:
Code:
Sub EmotImage(strImage As String, strSize As String)

    ' Insert the correct image
    Set oPicture = _
    Selection.InlineShapes.AddPicture _
    (FileName:="\\Server\IT Files\Macros\Emoticons\Images\" & strSize & "\" & strImage & ".png", _
        LinkToFile:=False, SaveWithDocument:=True)
    
    ' Now set the emoticon's size
    If strSize = "Small" Then
    oPicture.Width = 12
    oPicture.Height = 12
    End If
    
    If strSize = "Medium" Then
    oPicture.Width = 18
    oPicture.Height = 18
    End If
    
    If strSize = "Large" Then
    oPicture.Width = 24
    oPicture.Height = 24
    End If
    
End Sub

So, looking at all that crap that I'm probably doing totally wrong...can someone help me out?

Thanks!!!
Matt
 
No one has a suggestion for this? :-(
 
Walk me through this. I am not undestanding what the problem is. Actually, I am having a hard time figuring out what you are trying to do.

On one hand you state you want to parse text, search for a specific string, and change it to your inlineshape. OK, first of all you would need to open the file as a Word document. You do not seem to be doing that.

Next...am i following this? You want to replace all instances of the specified text with an inlineshape?

Where is the error? Please mark your code exactly.

What precisely is not going correctly?

Actually, I can guess where the error is:
Code:
ReplaceWith:=EmotIcon,
ReplaceWith has to be a string! EmotIcon is a procedure! ByReference, this is amismatch. A prcoedure is not a string. A string is expected.

Do your search, when it is found, delete the selection, then run the procedure at the collapsed Selection point. I am not clear on what exactly your are doing with:
Code:
Do While Not EOF(1)

' Get the replacement words from the opened file.

   Input #1, fileChangeFrom, fileChangeTo
   
   EmotIcon = EmotImage(fileChangeTo, "Small")

' Replace all instances of the word to be replaced.

   Selection.Find.Execute Findtext:=fileChangeFrom, ReplaceWith:=EmotIcon, MatchWholeWord:=True, Replace:=wdReplaceAll

Loop
Where are fileChangeFrom and fileChangeTo coming from? They seem to appear magically in your code.

In any case, you can not use the procedure EmotIcon as a ReplaceWith parameter.



Gerry
My paintings and sculpture
 
Hey Gerry,

Sorry for the delayed response...I've been out of town for a week.

Okay, I got the original ParseSmileys() sub from another site...only the sub was to find and replace specific chnks of text and replace that text with other text. It works fine for that. There's a file containing a delimited list with the text to search for and the text to change the found text to. Hence the vars fielChangeFrom and fileChangeTo. So, no, they don't appear magically, it's right there in the code I posted. It opens the outside file, uses a loop to go through the file and replace the text strings in the document.

Now, this only works while you have the Word document open. This code does not open a document for you. You have to have a text area selected for this code to search through.

Originally I used this to strip out html tags from old sent mail my office transferred over from Eudora to Outlook. Outlook leaves all the html tags in the body of the email. VERY hard to read. So, I just did this to strip it all out.

NOW, what I am trying to change this code to do is instead of replacement text, to replace the specified text with an IMAGE.

I'm not shy at all to say that I don't know how to do this. I don't know VBA. And yes, I know that it wants a string, like the replacement text was originally a string and that's why it doesn't work. So, that's why I'm asking for an alternative way of doing this. Because I am ignorant! :-D

So, can you suggest a cleaner way of doing this? I don't mind scrapping all the code I already have to do it! :)

Thanks Gerry!
Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top