|
Sometimes, when working on a project, you may want to save your documents in sequential
order (for example, "0001", "0002", "0003", and
so on). If you wanted to do this manually, you would need to sort through your
working directory for the latest file number before you could assign the next
number to a new file. Using this fairly straightforward Word macro, you can make
creating sequenced files as easy as pressing a button. Here's how.
Description of the Project
For this project, we want to be able to automatically create a new file that
is named with the next number in a running sequence. For instance, if I start
with "0001", I would want the next file to be named "0002".
In general, this is what we need to do:
- Find out which number was used last in the sequence
- Add 1 to that number to generate the next sequence number
- Create a new file and save it with the new sequence number
Not terribly complicated, is it? Now, let's consider how we would automate
this process with Word.
Storing and Accessing the Sequence Number Using PrivateProfileString
Obviously, since we will expect Word to keep track of the sequence number, we
will have to store it in such a way that it is available to Word at all times.
Visual Basic for Applications (VBA) has a built-in property, PrivateProfileString,
that makes it easy to look up preset values stored in files (if you want to
read up on this property, go right to the source at the Microsoft
Developer Network website).
In brief, PrivateProfileString allows you to store a value in a text file that
you can refer to and update at different times. Aside from the sequenced file
names that we are discussing now, it could be used to store and retrieve such
information as the name of the last file you worked on, a running total of the
number of words you've written for all documents generated from a particular
template, the number of times you've used a particular template, and so on.
For our purposes, PrivateProfileString is called as follows:
System.PrivateProfileString(Filename, Section, Key)
whereas, Filename is the name of the text file where we will store the
sequence number, Section is the number of the section in the file that
the sequence number is stored, and Key is the name of the variable that
we assign to hold the sequence number. It is this Key variable that we will
increment each time a new file is generated.
Automating the Process in Word
As mentioned above, the general process for creating sequenced file names is
to find out the last number in the sequence, add 1 to it, and create a new file
with that sequence number. Now, let's look at the step-by-step process of accomplishing
that in Word using VBA.
- Open a new file in Word
- Type the text MySeq= at the beginning of the document
- Save the file as C:\Windows\Application Data\Microsoft\Word\MySeq.txt
(see below)
- In the template that you want the macro to run in (our example will run
in the Normal document), create a macro that does the following:
- Retrieves the current value of the MySeq key from the MySeq file
- If MySeq is empty (as it will be the first time we run it), then add
1 to MySeq; else, add 1 to MySeq
- Create the text string strMyFileName, and assign to it the value of
MySeq
- Open a new file and save it with the value of strMyFileName in the
current directory.

Now that I've described how to do it in detail, here's what the actual macro
looks like:
Sub OnFileNew()
' Automatically creates a new document in a sequentially-numbered
order
' based upon the stored sequence number in MySeq text file
' Dimension the variables
Dim strMyFileName As String
' Open MySeq file and load the sequence number
MySeq = System.PrivateProfileString("C:\Windows\Application Data\Microsoft\Word\MySeq.txt",
_
"", "MySeq")
If MySeq = "" Then
MySeq = 1
Else
MySeq = MySeq + 1
End If
' Update the sequence number in MySeq
System.PrivateProfileString("C:\Windows\Application Data\Microsoft\Word\MySeq.txt",
"", _
"MySeq") = MySeq
' Load the filename string with leading zeroes
strMyFileName$ = Format(MySeq, "000#")
' Open a new document
Documents.Add Template:="C:\WINDOWS\ApplicationData\Microsoft\Templates\Normal.dot",
_
NewTemplate:=False, DocumentType:=0
' Set the filename to the new sequence number
ActiveDocument.SaveAs FileName:=strMyFileName
End Sub
Now, when you run the macro, Word will create a new, empty document that looks
like this (note the saved name of this file on the titlebar below):

Viola! Now, if you run the macro again, you will create a new document called
"0002", and so on. The value of MySeq will be preserved even after
you close Word and shutdown your computer.
Potential Modifications
You may want to tailor this macro to:
- Assign the macro to a particular template
- Change the location of the MySeq file
- Add text to the filename after the sequence number
To assign the macro to a particular template, simply open up that template
for editing and save the macro in its Project. To change the location of the
MySeq file, just modify the path in the line:
System.PrivateProfileString("C:\Windows\Application Data\Microsoft\Word\MySeq.txt",
"", _
"MySeq") = MySeq
To add text to the filename after the sequence number, insert this chunk of
code right before the line of code that opens the new document:
' Request alpha portion of file name
Dim varMessage, varTitle, varDefault, varMyValue
varMessage = "Enter file name"
varTitle = "File Name - Alpha Portion "
' Set default.
varDefault = "New doc"
' Display message, title, and default value.
varMyValue = InputBox(Message, Title, Default)
' Update file name to show alpha suffix
MyFileName$ = MyFileName$ + "_" + varMyValue
Now, Word will prompt you with a message box asking you for the text that you
want to add to the filename, as shown below:

Just type the text that you want to append to the file name, and click OK.
That's it! Although, to really make it slick, you can create a custom toolbar
button or menu and assign this macro to it, making creating a new sequenced
document as simple as a click! To learn how to create a custom menu or toolbar
button, check out this article: http://www.mousetrax.com/toolbars.html
I'd like to thank Dian Chapman, Doug Robbins, and Colin Chapman for their
invaluable assistance with this project.

|