Results 1 to 5 of 5
  1. #1
    Karon Purcell
    Guest

    IE # which auto changes each time Word is opened

    I have a document in Word with an invoice #. I would like the invoice # to change automatically each time the document is opened. Is this possible? I am not a guru so I am hoping for a rather simple solution.
    Thanks in advance

  2. #2
    BAM
    Guest

    Re: IE # which auto changes each time Word is opened

    Hi Karon:

    This article from the Microsoft Word MVP FAQ site provides the steps to obtain the result you are looking for:
    http://www.mvps.org/word/FAQs/MacrosVBA/NumberDocs.htm

    Note that it was designed to for new documents based on a specific template. I noticed that you indicated that you want a Word document to automatically increment an Invoice #.

    If you are using a Word document to create an Invoice and then using File/Save As to create a new document then you would want to use a template instead. This article covers the concepts of templates and the steps on how to create one:
    http://www.mvps.org/word/FAQs/Customizatio...mplatePart1.htm
    ~~~~~~~~~
    Cheers!

  3. #3
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: IE # which auto changes each time Word is opened

    Hi Karen:

    Here is a possible answer that I copied from a WordTips Newsletter, written by Allen Wyatt. You can find more information about the weekly newsletter at

    <A target="_blank" HREF=http://www.VitalNews.com>http://www.VitalNews.com</A>

    Here is the tip:

    Macro: Auto-incrementing Form Fields
    by Allen Wyatt: WordTips 11/28/98
    When you are working with forms, you save the forms as templates that are used as the basis of new documents in Word. There may be times when you want to add a form
    field that increments every time you create a new document based on your form template.

    For instance, you might have a sequence number that you need for a data input form, or you may want to use a form field to indicate an invoice number.

    Word does not have any fancy auto-increment fields. However, by combining some of the tools already available in Word, you can create a template and form field to do the trick for you. If you are using Word 6 or Word 95, you can use the following WordBasic macro to do the trick:

    <pre>Sub MAIN
    ' Unprotect the form so the AutoText entry can be changed
    ToolsUnprotectDocument
    ' Retrieve and increment the CurIncField AutoText entry
    TempNum = Val(GetAutoText$("CurIncField", 1)) + 1
    ' Convert the number to a string variable
    Temp$ = LTrim$(Str$(TempNum))
    ' Set the contents of the CurIncField AutoText entry
    SetAutoText "CurIncField", Temp$, 1
    ' Set the contents of the form field
    SetFormResult "IncField", Temp$
    ' Protect the document
    ToolsProtectDocument .Type = 2, .NoReset = 1
    ' Save the template
    SaveTemplate
    End Sub</pre>


    This macro should be saved only in the template that you will use to create your forms. The macro must be named AutoNew, so it will run whenever a document is created
    that is based on the form template. You should also make sure that you have a text form field in your document named IncField, which will display the incremental number. The number is tracked in the template in the AutoText entry named CurIncField.

    You should also note that this macro assumes that you have not used a password to protect the template for forms. If you have, you will need to either remove the password or change the macro so that it accommodates the password in both the ToolsUnprotectDocument statement and the ToolsProtectDocument statement. (You can find out the proper syntax for such changes in the on-line documentation for WordBasic.

    If you are using Word 97, you must use a VBA macro to do the same trick. The following macro will work for you:

    <pre>Sub AutoNew()
    ' Exit macro if no fields in document
    If ActiveDocument.FormFields.Count = 0 Then Exit Sub
    ' Create variables.
    Dim OfficeAppName As String
    Dim RegSection As String
    Dim RegKey As String
    Dim FieldObj As Object
    Dim RegValue As Long

    OfficeAppName = "Word 97"
    RegSection = "UserData"
    RegKey = "Current Counter"
    ' Generate error if form field doesn't exist
    On Error GoTo ErrHandler
    Set FieldObj = ActiveDocument.FormFields("IncField")
    ' Get stored registry value, if any
    RegValue = GetSetting(OfficeAppName, RegSection, RegKey, 0)
    ' If not previously set, set to default
    If RegValue = 0 Then RegValue = 1
    ' Set form field result to stored value
    FieldObj.Result = CStr(RegValue)
    ' Increment and update invoice number
    SaveSetting OfficeAppName, RegSection, RegKey, RegValue + 1
    ErrHandler:
    If Err <> 0 Then
    MsgBox Err.Description
    End If
    End Sub</pre>


    Many of the same caveats apply to the VBA version of the macro as they do to the WordBasic version. This macro should be saved only in the template that you will use to create your forms. The macro must be named AutoNew, so it will run whenever a document is created that is based on the form template. You should also make sure that you have a text form field in your document named IncField, which will display the incremental number. In this version, however, the number is tracked and stored in the Registry.

    Hope this helps.

  4. #4
    BAM
    Guest

    Re: IE # which auto changes each time Word is opened

    <<In this version, however, the number is tracked and stored in the Registry>>

    As pointed out by Doug Robbins, Word MVP, in his article, "Creating sequentially numbered documents (such as invoices)"
    http://www.mvps.org/word/FAQs/MacrosVBA/NumberDocs.htm

    (condensed)
    The only drawback to this is you are limited to using one workstation for the invoices. Along the same lines, if the user ever changed PC's the Registry Key would need to be manually copied and added to the new PC.

    As another note, (not that I'm being picky here - it just makes one wonder which came first the chicken or the egg) but I thought Allen's macros looked familiar. <grin>

    Take a look at these articles from the MS KB:
    "Macro to Increment Invoice Number to New Form Document"
    http://support.microsoft.com/support/kb/ar...s/Q161/3/09.asp
    "Using WordBasic to Add Sequential Number to Form Document":
    http://support.microsoft.com/support/kb/ar...s/Q119/9/90.asp?
    ~~~~~~~~~~
    Cheers!

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: IE # which auto changes each time Word is opened

    Not to be outdone by the previous fine contributions, I humbly offer another solution:

    <pre>Sub AutoNew()
    Dim nInvoice As Long
    nInvoice = Val(DocPropertyValue("InvoiceNo")) + 1
    Call DocPropertyAssign("InvoiceNo", CStr(nInvoice))
    ActiveDocument.AttachedTemplate.Save
    End Sub
    Function DocPropertyValue(strName As String)
    On Error Resume Next
    DocPropertyValue = ActiveDocument.AttachedTemplate. _
    CustomDocumentProperties(strName).Value
    End Function
    Sub DocPropertyAssign(strPropName As String, strPropValue As String)
    On Error Resume Next
    With ActiveDocument.AttachedTemplate
    .CustomDocumentProperties(strPropName).Delete
    .CustomDocumentProperties.Add Name:=strPropName, _
    Value:=strPropValue, _
    LinkToContent:=False, _
    Type:=msoPropertyTypeString
    End With
    End Sub

    Copy and paste the above code into the Thisdocument
    module of the Invoice Form template. My approach is to store
    the invoice number in a Document property. This allows
    multiple users on different PCs to create invoices with the
    template. The only requirements are that the template not be
    read only (or in a read/only network folder) or password
    protected.
    </pre>

    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Posting Permissions

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