Word form with VBA

M

MAB

I am designing a form with word and VBA.
I would like the user of the form to be able to drag and drop a jpg into the
form, which would them be copied into the word doc.
 
J

Jonathan West

MAB said:
I am designing a form with word and VBA.
I would like the user of the form to be able to drag and drop a jpg into
the
form, which would them be copied into the word doc.

Drag & drop might be rather difficult. But you could have a Browse button on
your UserForm, which could display a dialog using the FileDialog object.

Once you have the filename of the jpg, you can assign it to an Image object
on the Userform, and in due course insert it into the document by using the
Shapes.Add method or InlineShapes.Add method.


--
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
 
M

MAB

Do you mean a "browse" botton so I could attach it to the word doc as a
result?
because that could work, just not sure how to do it.
 
J

Jonathan West

I got the impression that your "form" and your "word doc" were two separate
things, and was assuming that you were therefore using a UserForm (custom
dialog)

Based on what you have now said, I'm no longer sure that is the case. Could
you describe in a bit more detail what you are doing overall?

--
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
 
M

MAB

A user would open up a word templete (.dot) file. The form embedded into the
..dot file would prompt the user to fill out some fields, including one to
attach a jpg/gif/bmp into the form which would then be imported into the dot
file along with the answers to the other fields
 
D

Doug Robbins - Word MVP

For this you should be using a userform of the type discussed in the article
"How to create a Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

Then you need a button on the form with a caption something like "Browse for
Image" and for the Click() event for that form, you would use the following
code:

With Dialogs(wdDialogInsertPicture)
If .Display Then
'Populate the txtOrgChartPath control with the selected filename
txtOrgChartPath = WordBasic.FilenameInfo$(.Name, 1)
cmdInsertOrgChart.Enabled = True
End If
End With

In the form from which I grabbed this piece of code, the user was browsing
for a file that contained and organization chart and there was a textbox
(txtOrgChartPath) on the form that was populated with the path and filename
of the file that was selected by the user when the InsertPicture dialog was
displayed. The code also activated a button that when clicked, inserted the
organisation chart into the document using the following code:

Private Sub cmdInsertOrgChart_Click()
' Check that a file has been selected
If InStr(txtOrgChartPath, ":") = 0 Then
MsgBox "You must select a file for the Organization Chart."
Exit Sub
End If
'Insert organization chart into Quality Manual
Set myDoc = Documents.Open(PathofSystemFiles & "\Quality
Manual\Quality_Manual.doc")
If myDoc.Bookmarks.Exists("Org_Chart") = True Then
'Clear existing organization chart
myDoc.Bookmarks("Org_Chart").Range.Tables(1).Cell(1, 2).Range.Delete
'Insert new organization chart
myDoc.Bookmarks("Org_Chart").Range.Tables(1).Cell(1,
2).Range.InlineShapes.AddPicture Filename:=txtOrgChartPath
'Save and close the document
myDoc.Save
End If
myDoc.Close

End Sub

You will need to modify this for your situation, but it should show you the
necessary syntax to use.


--
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
 
J

Jonathan West

MAB said:
A user would open up a word templete (.dot) file. The form embedded into
the
.dot file would prompt the user to fill out some fields, including one to
attach a jpg/gif/bmp into the form which would then be imported into the
dot
file along with the answers to the other fields

Doug's approach is one way. Another is to include a MACROBUTTON field in the
document, like this

{ MACROBUTTON BrowseForPicture Double-click to insert a picture}, where the
{ } characters are field braces inserted by pressing Ctrl-F9.

Then the BrowseForPicture macro can display the Insert Picture dialog using
code based on what Doug has provided. To insert the picture into the
document, you may need to have the code temporarily unprotect the document,
insert the picture, and then re-protect the document.
 

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