FileDialog在某些excel版本VBA中不起作用

时间:2016-11-30 13:43:57

标签: vba

我的代码通常在excel版本2010中运行,但在2013版本中没有。看一看。

Sub select_strategy()
'Subroutine that lets the user select a strategy table
'
    'Declare local variables
    Dim strategyFileDialog As Office.FileDialog
    'Declare local variables
    Dim intStrategySelection As Integer

    'Initialize global variables
    Call initialize_global_variables

    ' Allows the user to select the strategy table to use
    Set strategyFileDialog = Application.FileDialog(msoFileDialogFilePicker)

    With strategyFileDialog
        .Title = "Select Strategy Table"
        .InitialFileName = ActiveWorkbook.Path
        .AllowMultiSelect = False
    End With

    intStrategySelection = strategyFileDialog.Show  'updates cell only if a file is selected
     If intStrategySelection <> 0 Then
        wsMain.Cells(2, 3) = strategyFileDialog.SelectedItems(1)
     Else
        wsMain.Cells(2, 3) = ""
     End If

End Sub

2013版本的错误是编译错误:找不到项目或库。 任何想法如何解决?

1 个答案:

答案 0 :(得分:0)

要使用后期绑定,请使用Excel.Application对象中的FileDialog()函数。 Excel.Application.FileDialog()是一个函数,用于为打开的工作簿返回FileDialog对象。如果您将变量声明为特定对象(例如Office.FileDialog),它将创建与代码一起编写的VBA参考中使用的版本的对象,并且在运行时在另一台PC上将忽略其他版本参考,并查找声明变量的特定版本。

您可以使用对活动工作簿的引用,但在Excel中并不需要

Dim strategyFileDialog As Object 
Set strategyFileDialog = ActiveWorkbook.Application.FileDialog(3)

'Filedialog Enumerations: 
'msoFileDialogFilePicker = 3 File picker dialog box. 
'msoFileDialogFolderPicker = 4 Folder picker dialog box. 
'msoFileDialogOpen = 1 Open dialog box. 
'msoFileDialogSaveAs = 2 Save As dialog box