Insert Field - Please Help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I have created a form (in a table format) and I am looking to insert a field
that generates a random work order/invoice number. Can you please direct me
to where I can find the solution for this particular question. Thanks in
advance.

Ed
 
Ed,

You could set the value of the formfield by running a macro on entry.
Something like:

Sub RandomNumGen()
Dim randomNum
Randomize
randomNum = Int((5000 * Rnd) + 1) 'Generate 'random value between 1
and 5000.
ActiveDocument.FormFields("Text1").Result = randomNum
End Sub

Are you sure you want to use random numbers for your invoices? There
is nothing in the method above to prevent "duplicate" invoices numbers
from being generated. Wouldn't sequentially numbered invoices be more
appropriate? If so, see:
http://word.mvps.org/FAQs/MacrosVBA/NumberDocs.htm
 
Greg...thank you!
Your response and suggestion is appreciated.

I am stumped regarding the procedures outlined in the document called
"Creating sequentially numbered documents (such as invoices)"

I've inserted a bookmark {Order} in the invoice form (as per the
instructions - to add a bookmark). However, I am getting an error when I
re-open the template form.

Sub AutoNew()

Order = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Order")

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

System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _
"Order") = Order

ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "00#")
ActiveDocument.SaveAs FileName:="path" & Format(Order, "00#")

End Sub

Again, your help is appreciated.

Ed
 
Ed,

Can you describe the error? If you have the option explicit statement
in your VB Editor then the error could be cause by the not having the
varialbe "Order" declared.

Try adding:
Dim Order as String
immediately after the Sub AutoNew() line

Remember you are creating a Template. So when you go to create a new
invoice you don't open the template file instead you create a new
document based on the template.

HTH
 
Hi Greg,

Thanks again for the follow up...
When I added Dim Order As String and run the code, I am getting the
following error:

Run-time error '5941':
The requested member of the collection does not exist."


Sub AutoNew()
Dim Order As String
Order = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Order")

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

System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _
"Order") = Order

ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "00#")
ActiveDocument.SaveAs FileName:="path" & Format(Order, "00#")


End Sub


Again your help is appreciated.

Ed
 
And a follow up to the document error:

The document (that contains the field "Order") has the following message:

Error! Bookmark not defined.Error! Bookmark not defined.Error! Bookmark not
defined.
 
Ed,

I don't mean to sound blunt. You must be doing someting wrong.

To get you on track lets start from scrath.

Copy the AutoNew macro from the website to your clipboard.
Open a new template File>New>Create new template
Open the VB editor and paste the macro code in the new templateproject
Remove the option explicit statement for now.
Now in the new blank template type in - Number:
Then Insert>Bookmark and add a bookmark named Order
File Save and save the template in your template directory as Test

Now File>New and create a new document based on the test template.

As new file should open with the text Number: 001
 
Greg...being blunt in this case WORKS!!! (*smiles*)
You ROCK dude!!! A thousand thanks.

Ed
 
Back
Top