MS Project 宏不会在连续运行中打开 Excel 文件
我正在编写一个带有目标开口和 Excel 文件的宏。它在宏的所有其他运行中都能成功运行。下面的代码在第一次运行时成功打开文件,但第二次就不会运行。然后它将在第三次尝试时运行,但不会在第四次尝试时运行。然后重复该循环。
Sub OpenExcelFile()
Set pj = ActiveProject
Set xlApp = New Excel.Application
xlApp.Visible = True
CurrPath = Application.ActiveProject.Path
Dim FD As FileDialog
Set FD = xlApp.FileDialog(msoFileDialogFilePicker)
With FD
.InitialFileName = "N:\Corporate\P2P"
.Title = "Select Excel File"
.Filters.Clear
.Filters.Add "Excel Files", "*.xlsm*"
.Show
End With
Dim xlFile As String
xlFile = FD.SelectedItems(1)
MyFileName = xlFile
If MyFileName <> False Then
Workbooks.Open FileName:=MyFileName
End If
FileNameFromPath = Right(MyFileName, Len(MyFileName) -
InStrRev(MyFileName, "\"))
Set xlBook = xlApp.Workbooks(FileNameFromPath)
xlBook.Activate
End Sub
I am writing a macro with the goal opening and Excel file. It works successfully on every other run of the macro. The below opens the file successfully on the first run, but won't run a second time. It will then run on a third try, but not a fourth. The cycle then repeats.
Sub OpenExcelFile()
Set pj = ActiveProject
Set xlApp = New Excel.Application
xlApp.Visible = True
CurrPath = Application.ActiveProject.Path
Dim FD As FileDialog
Set FD = xlApp.FileDialog(msoFileDialogFilePicker)
With FD
.InitialFileName = "N:\Corporate\P2P"
.Title = "Select Excel File"
.Filters.Clear
.Filters.Add "Excel Files", "*.xlsm*"
.Show
End With
Dim xlFile As String
xlFile = FD.SelectedItems(1)
MyFileName = xlFile
If MyFileName <> False Then
Workbooks.Open FileName:=MyFileName
End If
FileNameFromPath = Right(MyFileName, Len(MyFileName) -
InStrRev(MyFileName, "\"))
Set xlBook = xlApp.Workbooks(FileNameFromPath)
xlBook.Activate
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有一个不合格的
Workbooks.Open
调用。我建议您将以下内容更改为:
You have an unqualified
Workbooks.Open
call in there. I'd suggest you change this:to this: