【问题标题】:How to launch SAVE AS FileDialog如何启动另存为文件对话框
【发布时间】:2018-03-07 15:36:14
【问题描述】:

我之前编写过 vba 以通过引用 Excel (because I understand that Outlook does not support the FileDialog object) 打开文件对话框作为文件选择器。但是,我在做非常相似的事情时遇到了很大的困难,但为了节省。

我的要求是构建一个将打开另存为文件对话框的宏,其中:

1) 在特定文件夹中打开

2) Pre-Populate 带有DateTime 标记的文件名(并允许用户填写名称的其余部分)

为了方便解释,我把下面的函数代码剪掉了(我从一个简单的Testing Sub中调用它)

我是一名长期的 Access VBA 开发人员[er,但在尝试在 Outlook 和 Word 中编写 vba 时遇到了困难,因此我们将不胜感激。


这行得通:

Public Function fnOpenFileDialog_Save2(strStartingPath As String)
On Error GoTo HandleErr

    'Launch File Browser
    'NOTE:  Outlook actually does NOT support the FileDialog, so you need to hack a solution and use another Office app instead
    'This uses Excel to open the FileDialog

   Dim xlobj As Excel.Application
   Set xlobj = New Excel.Application
        With xlobj.FileDialog(msoFileDialogOpen)

            .InitialFileName = strStartingPath
            .Title = "msoFileDialogOpen WORKS!"
            .Show
            'I have removed the code to select and Open Item/File as it DOES work
        End With
    xlobj.Quit
    Set xlobj = Nothing

ExitHere:
    Exit Function
HandleErr:    
    MsgBox "Error during fnOpenFileDialog_Save2 - " & Err.Description
    GoTo ExitHere
End Function

如果我尝试更改为 SaveAs FileDialog,我将无法正常工作。下面是我的失败示例:

Public Function fnOpenFileDialog_Save3(strStartingPath As String)
On Error GoTo HandleErr

   Dim xlobj As Excel.Application
   Set xlobj = New Excel.Application
        With xlobj.FileDialog(msoFileDialogSaveAs) 'Throws an ERROR HERE:  "Automation Error.  The remote procedure call failed"

            .InitialFileName = strStartingPath
            .Title = "msoFileDialogSaveAs does NOT work!"
            .Show
            'I have removed the code to select and Open Item/File as it DOES work
        End With
    xlobj.Quit
    Set xlobj = Nothing

ExitHere:
    Exit Function

    Public Function fnOpenFileDialog_Save4(strStartingPath As String)
    On Error GoTo HandleErr

    Dim xlobj As Application

       Set xlobj = New Application
            With xlobj.FileDialog(msoFileDialogSaveAs) 'Throws an ERROR HERE:  "Object doesn't support this property or method"

                .InitialFileName = strStartingPath
                .Title = "msoFileDialogSaveAs does NOT work!"
                .Show

            End With
        xlobj.Quit
        Set xlobj = Nothing

    ExitHere:
        Exit Function

Public Function fnOpenFileDialog_Save5(strStartingPath As String)
On Error GoTo HandleErr

Dim objDialog As Object
Set objDialog = Application.FileDialog(msoFileDialogSaveAs)

        With objDialog 'Throws an ERROR HERE:  "Object doesn't support this property or method"

            .InitialFileName = strStartingPath
            .Title = "msoFileDialogSaveAs does NOT work!"
            .Show
    End With
    objDialog.Quit
    Set objDialog = Nothing

ExitHere:
    Exit Function

【问题讨论】:

  • 一方面..您是否需要指定应用程序的类型而不仅仅是应用程序
  • 在“Save3”中,我使用了 Excel,我想我确实将应用程序指定为 Excel,但我可能会弄错。
  • 我的错。你在上面做了。
  • 您要保存什么类型的文件?扩展?

标签: vba outlook savefiledialog


【解决方案1】:

此函数将返回选定的文件名。它实际上不会保存任何东西。不清楚您要做什么。

Public Function fnOpenFileDialog(strStartingPath As String) As String
    Dim strStartingPath As String
    strStartingPath = Date
    Dim xlobj As Excel.Application
    Set xlobj = New Excel.Application
    fnOpenFileDialog = xlobj.GetSaveAsFilename(InitialFileName:=strStartingPath, fileFilter:="Excel Files (*.xls), *.xls")
End Function

【讨论】:

  • 对不起,如果我不清楚。我试图简洁的尝试可能失败了。我正在尝试编写一个 OUTLOOK 宏,但是 Outlook 不支持 FileDialogs,所以我需要调用 EXCEL 并使用 Excel 打开一个 FileDialog。问题是如果我以 FilePicker (msoFileDialogOpen) 的形式打开它,我可以通过这种方式成功打开 FileDialog。我不知道如何打开 FileDialog 进行保存(msoFileDialogSaveAs)。 FileDialog 让我完全困惑,因为我只是不知道如何在这些不同的应用程序中使用它??
  • 你在保存什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-18
  • 1970-01-01
相关资源
最近更新 更多