VBA code to select a list of photos and then add them to a word doc

D

danny

I would like to be able to select from a picture from a list of pictures and
then add that photo added to a open word document. The code listed below
will add the 2 pictures but i cannot choose only one to add. Can anyone
help.

thanks
danny

Sub Addpic()
'
'
Dim objShape

Set objShape = ActiveDocument.Shapes
objShape.AddPicture ("s:\pictures\Logos\aca.jpg")
objShape.AddPicture ("s:\pictures\Logos\HubZone.jpg")

End Sub
 
K

Karl E. Peterson

danny said:
I would like to be able to select from a picture from a list of pictures and
then add that photo added to a open word document. The code listed below
will add the 2 pictures but i cannot choose only one to add. Can anyone
help.

thanks
danny

Sub Addpic()
'
'
Dim objShape

Set objShape = ActiveDocument.Shapes
objShape.AddPicture ("s:\pictures\Logos\aca.jpg")
objShape.AddPicture ("s:\pictures\Logos\HubZone.jpg")

End Sub

How do you envision the user making the selection? I mean, you could pop up
MsgBox's, one at a time, asking for both. Or, you could pop up a UserForm, with a
more formal list. The choices are really many. Where do you see this going?
 
K

Karl E. Peterson

Have you ever created a userform? Do you have any documents that contain userforms,
which you could inspect, to use as examples? I guess what I"m asking is sort of a
classic usenet question -- what part of this problem are you having difficulty with?
 
D

danny

Karl
I'm having trouble with the combo box code, listed below is the code i've
wrote
I have two pictures listed aca and hubzone. When i run the combo box it will
load the aca picture but it will not load the hubzone picture.

thanks
danny


Private Sub UserForm_Initialize()
With Me.ComboBox1
..AddItem "aca"
..AddItem "Hubzone"
'.ListIndex = 0
Dim aca
Dim Hubzone
Dim objShape
Set objShape = ActiveDocument.Shapes
If Me.ComboBox1 = aca Then
objShape.AddPicture ("s:\pictures\Logos\aca.jpg")
Else
objShape.AddPicture ("s:\pictures\Logos\HubZone.jpg")
End If
End With

End Sub
 
K

Karl E. Peterson

Hmmmm, usually a combobox is coded such that something happens when the *user*
selects an item. Is that what you wanted? If so, I'd suggest you only load the
entries for the combobox in the Initialize event, and then load the objects in its
Click event.



danny said:
Karl
I'm having trouble with the combo box code, listed below is the code i've
wrote
I have two pictures listed aca and hubzone. When i run the combo box it will
load the aca picture but it will not load the hubzone picture.

thanks
danny


Private Sub UserForm_Initialize()
With Me.ComboBox1
.AddItem "aca"
.AddItem "Hubzone"
'.ListIndex = 0
Dim aca
Dim Hubzone
Dim objShape
Set objShape = ActiveDocument.Shapes
If Me.ComboBox1 = aca Then
objShape.AddPicture ("s:\pictures\Logos\aca.jpg")
Else
objShape.AddPicture ("s:\pictures\Logos\HubZone.jpg")
End If
End With

End Sub
 
D

danny

Karl
Could you give me a example of this, i'm a little confused on this
thanks
danny
 
K

Karl E. Peterson

danny said:
Karl
Could you give me a example of this, i'm a little confused on this

Okay, add a new UserForm to a document, and put a combobox and a label control on
it. Then add this code:

Option Explicit

Private Sub ComboBox1_Click()
Label1.Caption = "You selected: " & ComboBox1.Text
End Sub

Private Sub UserForm_Initialize()
Dim i As Long
For i = 1 To 10
ComboBox1.AddItem "Item #" & i
Next i
End Sub

When you run it, and select items in the list, that selection is reflected in the
Label control. Your code appears to have combined these two event procedures into
one?


 
D

danny

Karl
Thanks for the code, i was able to modify it some and get it to work, but
i'm having a problem getting the userform to open. I would like to have the
form open by using a macro that way it can be used on more than one
doucment. I looked all over the internet and just can't find any code that
would let me do this with a macro.

thanks
danny
 
J

Jonathan West

danny said:
Karl
Thanks for the code, i was able to modify it some and get it to work, but
i'm having a problem getting the userform to open. I would like to have
the form open by using a macro that way it can be used on more than one
doucment. I looked all over the internet and just can't find any code that
would let me do this with a macro.

Take a look at these articles

What do Templates and Add-ins store?
http://www.word.mvps.org/FAQs/Customization/WhatTemplatesStore.htm

Distributing macros to other users
http://www.word.mvps.org/FAQs/MacrosVBA/DistributeMacros.htm


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
D

danny

I'm sorry but neither of these articles will help me with what i need.
Macro's are stored with the name of SUB and the marcos name, the VBA code
that is used for the user form is private sub, when i try and use the code
listed below in a macro it wiil not work

Private Sub Form_load()

frmUserForm1.Show

End Sub

If i try this code below then i get the error that the command frmUserForm1
has been defined.


Sub Macro2()

frmUserForm1.Show
End Sub
 
K

Karl E. Peterson

Put the .Show command in your "macro" (within a standard BAS module), not within the
form module itself.
 

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