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