PDA

View Full Version : inserting images stored in userform into document bookmark using checkbox



sarveshspace
10-25-2018, 10:23 AM
Hello,
23084
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?

gmaxey
10-25-2018, 02:21 PM
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

sarveshspace
10-25-2018, 03:06 PM
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?

gmaxey
10-25-2018, 04:20 PM
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.

gmayor
10-25-2018, 08:11 PM
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

sarveshspace
10-26-2018, 10:06 AM
Dear sir ,
thank you so much! it worked!
:)

gmaxey
10-26-2018, 10:13 AM
Graham,

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