Results 1 to 6 of 6

Thread: [RESOLVED] Get SaveAs File Name in Word2003 VBA

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Resolved [RESOLVED] Get SaveAs File Name in Word2003 VBA

    I've searched everywhere... I usually code VBA in Excel, so Word is kind of foreign to me.

    When I save a copy of the document in VBA, I want the user to select the path and file name. Excel has the GetSaveAsFilename, but Word doesn't seem to have this...

    Any ideas?

    Thanks in advance,
    Chrissy

  2. #2
    Addicted Member
    Join Date
    Jan 2009
    Posts
    183

    Re: Get SaveAs File Name in Word2003 VBA

    This is what I use for PowerPoint. A quick test in word worked, although you will have to change the parameters for Word. Run the Demonstration function 'Test', and change the parameters it sends to the SaveAsCommonDialog in that function for your needs in Word.

    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetSaveFileNameA Lib "comdlg32.dll" (pOpenfilename As OPENFILENAME) As Long
    4. Private Declare Function GetActiveWindow Lib "user32" () As Long
    5. Private Type OPENFILENAME
    6.     lStructSize As Long
    7.     hwndOwner As Long
    8.     hInstance As Long
    9.     lpstrFilter As String
    10.     lpstrCustomFilter As String
    11.     nMaxCustFilter As Long
    12.     nFilterIndex As Long
    13.     lpstrFile As String
    14.     nMaxFile As Long
    15.     lpstrFileTitle As String
    16.     nMaxFileTitle As Long
    17.     lpstrInitialDir As String
    18.     lpstrTitle As String
    19.     Flags As Long
    20.     nFileOffset As Integer
    21.     nFileExtension As Integer
    22.     lpstrDefExt As String
    23.     lCustData As Long
    24.     lpfnHook As Long
    25.     lpTemplateName As String
    26.    
    27. End Type
    28.  
    29. Public Function SaveAsCommonDialog(Optional sTitle = "Save File", Optional sFilter As String = "", Optional sDefaultDir As String, Optional filename As String = "") As String
    30.     Const clBufferLen As Long = 255
    31.     Dim OFName As OPENFILENAME, sBuffer As String * clBufferLen
    32.    
    33.     On Error GoTo ExitFunction
    34.     OFName.lStructSize = Len(OFName)
    35.     OFName.hwndOwner = GetActiveWindow  'or Me.hwnd in VB
    36.     OFName.hInstance = 0                'or App.hInstance in VB
    37.     If Len(sFilter) Then
    38.         OFName.lpstrFilter = sFilter
    39.     Else
    40.         OFName.lpstrFilter = "Text Files (*.txt)" & Chr$(0) & "*.txt" & Chr$(0) & "All Files (*.*)" & Chr$(0) & "*.*" & Chr$(0)
    41.     End If
    42.    
    43.     ' set file to initialDir and Filename (Note - need to retain length of sBuffer)
    44.     If filename <> "" Then
    45.         Dim initialFilePath As String
    46.         If Right(sDefaultDir, 1) = "\" Then
    47.             initialFilePath = sDefaultDir & filename
    48.         Else
    49.             initialFilePath = sDefaultDir & "\" & filename
    50.         End If
    51.         sBuffer = initialFilePath & (Right(sBuffer, Len(sBuffer) - Len(initialFilePath)))
    52.     End If
    53.    
    54.     OFName.lpstrFile = sBuffer
    55.     OFName.nMaxFile = clBufferLen       'Set max number of characters
    56.     OFName.lpstrFileTitle = sBuffer
    57.     OFName.nMaxFileTitle = clBufferLen  'Set max number of characters
    58.    
    59.     'Set the initial directory
    60.     If Len(sDefaultDir) Then
    61.         OFName.lpstrInitialDir = sDefaultDir
    62.     Else
    63.         OFName.lpstrInitialDir = CurDir$
    64.     End If
    65.    
    66.     OFName.lpstrTitle = sTitle
    67.     OFName.Flags = 0
    68.    
    69.     'debug.Print "sBuffer: " & sBuffer
    70.    
    71.     'Show dialog
    72.     If GetSaveFileNameA(OFName) Then
    73.         SaveAsCommonDialog = Left$(OFName.lpstrFile, InStr(1, OFName.lpstrFile, Chr(0)) - 1)
    74.     Else
    75.         SaveAsCommonDialog = ""
    76.     End If
    77. ExitFunction:
    78.     On Error GoTo 0
    79. End Function
    80.  
    81. 'Demostration routine
    82. Private Function Test() As Boolean
    83.     Dim sFilePath As String
    84.     sFilePath = SaveAsCommonDialog("Save Presentation", "*.ppt", "C:\Program Files\", "Test FileName")
    85.     If Len(sFilePath) > 0 Then
    86.         MsgBox "File to save '" & sFilePath & "'."
    87.     Else
    88.         MsgBox "No file selected"
    89.     End If
    90. End Function

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Re: Get SaveAs File Name in Word2003 VBA

    I pasted into Word and ran the test... it seems to work. Thank you Thank you!!! It's almost quitting time right now, I will adjust to my specs and give it a try tomorrow before I close the thread.

    Thanks again!
    Chrissy

  4. #4
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Get SaveAs File Name in Word2003 VBA

    Not sure if this can help:
    Code:
    Function GetSaveAsFilename() As String
        Const msoFileDialogSaveAs = 2 '-- without referencing to Microsoft Office xx.x library
        
        With Application.FileDialog(FileDialogType:=msoFileDialogSaveAs)
            .InitialFileName = "C:\YourFolder\abc.doc" '-- optional
            .FilterIndex = 1
            If .Show = True Then
                GetSaveAsFilename = .SelectedItems(1)
            'Else
            '    '-- return empty string
            '    MsgBox "SaveAs process was cancelled"
            End If
        End With
    End Function
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Re: Get SaveAs File Name in Word2003 VBA

    I didn't try the code, but it looks like something I did try previously and I believe it only returns the name of a selected file... I want the user to be able to select a path and type in a new file name.

  6. #6
    Addicted Member
    Join Date
    Jan 2009
    Posts
    183

    Re: Get SaveAs File Name in Word2003 VBA

    Quote Originally Posted by Chrissy View Post
    I didn't try the code, but it looks like something I did try previously and I believe it only returns the name of a selected file... I want the user to be able to select a path and type in a new file name.
    Anhn's code will also do what you want - it returns the full file path (or an empty string if no file was selected).

    I've added a few options into my function (window title, filter list, etc.) which you could add to their function if you needed them. Also, my function is not application-specific, it should work in any Office application (although I've only tested it in PPT, Excel & Word). I use it because most of my programming is in PowerPoint, and, unlike Word and Excel, there is no built-in way to access the save/open dialogs, so it is necessary to use this function.

Posting Permissions

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



Click Here to Expand Forum to Full Width