Auto Number problem in form

R

Robin

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
 
Z

zkid

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
 
R

Robin

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
 
Z

zkid

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
 
T

Tony Jollans

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.
 

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