insert image in document depending on optionselected

C

colm o'brien

I am trying to code a document with several user forms and bookmarks one user
form frmadvisor has a frame with 4 radio buttons and depending on which
option is selected it adds the adviser's name to the letter. I would also
like to add a jpeg image of the adviser into another bookmark depending on
which option is set. but don't know how to satr.

I would store the images in the dirctory where the template is stored and
all image would be the same size.

any help would be appreciated.

Colm
 
D

DaveLett

Hi Colm,

Here's how you add a picture to an existing bookmark:

ActiveDocument.InlineShapes.AddPicture _
FileName:=sFileName, _
Range:=ActiveDocument.Bookmarks("Test").Range

Note that this DELETES the bookmark. We can get around that if needed. You
would only have to supply the value for sFileName. I assume that you want the
picture to appear in the same place in the document, regardless of which
adviser was selected. Therefore, I recommend that you set sFileName in the
branching for the options:

Select Case True
Case opt1 '''adviser 1
sFileName = "1.jpg"
Case opt2 '''adviser 2
sFileName = "2.jpg"
.....'''and so on
End Select

ActiveDocument.InlineShapes.AddPicture _
FileName:="C:\Temp\Advisers\" & sFileName, _
Range:=ActiveDocument.Bookmarks("Test").Range

Hope this gets you started,
Dave
 
C

colm o'brien

Dave

Thanks for your input i have added your code in but when i get a runtime error

"Runtime Error 5152
This is not a valid filename
try one or more of the following
*check the path to make sure it is typed correctly
*select a file from the list of files and folders"

The debug button highlights the 3 lines marked XXX

I am a novice at this and therefore any remarks on the code snippets to help
explain are very useful to me.

Private Sub OK_Click()
Dim stradvisor As String
If OptionButton1 = True Then stradvisor = "Paul Digney Cert.PFS"
If OptionButton2 = Trus Then stradvisor = "Colum Lynch Cert.PFS"
If OptionButton3 = True Then stradvisor = "Paddy O'Hanlon"
If OptionButton4 = True Then stradvisor = "Mark Williams"
xxx ActiveDocument.InlineShapes.AddPicture _
xxx FileName:=sFileName, _
xxx Range:=ActiveDocument.Bookmarks("image").Range
Select Case True
Case opt1 '''Paul Digney
sFileName = "pd.jpg"
Case opt2 '''Colum Lynch
sFileName = "cj.jpg"
Case opt3 '''Paddy O'Hanlon
sFileName = "poh.jpg"
Case opt4 '''Mark Williams
sFileName = "mw.jpg"
End Select

ActiveDocument.InlineShapes.AddPicture _
FileName:="x:\workgrouptemplates\images\" & sFileName, _
Range:=ActiveDocument.Bookmarks("image").Range

With ActiveDocument
.Bookmarks("Address").Range _
.InsertBefore Address
.Bookmarks("Salutation").Range _
.InsertBefore Salutation
.Bookmarks("provider").Range _
.InsertBefore Provider
.Bookmarks("policydescription").Range _
.InsertBefore Policydescription
.Bookmarks("Policyno").Range _
.InsertBefore Policyno
.Bookmarks("adviser").Range.Text = stradvisor
.InsertBefore adviser

End With
frmpolicyenc.Hide

End Sub
 
G

Graham Mayor

The error occurs because sFilename has not been defined at that point.
It is defined later in your case selection. As the rest of the code and the
form details are unknown the following may be nearer the mark

Dim stradvisor As String
Dim sFilename As String
If OptionButton1 = True Then stradvisor = "Paul Digney Cert.PFS"
If OptionButton2 = True Then stradvisor = "Colum Lynch Cert.PFS"
If OptionButton3 = True Then stradvisor = "Paddy O'Hanlon"
If OptionButton4 = True Then stradvisor = "Mark Williams"

Select Case stradvisor
Case Is = "Paul Digney Cert.PFS"
sFilename = "pd.jpg"
Case Is = "Colum Lynch Cert.PFS"
sFilename = "cj.jpg"
Case Is = "Paddy O'Hanlon"
sFilename = "poh.jpg"
Case Is = "Mark Williams"
sFilename = "mw.jpg"
End Select

ActiveDocument.InlineShapes.AddPicture _
FileName:="x:\workgrouptemplates\images\" & sFilename, _
Range:=ActiveDocument.Bookmarks("image").Range

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
C

colm o'brien

Thanks Graham

That's sorted out my problem.



Graham Mayor said:
The error occurs because sFilename has not been defined at that point.
It is defined later in your case selection. As the rest of the code and the
form details are unknown the following may be nearer the mark

Dim stradvisor As String
Dim sFilename As String
If OptionButton1 = True Then stradvisor = "Paul Digney Cert.PFS"
If OptionButton2 = True Then stradvisor = "Colum Lynch Cert.PFS"
If OptionButton3 = True Then stradvisor = "Paddy O'Hanlon"
If OptionButton4 = True Then stradvisor = "Mark Williams"

Select Case stradvisor
Case Is = "Paul Digney Cert.PFS"
sFilename = "pd.jpg"
Case Is = "Colum Lynch Cert.PFS"
sFilename = "cj.jpg"
Case Is = "Paddy O'Hanlon"
sFilename = "poh.jpg"
Case Is = "Mark Williams"
sFilename = "mw.jpg"
End Select

ActiveDocument.InlineShapes.AddPicture _
FileName:="x:\workgrouptemplates\images\" & sFilename, _
Range:=ActiveDocument.Bookmarks("image").Range

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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