以下代码显示了一个带有预设过滤器的“另存为”对话框。如果未取消,则返回保存文件的完整路径。
此代码适用于 32 位 Office。对于 64 位 Office,必须更改声明。
在任何 Subs 或 Functions 之前放置以下声明:
'Declarations for GetSaveAsFile
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (ByRef pOpenfilename As OPENFILENAME) As Long
Private Const OFN_EXPLORER As Long = &H80000
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 GetSaveAsFile(Optional ByVal HWnd As Long = 0, Optional ByVal strPath As String, _
Optional ByVal arrFilters As Variant, Optional ByRef lngFilterIndex As Long = -1, Optional ByVal strTitle As String = "") As String
'00-01s Prompts for a file name using a SaveAs dialog
'Returns full path; returns "" if canceled
'Saves to default location if no path specified
'"All Files" if no filters specified; no filter if arrFilters = ""
'lngFilterIndex: 0-based index for selected filter (in and out); -1 if canceled
'strTitle = "": "Save as" localized
On Error GoTo ErrHand
Dim typOFName As OPENFILENAME
Const lngMAX_FILE As Long = 500 'buffer size
Dim strFile As String 'file name
Dim strFilters As String 'filters string
Dim i As Long
strFile = Mid$(strPath, InStrRev(strPath, "\") + 1) 'crop preset file name/pattern from path
If IsMissing(arrFilters) Or IsEmpty(arrFilters) Then 'default: All files (*.*)
strFilters = "All files (*.*)" & vbNullChar & "*.*" & vbNullChar & vbNullChar
ElseIf VarType(arrFilters) = vbString Then 'no filter
Else 'compose filter string
For i = 0 To UBound(arrFilters, 1)
strFilters = strFilters & arrFilters(i, 0) & " (*." & arrFilters(i, 1) & ")" & vbNullChar & "*." & arrFilters(i, 1) & vbNullChar
Next i
End If
strFilters = strFilters & vbNullChar 'append second vbNullChar
'prepare structure
With typOFName
.lStructSize = Len(typOFName)
.hwndOwner = HWnd 'parent window
.lpstrFilter = strFilters 'file filters
.nFilterIndex = lngFilterIndex + 1 'preset filter index
.lpstrFile = strFile & Space$(lngMAX_FILE - Len(strFile) - 2) 'create buffer and preset file name
.nMaxFile = lngMAX_FILE 'maximum length of a returned file
.lpstrInitialDir = strPath 'initial path
.lpstrTitle = strTitle 'dialog title
.flags = OFN_EXPLORER 'show explorer style dialog
.lpstrDefExt = "" 'enables default extension according to selected filter
If GetSaveFileName(typOFName) Then 'call dialog
GetSaveAsFile = Left$(.lpstrFile, InStr(1, .lpstrFile, vbNullChar) - 1) 'cut before NullChar
lngFilterIndex = .nFilterIndex - 1 'read filter index
Else
lngFilterIndex = -1
End If
End With
Exit Function
ErrHand: 'your error handler
End Function
参数说明:
'HWnd:调用该函数的窗口的窗口句柄,例如您的 Word 窗口; ActiveWindow.HWnd。或者您可以传递0.
'strPath:使用此路径打开“另存为”对话框
'arrFilter:基于 0 的二维数组,具有一个或多个过滤器名称和文件扩展名;或 "" 表示所有文件 。
'lngFilterIndex:如果您传递具有多个过滤器定义的数组,则为 0 或更高。
'strTitle:如果您喜欢特别的一个
您调用该函数将文档保存为 docx,如下所示:
'Example to call the function:
Sub SaveDoc()
Dim arrFilters As Variant '0-based 2-dim. array with filter types and extensions
Dim strFullPath As String 'resulting full path from dialog
Dim lngFormat As Word.WdSaveFormat 'format to save document
ReDim arrFilters(0 To 0, 0 To 1) 'prepare array for 1 filter
arrFilters(0, 0) = "Word Document" 'filter file type
arrFilters(0, 1) = "docx" 'filter extension
lngFormat = wdFormatXMLDocument 'docx format (change if you prefer another format)
strFullPath = GetSaveAsFile(HWnd:=ActiveWindow.HWnd, strPath:=ActiveDocument.Path & "\", _
arrFilters:=arrFilters, lngFilterIndex:=0, strTitle:="")
If Len(strFullPath) > 0 Then 'skip if dialog canceled
ActiveDocument.SaveAs2 strFullPath, lngFormat 'save active document (see reference of SaveAs2 for more parameters)
End If
End Sub