Office Word Macro - create file to a specific directory

I have a macro that automaticaly creates a Word file with a prefix sequential number and suffix. It saves the file to the "My Documents" folder by default. I want to be able to save the file to a different location but don't know how to do this.

My knowledge of VB is limited. Any help appreciated. Her is the vb code:

Sub Report()
' 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:\Documents and Settings\50065a.SCI\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:\Documents and Settings\50065a.SCI\Application Data\Microsoft\Word\myseq.txt", "", _
"MySeq") = MySeq

' Load the filename string with a prefix leading zeroes and a suffix the hash symbol represents the sequential number
strMyFileName$ = Format(MySeq, "prefix#-suffix")

' Open a new document
Documents.Add Template:="C:\Documents and Settings\50065a.SCI\Application Data\Microsoft\Templates\atos_report.dot", _
NewTemplate:=False, DocumentType:=0

' Set the filename to the new sequence number
ActiveDocument.SaveAs FileName:=strMyFileName

End Sub




Answer this question

Office Word Macro - create file to a specific directory

  • Asim Javed

    Moved to VBA forum


  • riga66

    OK, look at the very end of your macro. You see
    ActiveDocument.SaveAs FileName:=strMyFileName

    This is where the file name is being assigned. The code creates the
    sequence number and stores it in strMyFileName. Then Word takes the
    "current folder" and saves the document there. You can change this by
    appending the folder of your choice to the front of what's going into
    FileName.

    Try something like this, where you substitute your path for the path I
    assign to strPath:

    Dim strPath as String
    strPath = "C: \test" & "\"
    ActiveDocument.SaveAs FileName:=strPath & strMyFileName

    <<I have a macro that automaticaly creates a Word file with a prefix
    sequential number and suffix. It saves the file to the "My Documents"
    folder by default. I want to be able to save the file to a different
    location but don't know how to do this.

    My knowledge of VB is limited. Any help appreciated. Her is the vb code:

    Sub Report()
    ' 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:\Documents and
    Settings\50065a.SCI\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:\Documents and
    Settings\50065a.SCI\Application Data\Microsoft\Word\myseq.txt", "", _
    "MySeq") = MySeq

    ' Load the filename string with a prefix leading zeroes and a suffix the
    hash symbol represents the sequential number
    strMyFileName$ = Format(MySeq, "prefix#-suffix")

    ' Open a new document
    Documents.Add Template:="C:\Documents and
    Settings\50065a.SCI\Application
    Data\Microsoft\Templates\atos_report.dot", _
    NewTemplate:=False, DocumentType:=0

    ' Set the filename to the new sequence number
    ActiveDocument.SaveAs FileName:=strMyFileName

    End Sub>>



  • Office Word Macro - create file to a specific directory