B
Billy B
I have a Word document with a single table containing four InsertPicture
Macro fields and four form fields each with its own bookmark name (Pic1, ID1,
Pic2, ID2, Pic3, ID3, etc) The code gets the name of the bookmark, processes
the insert picture macro then I want to go to the next highest ID number. The
problem I have is the insertpicture procedure is exited and it doesn't hold
the intCounter value to be used the next time the procedure is run. Of course
it works the first time through then not again. Can this be done? Below is
the code.
Public Sub FormInsertPicture()
Dim FName As String, LocName As String, BtnName As String
Dim PicRg As Range
Dim Photo As InlineShape
Dim RatioW As Single, RatioH As Single, RatioUse As Single
Dim PhotoNum As Integer
Dim NextDOC As String
Dim intCount as integer
'set the first picture location as 1
intCount = intCount +1
' IMPORTANT: set the following numbers to the desired maximum
' size (in inches) of the picture after insertion
' When you click a MacroButton, the Selection is
' the text of the button. It should be enclosed in
' a bookmark whose name is "PicButton" plus a number.
' This code gets that name and replaces "PicButton"
' with "PicLocation" to get the location bookmark
' name with the same number.
If Selection.Bookmarks.Count < 1 Then GoTo BadBtn
BtnName = Selection.Bookmarks(1).Name
If LCase(Left$(BtnName, 9)) <> "picbutton" Then GoTo BadBtn
LocName = Replace(BtnName, "PicButton", "PicLocation")
With ActiveDocument
If Not .Bookmarks.Exists(LocName) Then GoTo BadRange
If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=FormPassword
End If
' standard Insert > Picture > From File dialog
' [Note: If you try to call the dialog before you
' unprotect the document, it doesn't work.]
With Dialogs(wdDialogInsertPicture)
If .Display <> -1 Then GoTo Canceled
' get chosen file's full path and file name
FName = WordBasic.FileNameInfo$(.Name, 1)
End With
' create range for picture location
Set PicRg = .Bookmarks(LocName).Range
' if a picture is already there, delete it
Do While PicRg.InlineShapes.Count > 0
PicRg.InlineShapes(1).Delete
Loop
' insert the picture
Set Photo = .InlineShapes.AddPicture(FileName:=FName, _
LinkToFile:=False, SaveWithDocument:=True, _
Range:=PicRg)
With Photo
RatioW = CSng(InchesToPoints(PicWidth)) / .Width
RatioH = CSng(InchesToPoints(PicHeight)) / .Height
' choose the smaller ratio
If RatioW < RatioH Then
RatioUse = RatioW
Else
RatioUse = RatioH
End If
' size the picture to fit the cell
.Height = .Height * RatioUse
.Width = .Width * RatioUse
End With
' re-add the bookmark so it can be reused
.Bookmarks.Add Name:=LocName, Range:=Photo.Range
NextID = "ID" & intCount & ""
ActiveDocument.Bookmarks(NextID).Select
intCount = intCount + 1
'Goto the next form field (ie ID2, ID3)
ActiveDocument.Bookmarks(LocName).Delete
Canceled:
.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:=FormPassword
End With
Exit Sub
BadBtn:
MsgBox "The MacroButton field's bookmark is missing or incorrect", _
, "Error"
Exit Sub
BadRange:
MsgBox "The picture location bookmark is missing or incorrect", _
, "Error"
End Sub
Macro fields and four form fields each with its own bookmark name (Pic1, ID1,
Pic2, ID2, Pic3, ID3, etc) The code gets the name of the bookmark, processes
the insert picture macro then I want to go to the next highest ID number. The
problem I have is the insertpicture procedure is exited and it doesn't hold
the intCounter value to be used the next time the procedure is run. Of course
it works the first time through then not again. Can this be done? Below is
the code.
Public Sub FormInsertPicture()
Dim FName As String, LocName As String, BtnName As String
Dim PicRg As Range
Dim Photo As InlineShape
Dim RatioW As Single, RatioH As Single, RatioUse As Single
Dim PhotoNum As Integer
Dim NextDOC As String
Dim intCount as integer
'set the first picture location as 1
intCount = intCount +1
' IMPORTANT: set the following numbers to the desired maximum
' size (in inches) of the picture after insertion
' When you click a MacroButton, the Selection is
' the text of the button. It should be enclosed in
' a bookmark whose name is "PicButton" plus a number.
' This code gets that name and replaces "PicButton"
' with "PicLocation" to get the location bookmark
' name with the same number.
If Selection.Bookmarks.Count < 1 Then GoTo BadBtn
BtnName = Selection.Bookmarks(1).Name
If LCase(Left$(BtnName, 9)) <> "picbutton" Then GoTo BadBtn
LocName = Replace(BtnName, "PicButton", "PicLocation")
With ActiveDocument
If Not .Bookmarks.Exists(LocName) Then GoTo BadRange
If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=FormPassword
End If
' standard Insert > Picture > From File dialog
' [Note: If you try to call the dialog before you
' unprotect the document, it doesn't work.]
With Dialogs(wdDialogInsertPicture)
If .Display <> -1 Then GoTo Canceled
' get chosen file's full path and file name
FName = WordBasic.FileNameInfo$(.Name, 1)
End With
' create range for picture location
Set PicRg = .Bookmarks(LocName).Range
' if a picture is already there, delete it
Do While PicRg.InlineShapes.Count > 0
PicRg.InlineShapes(1).Delete
Loop
' insert the picture
Set Photo = .InlineShapes.AddPicture(FileName:=FName, _
LinkToFile:=False, SaveWithDocument:=True, _
Range:=PicRg)
With Photo
RatioW = CSng(InchesToPoints(PicWidth)) / .Width
RatioH = CSng(InchesToPoints(PicHeight)) / .Height
' choose the smaller ratio
If RatioW < RatioH Then
RatioUse = RatioW
Else
RatioUse = RatioH
End If
' size the picture to fit the cell
.Height = .Height * RatioUse
.Width = .Width * RatioUse
End With
' re-add the bookmark so it can be reused
.Bookmarks.Add Name:=LocName, Range:=Photo.Range
NextID = "ID" & intCount & ""
ActiveDocument.Bookmarks(NextID).Select
intCount = intCount + 1
'Goto the next form field (ie ID2, ID3)
ActiveDocument.Bookmarks(LocName).Delete
Canceled:
.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:=FormPassword
End With
Exit Sub
BadBtn:
MsgBox "The MacroButton field's bookmark is missing or incorrect", _
, "Error"
Exit Sub
BadRange:
MsgBox "The picture location bookmark is missing or incorrect", _
, "Error"
End Sub