Show image on document dependent on entry in UserForm

C

Christine

Hello ..
I have a template that includes an image. I would like to show this image
only if a check box in the associated user form is clicked.
Is this possible?

Since it's a template, can the image actually be saved with the document?

Thanks from an expat Bavarian in NZ.
 
J

Jay Freedman

Hello ..
I have a template that includes an image. I would like to show this image
only if a check box in the associated user form is clicked.
Is this possible?

Since it's a template, can the image actually be saved with the document?

Thanks from an expat Bavarian in NZ.

Yes, all of that is possible.

First, save the image in the template as an AutoText entry. See
http://www.word.mvps.org/FAQs/Customization/AutoText.htm. (Be careful
to set the Look In dropdown of the Insert > AutoText > AutoText dialog
to your template before clicking the Add button.)

Next, insert a bookmark at the location in the template where you want
the image to appear when it's enabled.

In the Click procedure for your OK button, write code like this,
replacing "MyPic" and "MyBkMk" with the names of your AutoText entry
and your bookmark, respectively:

Private Sub CommandButton1_Click()
Dim MyRg As Range
Set MyRg = ActiveDocument.Bookmarks("MyBkMk").Range
If CheckBox1.Value = -1 Then ' checked
With ActiveDocument
Set MyRg = .AttachedTemplate.AutoTextEntries("MyPic") _
.Insert(Where:=MyRg, RichText:=True)
.Bookmarks.Add Name:="MyBkMk", Range:=MyRg
End With
Else
If MyRg.InlineShapes.Count > 0 Then
MyRg.InlineShapes(1).Delete
ActiveDocument.Bookmarks.Add Name:="MyBkMk", Range:=MyRg
End If
End If
Me.Hide
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
S

Sam

This piece of code is almost what I've been looking for, but I need to check
a box that inserts a text autotext entry instead of an image. I've tried
playing around with it but can't get it to work properly. It inserts the
autotext entry correctly, it just doesn't remove it once I uncheck the box.

Any help would be greatly appreciated

Thanks in anticipation



Else
If MyRg.InlineShapes.Count > 0 Then
MyRg.InlineShapes(1).Delete
ActiveDocument.Bookmarks.Add Name:="MyBkMk", Range:=MyRg
End If
 
D

Doug Robbins - Word MVP on news.microsoft.com

If the autotext is being inserted into a Range, set the .Text attribute of
the Range to "" if you do not want the autotext to be displayed.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
S

Sam

Ah... it took me a while to figure out how to do it, but eventually I got it
working. I wrote the code like this:

Else
Set MyRg = ActiveDocument.Bookmarks("MyBkMk").Range
MyRg.Text = ""
ActiveDocument.Bookmarks.Add Name:="MyBkMk", Range:=MyRg
End If

Maybe there's a better way, but at least it works!

Thanks very much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top