in

MS Word VBA - Increment Filename

Hi, I have a form that automatically (1) increments the form number (2) saves the file with the same number.  The script uses a text file to keep track of the number. The only problem is, if the user does not save the file, the number is still incremented, thus the next file that is saved uses the new number.  When we look at the saved files, there are numbers missing.  We use the form for auditing purposes so we cannot have missing files.  What I'd like to do is:

When the user clicks on the SAVE and Print button (and only when they do) the directory where the files are stored is parsed and the text in the name stripped away and the last number used incremented then placed in the form as well as used in the name of the file.  Here's the code of what I currently have:

Private Sub CmdSavenPrint_Click()
Dim FileName As String
Dim PathName As String
Dim Doc As Dialog


       If ActiveDocument.FormFields("fldRequestedBy").Result = "Select Name" Then
          MsgBox "You must select a Requestor Name."
         Selection.GoTo wdGoToBookmark, Name:="fldRequestedBy"
        End If
        If ActiveDocument.FormFields("fldSystemName").Result = "Select System" Then
          MsgBox "You must select a System."
         Selection.GoTo wdGoToBookmark, Name:="fldSystemName"
         End If
         If ActiveDocument.FormFields("fldStatus").Result = "Select Status" Then
          MsgBox "You must select a Status."
         Selection.GoTo wdGoToBookmark, Name:="fldStatus"
         End If
         If ActiveDocument.FormFields("fldClass").Result = "Select Class" Then
          MsgBox "You must select a Class."
         Selection.GoTo wdGoToBookmark, Name:="fldClass"
         End If
         
         If MsgBox("Are you sure you are ready to save and print your request?  Please check all fields before continuing.", vbYesNo) = vbYes Then
     
     
    fldReqNo = System.PrivateProfileString("L:\Information Systems\Documentation\2004_Master_Documents\Change_Control_Docs\Settings.Txt", _
        "MacroSettings", "fldReqNo")

   ActiveDocument.SaveAs FileName:="L:\Information Systems\Documentation\IT_Change_Requests\IT_Request" & Format(fldReqNo, "000#")
   ActiveDocument.PrintOut
    End If
   End Sub
   
Sub FillReqNo()
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="GoDDe$$1"
End If

fldReqNo = System.PrivateProfileString("L:\Information Systems\Documentation\2004_Master_Documents\Change_Control_Docs\Settings.Txt", _
        "MacroSettings", "fldReqNo")

If fldReqNo = "" Then
    fldReqNo = 1
Else
    fldReqNo = fldReqNo + 1
End If
System.PrivateProfileString("L:\Information Systems\Documentation\2004_Master_Documents\Change_Control_Docs\Settings.txt", "MacroSettings", _
        "fldReqNo") = fldReqNo

Dim BMRange As Range
'Identify current Bookmark range and insert text
Set BMRange = ActiveDocument.Bookmarks("fldReqNo").Range
BMRange.Text = Format(fldReqNo, "000#")
'Re-insert the bookmark
ActiveDocument.Bookmarks.Add "fldReqNo", BMRange


If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
End If
End Sub


Any suggestions for acheiving what I need will be appreciated and rewarded.

Thanks!
Movie Stars

Solution: MS Word VBA - Increment Filename

You could remove the save and print options from the menu, but i don't know that that would be the best way (you'd have to put them back so users could open and work with other files... potential for problems)

This line is trying to find the number of a file given it's name.
filenumber = Int(Mid(FileName,11,3))
The mid function will return the characters, starting from the 11'th position, for a length of 3.  So if the filename was IT_Request001.txt, it would return the 001.
The Int function will convert the string "001" to the integer number 1.

So with the loop, it will search for all files, and get the highest number of the existing file.  This assumes that the number will always start at the 11th position and be 3 digits.  This function should work a bit better for varying length filenames:

Function GetFilenumber(Filename As String) As Integer
    'This will return the first number found in the Filename
    Dim i As Integer
    Dim StartPos As Integer
    Dim EndPos As Integer
    StartPos = -1
    EndPos = -1
    For i = 1 To Len(Filename)
        If IsNumeric(Mid(Filename, i, 1)) Then
            If StartPos = -1 Then StartPos = i
            EndPos = i
        Else
            If StartPos > 0 And EndPos > 0 Then Exit For
        End If
    Next i
    GetFilenumber = Int(Mid(Filename, StartPos, EndPos - StartPos + 1))
End Function


Use this instead of the int(mid(....

so the code will now look like this

'Path is the path where your files are stored.  
Path = "L:\Information Systems\Documentation\IT_Change_Requests\"

'FileName will get one filename in the directory matching the pattern.
'It looks like this is the pattern you need
FileName = Dir(Path & "IT_Request???.doc")

'This will loop through all files in the directory
While FileName <> ""

    'This will take the portion of the filename that contains a number
    filenumber = GetFilenumber(FileName)

    'This will save the highest number found
    If filenumber > highest Then highest = filenumber

    'This will get the next file in the directory
    FileName = Dir
Wend

'Here, the variable highest will contain the highest number found.



I still don't know where you are calling the FillReqNo function though, which might be part of the problem.