PDA

View Full Version : [SOLVED:] Form, Image, Bookmark



tjmck2
09-04-2017, 03:03 PM
My Dim Image1 As Range is wrong.... The code to upload the image to the form works. All the other codes work. I just can't get the image, once uploaded, to it's corresponding bookmark.

I'm attempting a simple VBA form in Microsoft Word where the UserForm1 pops up automatically, information is filled out in the UserForm1 (Name, Grade, Image of Student), then upon Submit the information goes to it's bookmarks.

Thank you in advance.



Private Sub CommandButton1_Click()
Dim FirstName As Range
Set FirstName = ActiveDocument.Bookmarks("FirstName").Range
FirstName.Text = Me.TextBox1.Value

Dim SurName As Range
Set SurName = ActiveDocument.Bookmarks("SurName").Range
SurName.Text = Me.TextBox2.Value

Dim Grade As Range
Set Grade = ActiveDocument.Bookmarks("Grade").Range
Grade.Text = Me.TextBox3.Value

Dim Image1 As Range
Set Image1 = ActiveDocument.Bookmarks("Image1").Range
Image1.Image1 = Me.Image1.Picture

Me.Repaint
UserForm1.Hide

End Sub

Private Sub CommandButton2_Click()

With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.ButtonName = "Submit"
.Title = "Select an image file"
.Filters.Add "Image", "*.gif; *.jpg; *.jpeg", 1
If .Show = -1 Then
Me.Image1.PictureSizeMode = fmPictureSizeModeZoom
Me.Image1.Picture = LoadPicture(.SelectedItems(1))
Else
End If
End With

gmayor
09-04-2017, 08:39 PM
Inserting an image from a userform is not as straightforward as you anticipate. The following additional function will do that. I have also added a function from my web site to write the textbox values to their bookmarks. In both cases you can run the form again and the functions will replace the bookmark values with the revised values from the userform.


Option Explicit

Private Sub CommandButton1_Click()
FillBM "FirstName", TextBox1.Text
FillBM "SurName", TextBox2.Text
FillBM "Grade", TextBox3.Text
UserFormImageToBM "Image1", Image1.Picture
Unload Me
End Sub

Private Sub CommandButton2_Click()
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.ButtonName = "Submit"
.Title = "Select an image file"
.Filters.Add "Image", "*.gif; *.jpg; *.jpeg", 1
If .Show = -1 Then
Me.Image1.PictureSizeMode = fmPictureSizeModeZoom
Me.Image1.Picture = LoadPicture(.SelectedItems(1))
Else
End If
End With
End Sub

Public Sub FillBM(strbmName As String, strValue As String)
'Graham Mayor - http://www.gmayor.com
Dim oRng As Range
With ActiveDocument
On Error GoTo lbl_Exit
Set oRng = .Bookmarks(strbmName).Range
oRng.Text = strValue
oRng.Bookmarks.Add strbmName
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub

Private Sub UserFormImageToBM(strbmName As String, oImage As Object)
'Graham Mayor - http://www.gmayor.com
'UserformImageToBM "bkSig", Image1.Picture
Dim oRng As Range
Dim TempFile As String
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
TempFile = Replace(fso.GetTempName, "tmp", "bmp")
SavePicture oImage, TempFile
With ActiveDocument
On Error GoTo lbl_Exit
Set oRng = .Bookmarks(strbmName).Range
oRng.Text = ""
oRng.InlineShapes.AddPicture _
FileName:=TempFile, LinkToFile:=False, _
SaveWithDocument:=True
oRng.End = oRng.End + 1
oRng.Bookmarks.Add strbmName
End With
Kill TempFile
lbl_Exit:
Set fso = Nothing
Set oRng = Nothing
Exit Sub
End Sub

tjmck2
09-04-2017, 09:11 PM
Hello Gmayor

Thanks that is perfect! Did the trick! I'll make it as solved.

However, Is there a way to automatically resize the image to fit the bookmarked table cell in Word?

gmayor
09-04-2017, 10:13 PM
If the process is putting the image into a table cell, put the bookmark IN the cell and make the table cell fixed width. The image will shrink to the width of the cell.

tjmck2
09-04-2017, 10:36 PM
Thanks again!!