Need field in form that increases serial number with each new docu

G

Guest

I need to set up a trouble-ticket form, with numerous blank fields. One
field must hold a serial number that increments by one each time a new, blank
trouble ticket is created.

I'm guessing that I might use a template with a field that retains the last
serial number, but increments it by one when a document is created from the
template. Then I would have a clean, blank document, with blank fields,
except for the serial number field.

I've used some fields before, but I don't know how to do this. The SEQ
field won't work for this. Any ideas?

Lee Techwriter
 
G

Guest

Hello Jay,

Pardon me for addressing you personally, but I can't find a way to post a
message to the general site, even though I did it last time.

The code you referred me to worked beautifully. The serial number
incremented by one each time I made a new doc from the template.
However...the document is a form. I need to protect it, so users can only
enter information in fields.

I can't depend on users to open the forms toolbar and select the little lock
to protect the document. Some people will know little about Word.

When I protect the template, and create a new document from it, I get the
following error message: "Run-tiime error '4605': This method or property is
not available because the object refers to a protected area of the document."

Here's the code. I modified it only to change the name of the variable to
"TicketNumber," and the format of the number. Everything works, except the
protection problem.

Sub Autonew()
'
' Autonew Macro
' Macro created 3/30/2007 by Lee Techwriter
'
TicketNumber = System.PrivateProfileString("C:\Settings.txt",
"MacroSettings", "TicketNumber")
'
If TicketNumber = "" Then
TicketNumber = 1001
Else
TicketNumber = TicketNumber + 1
End If
'
System.PrivateProfileString("C:\Settings.Txt", "MacroSettings",
"TicketNumber") = TicketNumber
'
ActiveDocument.Bookmarks("TicketNumber").Range.InsertBefore
Format(TicketNumber, "######")

ActiveDocument.SaveAs FileName:="Ticket-" & Format(TicketNumber, "######")
'
End Sub

If you don't want to fool with this, please pass it to the general community.

Thanks,
--Lee Techwriter
 
J

Jay Freedman

No problem.

The error occurs because the
ActiveDocument.Bookmarks("TicketNumber").Range.InsertBefore statement is
trying to "type" the number into the protected part of the text. The
solution is to unprotect the document, insert the number, and reprotect the
document. This all happens so fast that the user has no chance to change
anything.

Modify the macro, starting with the
ActiveDocument.Bookmarks("TicketNumber").Range.InsertBefore statement, to
this (beware of bad line breaks introduced by posting):

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

ActiveDocument.Bookmarks("TicketNumber").Range.InsertBefore
Format(TicketNumber, "######")

ActiveDocument.Protect Type:=wdAllowOnlyFormFields

ActiveDocument.SaveAs FileName:="Ticket-" & Format(TicketNumber, "######")
'
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
G

Guest

Jay this is terrific. Does exactly what I need. But it's saving the new
documents to the Templates folder. Can I get it to save the new doc to a
folder under My Documents > MysteryFolder? I looked through VBA help, but
couldn't get a fix on this.

If I ever meet you in real life, I owe you dinner in (almost) any restaurant
you choose. Is that a bribe? Maybe so. I don't care.

-- Lee Techwriter
 
J

Jay Freedman

I don't know why it would be saving to your Templates folder, unless
your most recent previous save left the current folder pointing there.

You can be certain of the folder the file will go to by including the
entire path in the ActiveDocument.SaveAs statement. I have no way of
knowing the exact path on your machine, but it will be something like

ActiveDocument.SaveAs FileName:="C:\Documents and Settings\Lee\My
Documents\MysteryFolder\Ticket-" & Format(TicketNumber, "######")

(That should all be on one line; if you copy and paste from here,
you'll have to delete the carriage return from the end of the first
line.)

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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