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

embed Graphic in Word Document to address via form field 1

Status
Not open for further replies.

firegambler

Technical User
Sep 25, 2002
455
AT
Hi Folks,

I'd be very grateful if you could help me on the following:

I have a MS Word document that is connected to a Server via form fields.
Depending on a boolean value of a form field
a graphic should be displayed (or not).

For a reason that does not need to be explained here it is very unpractical for me to link to the image locally or via internet.

Is there a way to invisibly embed the image in the word doc so that I can address the embedded graphic in order to display it only when needed?

(In case you think of optionally just displaying placeholders instead of the graphics - this unfortunately won't do it for me)

Thanks in advance for your help!

regards

firegambler
 
Here is one way. It will only work if you have access to the template used to create the document.

1. In the template file, insert the image.
2. Select it.
3. Insert > AutoText > New
4. Give it a name (say, "MyPic")
5. Now the AutoText entry will default to Normal, so you need to explicitly move it to the template file. If the template file is protected for forms, unprotect it.
6. Tools > Templates and addins. Click Organizer.
7. Under the AutoText tab, find the AutoText in Normal (say, "MyPic"), and Copy it to the template file. Press OK.
8. Delete the inserted image.
9. Go to where you want the image to be, or not to be. Insert > Bookmark.
10. Give the bookmark a name, say "Pic". Press OK.

What this does:

A) Creates an explicit AutoText of the image
B) Creates a bookmark that will be used as the location to insert, or delete, that image.

Save your template file.

Assuming the checkbox formfield is named Check1 (the default...but it would be better to use an explicit name), then the following code set as the OnExit macro for the checkbox will do exactly as you want.
Code:
Sub YesMaybeNo()
ActiveDocument.Unprotect Password:=""
If ActiveDocument.FormFields("Check1").Result = True Then
   ActiveDocument.Bookmarks("Pic").Select
   AttachedTemplate.AutoTextEntries("MyPic").Insert _
    Where:=Selection.Range, RichText:=True
Else
   Selection.GoTo What:=wdGoToBookmark, Name:="Pic"
   ActiveDocument.Bookmarks("Pic").Range.Delete
   ActiveDocument.Bookmarks.Add Name:="Pic", _
      Range:=Selection.Range
End If
ActiveDocument.Protect wdAllowOnlyFormFields, _
   NoReset:=True, Password:=""
End Sub
What it does:

1. unprotects the document. required to use the bookmark.
2. checks the value of the checkbox.
3. if True (checked), selects the bookmark and then inserts the AutoText - ie. put the image in.
4. if False (unchecked), selects the bookmark, deletes the bookmark, then adds the same bookmark.
5. re-protect the document.

Testing: check the checkbox and Tab out of it. The OnExit macro fires. If checked, it inserts the image. If unchecked it totally removes the bookmark, then recreates it. Therefore, if previously checked (and the image is there), if unchecked, the bookmark (and the image) is deleted, and a blank bookmark is recreated at the same location.

It works. You can toggle back and forth checking and unchecking the checkbox. If checked - there is the image. If unchecked, there is no image.

I know of no way to "invisibly embed" an image. However, you can have an image there (or not) using AutoText and a bookmark.

faq219-2884

Gerry
My paintings and sculpture
 
Hi Gerry,

thanks a LOT for taking the time to write such a great tip!!!



regards

firegambler
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top