【问题标题】:getting save as file name in word在word中保存为文件名
【发布时间】:2012-05-11 09:25:22
【问题描述】:

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

我正在阅读有关 GetSaveAsFilename 的信息,但在使用它时出现错误:“找不到方法或成员”。

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

这是为 Word 2010 编写的。我认为 GetSaveAsFilename 在 Word VBA 中可用是不是错了?

 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

【问题讨论】:

    标签: vba ms-word save


    【解决方案1】:

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

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

    【讨论】:

    • +1 请参阅this worked out example 了解如何使用FileDialog
    • 道格,我添加了一个稍微不同的答案,因为我认为上面的代码没有清楚地说明如何检索用户条目(我认为它需要像 dlgSaveAs.SelectedItems(1) 这样的东西)
    • 是的,我有这个,但正如 Brettdj 所说,这并没有明确返回文件名
    【解决方案2】:

    您可以为对话框提供包括文件名在内的默认路径,即

    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
    

    【讨论】:

    • 一件事只返回名称而不是完整路径?但我确信我可以解决这个问题:) 为示例人员欢呼
    【解决方案3】:
    Dim strFilePath, strFileName
    strFilePath = "C:\Users\Public\Documents\"
    strFileName = "put-filename-here.docx"
    
    With Dialogs(wdDialogFileSaveAs)
        .Name = strFilePath & strFileName
        .Show
    End With
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多