Hi All,
I have a word template form that we use for quotations. I have created a
macro to unlock, insert an auto number, save the document and relock the form
(works fine). However when i open the newly created doc the autonumber macro
runs again. Can anyone suggest how i can check to see if the document
contains a number already and if so do not run the macro? My code so far is:

Sub AutoOpen()
'
' AutoOpen Macro
'
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:="22747"
End If

'Create and insert the quotation number
Order = System.PrivateProfileString("S:\Public
Access\Quotations\Settings.Txt", _
"MacroSettings", "Order")

If Order = "" Then
Order = 1
Else
Order = Order + 1
End If

System.PrivateProfileString("S:\Public Access\Quotations\Settings.Txt",
"MacroSettings", _
"Order") = Order

ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "#")
ActiveDocument.SaveAs "S:\Public Access\Quotations\quotation_" &
Format(Order, "#")

'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="22747"

End If

Thanks for your help.
Rob

RE: Auto Number problem in form by zkid

zkid
Thu Dec 06 19:03:03 PST 2007

Okay, I think I understand what is happening here. The number isn't really
an auto number as much as it is a hard-coded incremented number based on
what's in the registry settings file. Correct?

If I understand your code correctly, the incremented number is placed
adjacent to the bookmark named "Order." So, you will need to go to the
bookmark and determine if the entry adjacent to it is a number.

Here's something to get you started:

ActiveDocument.Bookmarks("Order").Select
Selection.Collapse (wdCollapseEnd)

Select Case Asc(Selection.Text)
Case 48 To 57 'number between 0 and 9
'Maybe set another boolean to true that a number exists
bNumExists = True
Case Else
'Set the boolean to false
bNumExists = False
End Select

Then, place an if statement around the remainder of your code inserting the
number

"Robin" wrote:

> Hi All,
> I have a word template form that we use for quotations. I have created a
> macro to unlock, insert an auto number, save the document and relock the form
> (works fine). However when i open the newly created doc the autonumber macro
> runs again. Can anyone suggest how i can check to see if the document
> contains a number already and if so do not run the macro? My code so far is:
>
> Sub AutoOpen()
> '
> ' AutoOpen Macro
> '
> 'Unprotect the file
> If ActiveDocument.ProtectionType <> wdNoProtection Then
> bProtected = True
> ActiveDocument.Unprotect Password:="22747"
> End If
>
> 'Create and insert the quotation number
> Order = System.PrivateProfileString("S:\Public
> Access\Quotations\Settings.Txt", _
> "MacroSettings", "Order")
>
> If Order = "" Then
> Order = 1
> Else
> Order = Order + 1
> End If
>
> System.PrivateProfileString("S:\Public Access\Quotations\Settings.Txt",
> "MacroSettings", _
> "Order") = Order
>
> ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "#")
> ActiveDocument.SaveAs "S:\Public Access\Quotations\quotation_" &
> Format(Order, "#")
>
> 'Reprotect the document.
> If bProtected = True Then
> ActiveDocument.Protect _
> Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="22747"
>
> End If
>
> Thanks for your help.
> Rob
>

RE: Auto Number problem in form by Robin

Robin
Fri Dec 07 06:33:02 PST 2007

Yeh, that's correct. I have text file storing the last used number. I
tried your code and messed around with it but couldn't get it to work
properly. I haven't used Case before so i didn't really understand it,
especially Case 48 to 57...?

I tried to tackle this another way, If the active doc filename = "template
filename" run the code, if not don't run the code, but i could get the
correct syntax for accessing the filename. Is this a possible solution?

Any help is appreciated...
Robin

"zkid" wrote:

> Okay, I think I understand what is happening here. The number isn't really
> an auto number as much as it is a hard-coded incremented number based on
> what's in the registry settings file. Correct?
>
> If I understand your code correctly, the incremented number is placed
> adjacent to the bookmark named "Order." So, you will need to go to the
> bookmark and determine if the entry adjacent to it is a number.
>
> Here's something to get you started:
>
> ActiveDocument.Bookmarks("Order").Select
> Selection.Collapse (wdCollapseEnd)
>
> Select Case Asc(Selection.Text)
> Case 48 To 57 'number between 0 and 9
> 'Maybe set another boolean to true that a number exists
> bNumExists = True
> Case Else
> 'Set the boolean to false
> bNumExists = False
> End Select
>
> Then, place an if statement around the remainder of your code inserting the
> number
>
> "Robin" wrote:
>
> > Hi All,
> > I have a word template form that we use for quotations. I have created a
> > macro to unlock, insert an auto number, save the document and relock the form
> > (works fine). However when i open the newly created doc the autonumber macro
> > runs again. Can anyone suggest how i can check to see if the document
> > contains a number already and if so do not run the macro? My code so far is:
> >
> > Sub AutoOpen()
> > '
> > ' AutoOpen Macro
> > '
> > 'Unprotect the file
> > If ActiveDocument.ProtectionType <> wdNoProtection Then
> > bProtected = True
> > ActiveDocument.Unprotect Password:="22747"
> > End If
> >
> > 'Create and insert the quotation number
> > Order = System.PrivateProfileString("S:\Public
> > Access\Quotations\Settings.Txt", _
> > "MacroSettings", "Order")
> >
> > If Order = "" Then
> > Order = 1
> > Else
> > Order = Order + 1
> > End If
> >
> > System.PrivateProfileString("S:\Public Access\Quotations\Settings.Txt",
> > "MacroSettings", _
> > "Order") = Order
> >
> > ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "#")
> > ActiveDocument.SaveAs "S:\Public Access\Quotations\quotation_" &
> > Format(Order, "#")
> >
> > 'Reprotect the document.
> > If bProtected = True Then
> > ActiveDocument.Protect _
> > Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="22747"
> >
> > End If
> >
> > Thanks for your help.
> > Rob
> >

