• Incrementing a document number (2003)

    Author
    Topic
    #435955

    I have been asked whether it is possible to build in an incrementing number into a Work Permit form that has been designed by somebody else.

    I have decided to turn the file into a template (attached as a .doc) and use the file properties to hold the last issued number which I am incrementing whenever a new file is produced from the template. This approach is currently working well apart from 2 issues I would appreciate some help with:
    1. I would like the document created to be saved in the same directory as the template. The ChDir statement I am currently using is not working.
    2. At the moment if I try to create a new document from an existing document the Document_New event in the template is triggered. Can I stop this becuase I only want documents created by the template itself to increment the number, I have tried using “ActiveDocument.BuiltInDocumentProperties(wdPropertyKeywords).Value” as a test but this doesn’t work.

    This is my first ever attempt at writing VBA for Word. As such I expect my problem my lie in the lack of knowledge I have on the object model.

    Any and all help is appreciated,

    Viewing 2 reply threads
    Author
    Replies
    • #1032105

      These are some old threads that might help in general: numbering invoices (Word 2000); VBA (Word 2000).

      1. Try ChangeFileOpenDirectory. If that doesn’t work, you might need to hijack the FileSave and FileSaveAs commands and assign the “name” of the folder in the process of displaying the dialog.

      2. I don’t understand why Document_New runs. Is the user treating the document as a template, or is the code being copied into the document?? One workaround might be to disconnect your custom template once it is no longer needed. That can be difficult to ascertain, unfortunately.

      I don’t understand your reference to keywords. How about creating a custom property of your own when you generate a new document?

      Added: I should have opened your document first. wink I think it would be safer to store your values in custom properties that users don’t ordinarily notice. Unfortunately, there is a chance that a metadata cleaner will at some point cleanse all of the document properties, document variables, etc., so perhaps in the course of creating the document you should “unlink” the permit number field code so that it is converted to unchanging text.

    • #1032106

      To save the document in the same folder as the template:

      Dim strPath As String
      strPath = ActiveDocument.AttachedTemplate.Path
      If Not Right(strPath, 1) = "" Then
      strPath = strPath & ""
      End If

      ...

      ActiveDocument.SaveAs FileName:=strPath & "WP" & Format(lNext, "00000")

      If you create a new document from an existing document, the new document will be based on the same template as the existing document. So you should break the link between the document and the template. For example:

      Private Sub Document_New()
      Dim lCurrent As Long
      Dim lNext As Long
      Dim strPath As String
      strPath = ActiveDocument.AttachedTemplate.Path
      If Not Right(strPath, 1) = “” Then
      strPath = strPath & “”
      End If
      lCurrent = ThisDocument.BuiltInDocumentProperties(wdPropertyComments).Value
      lNext = lCurrent + 1
      ThisDocument.BuiltInDocumentProperties(wdPropertyComments).Value = lNext
      ThisDocument.Fields.Update
      ThisDocument.Save
      ‘ Unlink document from template
      ActiveDocument.AttachedTemplate = “Normal”
      ActiveDocument.BuiltInDocumentProperties(wdPropertyComments).Value = lNext
      ActiveDocument.Fields.Update
      ActiveDocument.SaveAs FileName:=strPath & “WP” & Format(lNext, “00000”)
      End Sub

      BTW I don’t really like the idea of saving the template each time. I think I’d store the number in a text file or in a database.

    • #1032118

      Jefferson/Hans,

      Thanks for your help.

      Hans I was in the middle of replying to Jefferson to ask how I broke the link to the custom template I was trying “” instead of “Normal” when I saw your reply that cleared the muddy water for me.

      To answer some of the points you raised:
      The users of the template have very basic PC skills so I wanted a solution that didn’t rely on any external source for the numbering and also on that would allow them to reset or change the number if absolutely necessary. This was also part of my decision to use the built-in properties as was my inexperience in VBA for Word. Finally, as far as I know I also do not need to worry about metadata as the electronic file doesn’t leave the company.

      Thanks again for your help,

    Viewing 2 reply threads
    Reply To: Incrementing a document number (2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: