Picture to Ms Word via VBA?? 
Author Message
 Picture to Ms Word via VBA??

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



Wed, 01 Feb 2012 16:14:21 GMT  
 Picture to Ms Word via VBA??
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

Quote:
> 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



Thu, 02 Feb 2012 15:54:21 GMT  
 Picture to Ms Word via VBA??
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.


Fri, 03 Feb 2012 23:39:23 GMT  
 Picture to Ms Word via VBA??

Quote:
> 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



Sat, 04 Feb 2012 23:06:31 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. MS PJ 2000 and MS Word via VBA

2. Get Image/picture in MS Word using VBA (macro)

3. Print out MS Outlook calendars via VBA WITHIN MS Outlook

4. mail-merge MS Word and MS Access via ASP

5. how to control MS word via MS Basic script

6. MS Word VBA with MS Graph

7. MS Word VBA with MS Graph

8. How to insert pictures via VB in an MS Access Database

9. Linked picture - change path via VBA

10. Using MS-Word via Visual Basic?

11. Controling MS Word Via VB ..Help!

12. Exporting Access records to MS Word via VB6 front end app

 

 
Powered by phpBB® Forum Software