RE: Auto Number problem in form by zkid

zkid
Fri Dec 07 09:51:02 PST 2007

The code I provided does work. The problem might be how it copies from this
location. The case statement is an alternative to the if..then..else
statement and is a bit cleaner. 48 to 57 means if the ascii code for the
character at the cursor is between 48 and 57 (the ascii numbers for 0 to 9),
then it is a number and the doc is already numbered.

Try copying this code again and make sure the > symbols are deleted:

Dim bNumExists as Boolean

'Go to right of Order bookmark
ActiveDocument.Bookmarks("Order").Select
Selection.Collapse (wdCollapseEnd)

'Checks for number between 0 and 9 with ascii codes
Select Case Asc(Selection.Text)
Case 48 To 57
bNumExists = True
Case Else
bNumExists = False
End Select

â??Now, at this point, you need to place an if statement around
â??the rest of your code that inserts the number. Something like:

If bNumExists = False then â??Execute the rest of the code
â??The rest of your code here
End if



"Robin" wrote:

> Yeh, that's correct. I have text file storing the last used number. I
> tried your code and messed around with it but couldn't get it to work
> properly. I haven't used Case before so i didn't really understand it,
> especially Case 48 to 57...?
>
> I tried to tackle this another way, If the active doc filename = "template
> filename" run the code, if not don't run the code, but i could get the
> correct syntax for accessing the filename. Is this a possible solution?
>
> Any help is appreciated...
> Robin
>
> "zkid" wrote:
>
> > Okay, I think I understand what is happening here. The number isn't really
> > an auto number as much as it is a hard-coded incremented number based on
> > what's in the registry settings file. Correct?
> >
> > If I understand your code correctly, the incremented number is placed
> > adjacent to the bookmark named "Order." So, you will need to go to the
> > bookmark and determine if the entry adjacent to it is a number.
> >
> > Here's something to get you started:
> >
> > ActiveDocument.Bookmarks("Order").Select
> > Selection.Collapse (wdCollapseEnd)
> >
> > Select Case Asc(Selection.Text)
> > Case 48 To 57 'number between 0 and 9
> > 'Maybe set another boolean to true that a number exists
> > bNumExists = True
> > Case Else
> > 'Set the boolean to false
> > bNumExists = False
> > End Select
> >
> > Then, place an if statement around the remainder of your code inserting the
> > number
> >
> > "Robin" wrote:
> >
> > > Hi All,
> > > I have a word template form that we use for quotations. I have created a
> > > macro to unlock, insert an auto number, save the document and relock the form
> > > (works fine). However when i open the newly created doc the autonumber macro
> > > runs again. Can anyone suggest how i can check to see if the document
> > > contains a number already and if so do not run the macro? My code so far is:
> > >
> > > Sub AutoOpen()
> > > '
> > > ' AutoOpen Macro
> > > '
> > > 'Unprotect the file
> > > If ActiveDocument.ProtectionType <> wdNoProtection Then
> > > bProtected = True
> > > ActiveDocument.Unprotect Password:="22747"
> > > End If
> > >
> > > 'Create and insert the quotation number
> > > Order = System.PrivateProfileString("S:\Public
> > > Access\Quotations\Settings.Txt", _
> > > "MacroSettings", "Order")
> > >
> > > If Order = "" Then
> > > Order = 1
> > > Else
> > > Order = Order + 1
> > > End If
> > >
> > > System.PrivateProfileString("S:\Public Access\Quotations\Settings.Txt",
> > > "MacroSettings", _
> > > "Order") = Order
> > >
> > > ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "#")
> > > ActiveDocument.SaveAs "S:\Public Access\Quotations\quotation_" &
> > > Format(Order, "#")
> > >
> > > 'Reprotect the document.
> > > If bProtected = True Then
> > > ActiveDocument.Protect _
> > > Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="22747"
> > >
> > > End If
> > >
> > > Thanks for your help.
> > > Rob
> > >

Re: Auto Number problem in form by Tony

Tony
Mon Dec 10 10:11:39 PST 2007

If you use an AutoNew macro instead of an AutoOpen then it will only run
when a new document is created rather than every time a document (based on
the template) is opened.

--
Enjoy,
Tony

"Robin" <Robin@discussions.microsoft.com> wrote in message
news:40BD7C5A-8A9E-4945-A096-CFB7CD8F9FDA@microsoft.com...
> Hi All,
> I have a word template form that we use for quotations. I have created a
> macro to unlock, insert an auto number, save the document and relock the
> form
> (works fine). However when i open the newly created doc the autonumber
> macro
> runs again. Can anyone suggest how i can check to see if the document
> contains a number already and if so do not run the macro? My code so far
> is:
>
> Sub AutoOpen()
> '
> ' AutoOpen Macro
> '
> 'Unprotect the file
> If ActiveDocument.ProtectionType <> wdNoProtection Then
> bProtected = True
> ActiveDocument.Unprotect Password:="22747"
> End If
>
> 'Create and insert the quotation number
> Order = System.PrivateProfileString("S:\Public
> Access\Quotations\Settings.Txt", _
> "MacroSettings", "Order")
>
> If Order = "" Then
> Order = 1
> Else
> Order = Order + 1
> End If
>
> System.PrivateProfileString("S:\Public Access\Quotations\Settings.Txt",
> "MacroSettings", _
> "Order") = Order
>
> ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "#")
> ActiveDocument.SaveAs "S:\Public Access\Quotations\quotation_" &
> Format(Order, "#")
>
> 'Reprotect the document.
> If bProtected = True Then
> ActiveDocument.Protect _
> Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="22747"
>
> End If
>
> Thanks for your help.
> Rob
>