在word中保存为文件名

时间:2012-05-01 21:34:03

标签: ms-word save word-vba

在下面的代码中,文件名是硬编码的,但我希望用户能够选择它。

我正在阅读有关GetSaveAsFilename但我在使用时遇到错误:“找不到方法或成员”。

fileSaveName = Application.GetSaveAsFilename _
    (fileFilter:="Excel Files (*.txt), *.txt")

这是为Word 2010编写的。我认为在{VBA'中可以使用GetSaveAsFilename是错误的吗?

 Sub Macro3()
'
' Macro3 Macro
'
'
    ActiveDocument.SaveAs2 FileName:="Questionnaire01-05-20122.txt", _
        FileFormat:=wdFormatText, LockComments:=False, Password:="", _
        AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
        EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
        :=True, SaveAsAOCELetter:=False, Encoding:=1252, InsertLineBreaks:=False, _
         AllowSubstitutions:=False, LineEnding:=wdCRLF, CompatibilityMode:=0
End Sub

3 个答案:

答案 0 :(得分:4)

我没有意识到Word没有GetSaveAsFileName或GetOpenFileName方法(Excel有)。但事实并非如此。相反,您可以尝试SaveAs FileDialog(200320072010):

Sub ShowSaveAsDialog()
Dim dlgSaveAs As FileDialog
Set dlgSaveAs = Application.FileDialog(FileDialogType:=msoFileDialogSaveAs)
dlgSaveAs.Show
End Sub

答案 1 :(得分:4)

您可以在对话框中提供包含文件名等的默认路径,即

Sub SaveName()
    Dim strFileName As String
    Dim StrPath As String
    'provide default filename
    StrPath = "c:\temp\test.docx"
    With Dialogs(wdDialogFileSaveAs)
        .Name = StrPath
        If .Display <> 0 Then
            strFileName = .Name
        Else
            strFileName = "User Cancelled"
        End If
    End With
    MsgBox strFileName
End Sub

答案 2 :(得分:0)

Dim strFilePath, strFileName
strFilePath = "C:\Users\Public\Documents\"
strFileName = "put-filename-here.docx"

With Dialogs(wdDialogFileSaveAs)
    .Name = strFilePath & strFileName
    .Show
End With