Save Next - Visual Basic Macro

* * *

SAVENEXT is a Visual Basic Macro that runs on Microsoft Word. The purpose of the macro is to increment the number in a file name. The file name must have the format: "FileNname_X.doc", where X is a number between 1 and 99. File X is a Word document. File X is opened and when the macro is called it increments "X" to "X +1", and saves the file as "FileName_X+1.doc".


Visual Basic Code

SAVENEXT macro

Rem SaveNext macro

Rem Save current file as next file ie, "File_2.doc -> "File_3.doc"

Rem Assume 0 <= File no. <= 99

Rem

Public Sub MAIN()

Dim FilePath$

Dim StrLen

Dim Index_

Dim char$

Dim DigitCount

Dim FileNo$

Dim NumFileNo

Dim Length

Dim AbsFileNo$

Dim LeftName$

Dim NextFile$

Rem Get current file & path

FilePath$ = WordBasic.[FileName$]()

StrLen = Len(FilePath$)

Rem Incr. file no.

Rem Check if file no. > 9 ie, 1 or 2 digits

For Index_ = StrLen To 1 Step -1

char$ = Mid(FilePath$, Index_, 1)

If (char$ = "_") And (Index_ = (StrLen - 5)) Then

DigitCount = 1

GoTo Continue:

End If

If char$ = "_" And Index_ = (StrLen - 6) Then

DigitCount = 2

GoTo Continue:

End If

Next Index_

Rem

Rem Need an error handler here if "_" not found

Rem

Continue:

Rem Get file no.

FileNo$ = Mid(FilePath$, Index_ + 1, DigitCount)

Rem Incr. file no.

NumFileNo = WordBasic.Val(FileNo$) + 1

FileNo$ = Str(NumFileNo)

Length = Len(FileNo$)

Rem Extract (absolute) file no. from FileNo$ (first char is "0")

AbsFileNo$ = Mid(FileNo$, 2, Length - 1)

Rem Create next file path name

LeftName$ = WordBasic.[Left$](FilePath$, Index_)

NextFile$ = LeftName$ + AbsFileNo$ + ".doc"

Rem Save new file

WordBasic.FileSaveAs Name:=NextFile$

End Sub

Back to list of my macros

Like some details on the programmer?