Consulting

Results 1 to 7 of 7

Thread: inserting images stored in userform into document bookmark using checkbox

  1. #1

    inserting images stored in userform into document bookmark using checkbox

    Hello,
    pic.jpg
    I want to insert an image which is stored in my userform to my document based on whether i select the checkbox or not.

    I have attached an image which will give an idea of what i want to do .
    If i select checkbox 16 , i want the product orientation pic to go to a bookmark in my document. If i select checkbox 17 , then i want the other pic to go to the same bookmark.

    can this be done?

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,866
    Location
    I would use options buttons instead of check boxes. This avoids someone checking both boxes.

    Not very elegant, but I don't know of a way to actually get the picture out of the control otherwise:

    Private Sub CommandButton1_Click()
    Dim oRng As Range
    Dim oILS As InlineShape
      Hide
      Select Case True
        Case optA: SavePicture Image1.Picture, "C:\TempPic.bmp"
        Case optB: SavePicture Image2.Picture, "C:\TempPic.bmp"
        Case Else
         Exit Sub
      End Select
      Set oRng = ActiveDocument.Bookmarks("Picture").Range
      oRng.Delete
      Set oILS = oRng.InlineShapes.AddPicture(FileName:="C:\TempPic.bmp", _
           LinkToFile:=False, SaveWithDocument:=True)
      Set oRng = oILS.Range
      ActiveDocument.Bookmarks.Add "Picture", oRng
      oILS.Width = Image1.Width
      oILS.Height = Image1.Height
      On Error Resume Next
      Kill "C:\TempPic.bmp"
      On Error GoTo 0
      Set oRng = Nothing
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Quote Originally Posted by gmaxey View Post
    I would use options buttons instead of check boxes. This avoids someone checking both boxes.

    Not very elegant, but I don't know of a way to actually get the picture out of the control otherwise:

    Private Sub CommandButton1_Click()
    Dim oRng As Range
    Dim oILS As InlineShape
      Hide
      Select Case True
        Case optA: SavePicture Image1.Picture, "C:\TempPic.bmp"
        Case optB: SavePicture Image2.Picture, "C:\TempPic.bmp"
        Case Else
         Exit Sub
      End Select
      Set oRng = ActiveDocument.Bookmarks("Picture").Range
      oRng.Delete
      Set oILS = oRng.InlineShapes.AddPicture(FileName:="C:\TempPic.bmp", _
           LinkToFile:=False, SaveWithDocument:=True)
      Set oRng = oILS.Range
      ActiveDocument.Bookmarks.Add "Picture", oRng
      oILS.Width = Image1.Width
      oILS.Height = Image1.Height
      On Error Resume Next
      Kill "C:\TempPic.bmp"
      On Error GoTo 0
      Set oRng = Nothing
    lbl_Exit:
      Exit Sub
    End Sub

    So i created two option buttons , opta and optb.
    when i select the first image, i get error 75
    path/file access error .
    Its at the following line SavePicture Image1.Picture, "C:\TempPic.bmp"

    what do i do with the"c:\temppic.bmp " exactly?

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,866
    Location
    Well you don't actually do anything with it other than what the code is already trying to do. The code first tries (and succeeds here) to save the picture defined by the control titled Image1 or Image as a file on the C drive named TempPic.bmp. It then inserts an inlineshape and uses the file C:\TempPic.bmp to defined the shape. It then deletes the temporary picture file.

    Do you have write permission to your C drive? You might try changing the C to some other drive letter that you can save files to.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Recent Windows versions don't provide automatic write capabaility to the root of the C drive so an alternative path would be preferable, I would suggests the users Temp path e,g,
    Private Sub CommandButton1_Click()
    Dim oRng As Range
    Dim oILS As InlineShape
    Dim sPath As String
        sPath = Environ("TEMP") & "\TempPic.bmp"
        Hide
        Select Case True
            Case optA: SavePicture Image1.Picture, sPath
            Case optB: SavePicture Image2.Picture, sPath
            Case Else
                Exit Sub
        End Select
        Set oRng = ActiveDocument.Bookmarks("Picture").Range
        oRng.Delete
        Set oILS = oRng.InlineShapes.AddPicture(FileName:=sPath, _
                                                LinkToFile:=False, SaveWithDocument:=True)
        Set oRng = oILS.Range
        ActiveDocument.Bookmarks.Add "Picture", oRng
        oILS.Width = Image1.Width
        oILS.Height = Image1.Height
        On Error Resume Next
        Kill sPath
        On Error GoTo 0
    lbl_Exit:
        Set oRng = Nothing
        Set oILS = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    Dear sir ,
    thank you so much! it worked!

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,866
    Location
    Graham,

    Thanks. I suppose I should catch up with the times and upgrade to Windows 10 :-(
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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