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

Decide which picture to present in a macro

Status
Not open for further replies.

ricaforrica

Programmer
Jun 30, 2005
65
PT
Greetings!

I have two pictures and I want to set which one will be visible when I open my word document, based on a template.

It depends on a variable I will get from a file: if "sex = male" I want the male schema, if "sex = female" I want the female schema.

I believe I could do this either inserting the chosen picture from the file system; or having them both on my template and deleting the not chosen...

Any ideas on how I should implement this?

Thanks for your ideas!
Best regards,

Ricardo
 

Hi,

I'd implement the former, especially if, at some time in the future, you may want different pix.

Skip,

[glasses] [red]Be advised:[/red]We know Newton's 3 Laws. But did you hear about the [red]FOURTH???[/red]
Only ONE fig per cookie![tongue]
 
Hey!

Yes, Skip, that was what I have done :D
The code is the following:

Sub Image()

Dim sex As String

' I placed a bookmark before the field where the sex information is and searched the word "Female", which got selected

Selection.GoTo What:=wdGoToBookmark, Name:="Sex"
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Female"
.Forward = True
End With
Selection.Find.Execute

'Defining the sex variable
sex = Selection.Text

' Going to the place where the image must be inserted (the bookmark actually is the image itself)
Selection.GoTo What:=wdGoToBookmark, Name:="Image"

'Test to add the correct image
If sex = "Female" Then
Selection.InlineShapes.AddPicture FileName:="C:\saude\Female.bmp", LinkToFile:=False, SaveWithDocument:=True
Else
Selection.InlineShapes.AddPicture FileName:="C:\saude\Male.bmp", LinkToFile:=False, SaveWithDocument:=True
End If

'Add a new bookmark to the new image
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
ActiveDocument.Bookmarks.Add Name:="Image", Range:=Selection.Range

End Sub


Thanks and enjoy VBA!

Ricardo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top