【发布时间】: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