VBA Excel 文件打开提示取消错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15898853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:29:52  来源:igfitidea点击:

VBA Excel File Open Prompt Cancel Error

excelvbaerror-handlingfile-processing

提问by grimchamp

So I am using a file open prompt to gather a filename. I then open this file in a background instance, parse information to a dictionary and close the file. This works fine. The code for this is:

所以我使用文件打开提示来收集文件名。然后我在后台实例中打开此文件,将信息解析为字典并关闭文件。这工作正常。代码如下:

Application.FileDialog(msoFileDialogOpen).Show
sFullName = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)

If the user presses cancel, which is obviously a feasible scenario, I get the following error:

如果用户按下取消,这显然是一个可行的方案,我会收到以下错误:

Invalid procedure call or argument

I have tried to change the 'gather' line to:

我试图将“收集”行更改为:

If Application.FileDialog(msoFileDialogOpen).SelectedItems(1) Then sFullName = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)

However this still brings up the error. Even disabling alerts brings up an error or '400'. Any help on how to make this popup or handle it would be greatly appreciated.

但是,这仍然会带来错误。即使禁用警报也会出现错误或“400”。任何有关如何制作此弹出窗口或处理它的帮助将不胜感激。

采纳答案by brettdj

You could use GetSaveAsFilenamei.e.

你可以使用GetSaveAsFilename

Dim strFileName As String
strFileName = Application.GetSaveAsFilename
If strFileName = "False" Then MsgBox "User cancelled"

回答by Alex K.

You need check the bounds to determine if anything was selected

您需要检查边界以确定是否选择了任何内容

with Application.FileDialog(msoFileDialogOpen)

    .Show

    if (.SelectedItems.Count = 0) Then
        '// dialog dismissed with no selection
    else
        sFullName = .SelectedItems(1)
    end if
end with