如何启动SAVE AS FileDialog

时间:2018-03-07 15:36:14

标签: vba outlook outlook-vba savefiledialog

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

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

1)在特定文件夹中打开

2)Pre-Populate带有DateTime邮票的文件名(并允许用户填写其余名称)

为了便于说明,我修剪了下面的功能代码(我从一个简单的测试子中调用它)

我是一个长期的Access VBA开发[呃,但是当我尝试在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

1 个答案:

答案 0 :(得分:0)

此函数将返回所选的文件名。它实际上并没有节省一些东西。它不清楚你想要做什么。

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