Picture to Ms Word via VBA??

M

Mojo

Hi All

I'm using a VB app to interact with Ms Word.

I've given the user the ability to choose their own JPGs within my app, but
is there anyway I can automatically put these JPGs into MS Word via my
app/VBA??

Thanks
 
D

Doug Robbins - Word MVP

Using the command

Dialogs(wdDialogInsertPicture).Show

will allow the user to select and insert a picture.

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

Jeff Johnson

For future reference, cross-posting to microsoft.public.vb.controls,
microsoft.public.vb.enterprise, and microsoft.public.vb.general.discussion
was excessive. Those groups are for the standalone version of VB, which
differs enough from VBA that all you'll get from those groups is a redirect
to a specific group, like microsoft.public.word.vba.general, which of course
you already posted to.
 
T

That Guy

Hi All

I'm using a VB app to interact with Ms Word.

I've given the user the ability to choose their own JPGs within my app, but
is there anyway I can automatically put these JPGs into MS Word via my
app/VBA??

Thanks

I had this same problem before.

Though showing the dialog to select the picture may be a vlaid answer
if you want to fire the insert one at a time but I wanted to add 4
pictures after selecting them.

Here is how I did it:

put this in a module:

Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

Public Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

and this on a form:

Private Sub cmdCancel_Click()
Unload Me
End Sub

Private Sub cmdOK_Click()
ActiveDocument.imgPicture1.Picture = imgFirst.Picture
ActiveDocument.imgPicture2.Picture = imgSecond.Picture
ActiveDocument.imgPicture3.Picture = imgThird.Picture
ActiveDocument.imgPicture4.Picture = imgFourth.Picture

Unload Me
End Sub

Private Sub imgFirst_Click()

Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String

OpenFile.lStructSize = Len(OpenFile)
sFilter = "Image Files(*.BMP;*.JPG;*.GIF)" & Chr(0) &
"*.BMP;*.JPG;*.GIF" & Chr(0)
'sFilter = "Batch Files (*.bat)" & Chr(0) & "*.BAT" & Chr(0)
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = Left(ActiveDocument.FullName, InStrRev
(ActiveDocument.FullName, "\"))
OpenFile.lpstrTitle = "Please Select The Picture To Add"
OpenFile.flags = 0
lReturn = GetOpenFileName(OpenFile)
If lReturn <> 0 Then
imgFirst.Picture = LoadPicture(OpenFile.lpstrFile)
Me.Repaint
End If

End Sub

Private Sub imgFirst_MouseDown(ByVal Button As Integer, ByVal Shift As
Integer, ByVal x As Single, ByVal Y As Single)
Dim Result As Long
If Button = 2 Then
Result = MsgBox("Clear the Picture?", vbYesNo, "Clear the
Picture?")
If Result = vbYes Then
imgFirst.Picture = Nothing
Me.Repaint
End If
End If
End Sub

The above piece of code deals with a form that has four image boxes an
ok button and a cancel button on it. When the user clicks a box with
the left button it shows an open dialog to open the picture, then puts
the opened picture into the image box. A right click on an image box
will give the user the chance to clear the picture.

When the OK button is pressed I put each of the four pictures into
previously placed image boxes in the document that i placed and sized
the way that I wanted them.

If you set the image box on the document to stretch then the picture
will scale to fit no matter what size. Only you can determine a good
size for the picture you are using.

Hope that helps
 

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