Results 1 to 12 of 12
  1. #1
    2 Star Lounger
    Join Date
    Jul 2005
    Location
    North Carolina, USA
    Posts
    195
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Command to insert image on a control (2003)

    Hi, Trying to get the help of professionals. I have a word doc. with two table, I have an image control box which i will like to click on it and browse for an image to be inserted. I was trying modify some vba code that Hans provided for an access thread but I can not make it work. The reason behind the image control is to prevent an image for been to large and disrupt the format of the whole document. Any suggestions.

    Private Sub Image1_Click()
    Dim OFN As OPENFILENAME
    Dim PicFile As Image
    On Error GoTo Err_Image1_Click

    ' Set options for dialog box.
    With OFN
    .flags = &H1804 ' OFN_FileMustExist + OFN_PathMustExist + OFN_HideReadOnly
    .lpstrFilter = MakeFilterString("Image files (*.bmp;*.gif;*.jpg;*.wmf)", "*.bmp;*.gif;*.jpg;*.wmf", _
    "All files (*.*)", "*.*")
    End With

    If OpenDialog(OFN) Then
    PicFile = OFN.lpstrFile
    Image1.Picture = PicFile

    End If
    Exit Sub

    Err_Image1_Click:
    MsgBox Err.Description, vbExclamation
    End Sub

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Command to insert image on a control (2003)

    What kind of object is your image control. Can you post a stripped down version of your document?

  3. #3
    2 Star Lounger
    Join Date
    Jul 2005
    Location
    North Carolina, USA
    Posts
    195
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Command to insert image on a control (2003)

    Here is a copy of the portion i am working on.
    Attached Files Attached Files

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Command to insert image on a control (2003)

    As far as I can tell, you don't need the window handle; try commenting that line out.

    In your event handler, try using LoadPicture to read the image file into the control:

    <code>Private Sub Image1_Click()
    Dim OFN As OPENFILENAME
    On Error GoTo Err_Image1_Click

    ' Set options for dialog box.
    With OFN
    .flags = &H1804 ' OFN_FileMustExist + OFN_PathMustExist + OFN_HideReadOnly
    .lpstrFilter = MakeFilterString("Image files (*.bmp;*.gif;*.jpg;*.wmf)", "*.bmp;*.gif;*.jpg;*.wmf", _
    "All files (*.*)", "*.*")
    End With

    If OpenDialog(OFN) Then
    Image1.Picture = LoadPicture(OFN.lpstrFile)
    End If
    Exit Sub

    Err_Image1_Click:
    MsgBox Err.Description, vbExclamation
    End Sub</code>

    I don't actually see the image until I use Print Preview to repaint the display. If there is another way to do it, obviously that would be preferable. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Command to insert image on a control (2003)

    As Jefferson noted, you must print preview the document before the new picture is displayed. The following version adds that to the code:

    Private Sub Image1_Click()
    Dim OFN As OPENFILENAME
    On Error GoTo Err_Image1_Click

    ' Set options for dialog box.
    With OFN
    .flags = &H1804 ' OFN_FileMustExist + OFN_PathMustExist + OFN_HideReadOnly
    .lpstrFilter = MakeFilterString("Image files (*.bmp;*.gif;*.jpg;*.wmf)", _
    "*.bmp;*.gif;*.jpg;*.wmf", "All files (*.*)", "*.*")
    End With

    If OpenDialog(OFN) Then
    Image1.Picture = LoadPicture(OFN.lpstrFile)
    Application.ScreenUpdating = False
    ActiveDocument.PrintPreview
    Application.PrintPreview = False
    Application.ScreenUpdating = True
    End If
    Exit Sub

    Err_Image1_Click:
    MsgBox Err.Description, vbExclamation
    End Sub

    And, also as noted, you must remove the line that sets the window handle - hWndAccessApp is not valid in Word.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Command to insert image on a control (2003)

    BTW, in Office 2002 and later, you don't need all this OpenDialog stuff, you can use the built-in FileDialog object. If you type FileDialog in the Visual Basic Editor and press F1, you'll get help. You can also find examples by searching here in the Lounge.

  7. #7
    2 Star Lounger
    Join Date
    Jul 2005
    Location
    North Carolina, USA
    Posts
    195
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Command to insert image on a control (2003)

    Hi, Sorry i took so long to respond. I tried all your subjections, for some reason when i exit the design mode, the control is not clickable. I am working with Hans last subjection (FileDialog) it is very easy but when i click on the image to be inserted into the control it does not remain. I try to preview but it does not show either. here is the sample.
    Attached Files Attached Files

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Command to insert image on a control (2003)

    Your code is only a small part of the code I posted. It won't do anything with the selected image file. Please use the code I posted.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Command to insert image on a control (2003)

    Here is a complete version of the code using FileDialog:

    Private Sub Image1_Click()
    Dim dlgOpen As FileDialog
    Set dlgOpen = Application.FileDialog( _
    FileDialogType:=msoFileDialogOpen)
    With dlgOpen
    .Filters.Clear
    .Filters.Add "All files", "*.*"
    If .Show = True Then
    Me.Image1.Picture = LoadPicture(.SelectedItems(1))
    Application.ScreenUpdating = False
    ActiveDocument.PrintPreview
    Application.PrintPreview = False
    Application.ScreenUpdating = True
    End If
    End With
    End Sub

  10. #10
    2 Star Lounger
    Join Date
    Jul 2005
    Location
    North Carolina, USA
    Posts
    195
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Command to insert image on a control (2003)

    That code is awesome Hans. It work perfect. Question how can I change the property of the image box to be able to adjust the size of the image to fit in the control.

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Command to insert image on a control (2003)

    Activate design mode (first button on the control toolbar).
    Right-click the picture and select Properties from the popup menu.
    Set the PictureSizeMode property to 3 - fmPictureSizeModeZoom
    You only have to do this once, so it's not necessary to use code for it, but you could use

    Me.Image1.PictureSizeMode = fmPictureSizeModeZoom

  12. #12
    2 Star Lounger
    Join Date
    Jul 2005
    Location
    North Carolina, USA
    Posts
    195
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Command to insert image on a control (2003)

    Thank again. You did it again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •