[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
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:
Option Explicit
Private Declare Function GetSaveFileNameA Lib "comdlg32.dll" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Public Function SaveAsCommonDialog(Optional sTitle = "Save File", Optional sFilter As String = "", Optional sDefaultDir As String, Optional filename As String = "") As String
Const clBufferLen As Long = 255
Dim OFName As OPENFILENAME, sBuffer As String * clBufferLen
On Error GoTo ExitFunction
OFName.lStructSize = Len(OFName)
OFName.hwndOwner = GetActiveWindow 'or Me.hwnd in VB
OFName.hInstance = 0 'or App.hInstance in VB
If Len(sFilter) Then
OFName.lpstrFilter = sFilter
Else
OFName.lpstrFilter = "Text Files (*.txt)" & Chr$(0) & "*.txt" & Chr$(0) & "All Files (*.*)" & Chr$(0) & "*.*" & Chr$(0)
End If
' set file to initialDir and Filename (Note - need to retain length of sBuffer)
If filename <> "" Then
Dim initialFilePath As String
If Right(sDefaultDir, 1) = "\" Then
initialFilePath = sDefaultDir & filename
Else
initialFilePath = sDefaultDir & "\" & filename
End If
sBuffer = initialFilePath & (Right(sBuffer, Len(sBuffer) - Len(initialFilePath)))
End If
OFName.lpstrFile = sBuffer
OFName.nMaxFile = clBufferLen 'Set max number of characters
OFName.lpstrFileTitle = sBuffer
OFName.nMaxFileTitle = clBufferLen 'Set max number of characters
'Set the initial directory
If Len(sDefaultDir) Then
OFName.lpstrInitialDir = sDefaultDir
Else
OFName.lpstrInitialDir = CurDir$
End If
OFName.lpstrTitle = sTitle
OFName.Flags = 0
'debug.Print "sBuffer: " & sBuffer
'Show dialog
If GetSaveFileNameA(OFName) Then
SaveAsCommonDialog = Left$(OFName.lpstrFile, InStr(1, OFName.lpstrFile, Chr(0)) - 1)
Else
SaveAsCommonDialog = ""
End If
ExitFunction:
On Error GoTo 0
End Function
'Demostration routine
Private Function Test() As Boolean
Dim sFilePath As String
sFilePath = SaveAsCommonDialog("Save Presentation", "*.ppt", "C:\Program Files\", "Test FileName")
If Len(sFilePath) > 0 Then
MsgBox "File to save '" & sFilePath & "'."
Else
MsgBox "No file selected"
End If
End Function
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
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
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.
Re: Get SaveAs File Name in Word2003 VBA
Quote:
Originally Posted by
Chrissy
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.