Results 1 to 4 of 4
  1. #1
    Lounger
    Join Date
    Nov 2001
    Location
    Calgary, Alberta, Canada
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Incrementing a document number (2003)

    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(wdProper tyKeywords).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,
    Attached Files Attached Files

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Incrementing a document number (2003)

    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. <img src=/S/wink.gif border=0 alt=wink width=15 height=15> 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.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Incrementing a document number (2003)

    To save the document in the same folder as the template:
    <code>
    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")
    </code>
    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(wdPropertyC omments).Value
    lNext = lCurrent + 1
    ThisDocument.BuiltInDocumentProperties(wdPropertyC omments).Value = lNext
    ThisDocument.Fields.Update
    ThisDocument.Save
    ' Unlink document from template
    ActiveDocument.AttachedTemplate = "Normal"
    ActiveDocument.BuiltInDocumentProperties(wdPropert yComments).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.

  4. #4
    Lounger
    Join Date
    Nov 2001
    Location
    Calgary, Alberta, Canada
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Incrementing a document number (2003)

    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,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •