473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Replace text in a word document with an inline image

Hello,

First, I'd like to thank everyone on this forum who has been so helpful so far with a database I've been working on. I'm a bit of a newbie to VB, but I've been learning fast!

I'm trying to write a macro that will find all instances of "G~" in a Word document and replace it with an inline image (probably a small GIF or BMP). Has anyone any ideas how I might start going about this?

Hope you guys can help!

Jon
Aug 23 '07 #1
3 7613
Alternatively, an approach I have thought of is a loop that will go through the entire document and repeating a 'find' action and inserting the same image at each point. I'm not quite sure how to do this iterative 'find' process though.

Any ideas anyone?
Aug 24 '07 #2
I've managed it... here it is in case anyone needs it!

Expand|Select|Wrap|Line Numbers
  1. Sub replaceWithImage(sResponse As String, imageRelativePath As String)
  2.  
  3.     origDocPath = ActiveDocument.Path
  4.     imageFullPath = (origDocPath & "\" & imageRelativePath)
  5.  
  6.     'Application.ScreenUpdating = False
  7.     With Selection
  8.         .HomeKey Unit:=wdStory
  9.  
  10.         With .Find
  11.             .ClearFormatting
  12.             .Text = sResponse
  13.             ' Loop until Word can no longer
  14.             ' find the search string, inserting the specified image at each location
  15.             Do While .Execute
  16.                 Selection.MoveRight
  17.                 Selection.InlineShapes.AddPicture FileName:=imageFullPath, LinkToFile:=False, SaveWithDocument:=True
  18.             Loop
  19.  
  20.         End With
  21.     End With
  22.  
  23.     'Delete the original text
  24.     With Selection.Find
  25.         .ClearFormatting
  26.         .Text = sResponse
  27.         .Replacement.Text = ""
  28.         .Forward = True
  29.         .Wrap = wdFindContinue
  30.         .Format = False
  31.         .MatchCase = False
  32.         .MatchWholeWord = True
  33.         .MatchKashida = False
  34.         .MatchDiacritics = False
  35.         .MatchAlefHamza = False
  36.         .MatchControl = False
  37.         .MatchWildcards = False
  38.         .MatchSoundsLike = False
  39.         .MatchAllWordForms = False
  40.     End With
  41.     Selection.Find.Execute Replace:=wdReplaceAll
  42.  
  43.     'Application.ScreenUpdating = True
  44.  
  45. End Sub
  46.  
Aug 24 '07 #3
Killer42
8,435 Expert 8TB
Sorry to see you didn't get any response on that one, jonnyboy. Thanks very much for posting the answer, though. It'll be helpful to anyone else who comes along with a similar question.

Hope we're more help to you next time.

Keep up the good work - looks as though you're learning fast.
Aug 27 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

52
by: DJ WIce | last post by:
Hi all, I did make a script/css thing to replace the contextmenu on the website with a new one: http://www.djwice.com/contextmenu.html It works nice in MSIE, but on Netscape (and probable...
16
by: juglesh | last post by:
Hello, I need to look through the text on a page and replace certain words with an image or other word something like: read document find all instances of the word "blah" change all...
8
by: Orloff | last post by:
Hi, on most (all?) browsers, when you put the pointer on a <a href="..." title="popup text">this is a link</a> without clicking on the link, there is a popup caption with the "popup text". I...
1
by: adam lital | last post by:
Hi, I have word document and i want to replace a specific text with a bookmark using code. Example: in the document i have the text and i want to replace this to a bookmark called ClientName....
0
by: Balakrsihna | last post by:
Hi All, Can anyone tell me how to replace an image in MS Word 2003 using c#.net. I am converting an html file to word doc and sending this as attachment using System.Web.Mail; the...
5
by: Casey | last post by:
Hello, Can someone give me specific code to replace text on a page using server side javascript? I need to use server-side because I need the output to be recognized in the final HTML so that...
3
by: gregpinero | last post by:
Hi guys, What I'm trying to do is find all instances of an acronymn such as IBM on a webpage and replace it with <acronym title="International Business Machines">IBM</acronym>. However in my...
2
by: george.leithead | last post by:
Hi all, I have a very strange problem! In following Web page (which is generated from a CMS System), the navigation to the left 'dissapears' when you roll the mouse over the links? It does not...
1
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i am sure i will have the solution from experts. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.