Help with logic and processing

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
 
D

Doug Robbins - Word MVP

You will need to store the value somewhere external to the procedure. A
document variable would do in this case. Replace

'set the first picture location as 1
intCount = intCount +1

with

If ActiveDocument.Variables("varintCount").Value="" then
intCount = 1
Else
intCount=ActiveDocument.Variables("varintCount").Value
End if

and replace the

intCount = intCount + 1

further down your code with

ActiveDocument.Variables("varintCount").Value = intCount + 1

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Billy B said:
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
 
B

Billy B

Doug,

I am not sure where to put the document variable statement. I tried putting
it in the Document AutoOpen and then in the ThisDocument General Declaration
section. I tried a simple Dim statement and that didn't work. Then I tried:
ActiveDocument.Variables.Add Name:="intCount", Value:="1" and still when I
get to the first intCount variable in the code I get a "variable not defined"
error.

Thanks for your help.

Doug Robbins - Word MVP said:
You will need to store the value somewhere external to the procedure. A
document variable would do in this case. Replace

'set the first picture location as 1
intCount = intCount +1

with

If ActiveDocument.Variables("varintCount").Value="" then
intCount = 1
Else
intCount=ActiveDocument.Variables("varintCount").Value
End if

and replace the

intCount = intCount + 1

further down your code with

ActiveDocument.Variables("varintCount").Value = intCount + 1

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Billy B said:
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
 
B

Billy B

Doug,

I got it figured out. I put it in the General Declarations of th module. I
am still confused as to how you declare a global variable to be made
available throughtout the document, tho.
Thanks again.

Billy B said:
Doug,

I am not sure where to put the document variable statement. I tried putting
it in the Document AutoOpen and then in the ThisDocument General Declaration
section. I tried a simple Dim statement and that didn't work. Then I tried:
ActiveDocument.Variables.Add Name:="intCount", Value:="1" and still when I
get to the first intCount variable in the code I get a "variable not defined"
error.

Thanks for your help.

Doug Robbins - Word MVP said:
You will need to store the value somewhere external to the procedure. A
document variable would do in this case. Replace

'set the first picture location as 1
intCount = intCount +1

with

If ActiveDocument.Variables("varintCount").Value="" then
intCount = 1
Else
intCount=ActiveDocument.Variables("varintCount").Value
End if

and replace the

intCount = intCount + 1

further down your code with

ActiveDocument.Variables("varintCount").Value = intCount + 1

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Billy B said:
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
 
D

Doug Robbins - Word MVP

The code that I gave you was intended to be inserted into your code in place
of the commands that I referenced.

The variable is not Global in the sense that it would apply across multiple
documents. It is unique to the one document and is stored in the document.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Billy B said:
Doug,

I got it figured out. I put it in the General Declarations of th module. I
am still confused as to how you declare a global variable to be made
available throughtout the document, tho.
Thanks again.

Billy B said:
Doug,

I am not sure where to put the document variable statement. I tried
putting
it in the Document AutoOpen and then in the ThisDocument General
Declaration
section. I tried a simple Dim statement and that didn't work. Then I
tried:
ActiveDocument.Variables.Add Name:="intCount", Value:="1" and still when
I
get to the first intCount variable in the code I get a "variable not
defined"
error.

Thanks for your help.

Doug Robbins - Word MVP said:
You will need to store the value somewhere external to the procedure.
A
document variable would do in this case. Replace

'set the first picture location as 1
intCount = intCount +1

with

If ActiveDocument.Variables("varintCount").Value="" then
intCount = 1
Else
intCount=ActiveDocument.Variables("varintCount").Value
End if

and replace the

intCount = intCount + 1

further down your code with

ActiveDocument.Variables("varintCount").Value = intCount + 1

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top