Excel VBA-打开MS Project文件的代码无效

Excel VBA-打开MS Project文件的代码无效,vba,excel,ms-project,Vba,Excel,Ms Project,我编写了一些代码,允许我选择一个MS项目文件并打开它,但是当我运行代码时,什么也没有发生 零错误,只是存在,对我做错了什么有什么建议吗 代码如下 Sub START() ' MS Project variables Dim Proj As MSProject.Application Dim NewProj As MSProject.Project 'File Name Variables Dim FileOpenType As Varian

我编写了一些代码,允许我选择一个MS项目文件并打开它,但是当我运行代码时,什么也没有发生

零错误,只是存在,对我做错了什么有什么建议吗

代码如下

Sub START()

' MS Project variables 
Dim Proj             As MSProject.Application
Dim NewProj          As MSProject.Project

'File Name Variables
Dim FileOpenType     As Variant
Dim NewProjFileName  As String
Dim NewProjFilePath  As String
Dim NewProjFinal     As String


'Code to find and open project files
Set Proj = CreateObject("MsProject.Application")
MsgBox ("Please Select MS Project File for Quality Checking")

'Select Project File
FileOpenType = Application.GetOpenFilename( _
               FileFilter:="MS Project Files (*.mpp), *.mpp", _
               Title:="Select MS Project file", _
               MultiSelect:=False)

'Detect if File is selected, if not then stop code

If FileOpenType = False Then
   MsgBox ("You Havent Selected a File")
   GoTo EndPoint
End If

'Write the FileOpenType variant to two separate strings
NewProjFilePath = Left$(FileOpenType, InStrRev(FileOpenType, "\"))
NewProjFileName = Mid$(FileOpenType, InStrRev(FileOpenType, "\") + 1)

'Open Project File
Proj.FileOpen NewProjFilePath & NewProjFileName


EndPoint:
End Sub

通过添加以下行解决,编辑代码以显示

Proj.Application.Visible = True

请注意:

首先,因为您使用早期绑定来引用MS Project,所以您可以使用
Set Proj=New MsProject.Application
而不是设置用于后期绑定的
Set Proj=CreateObject(“MsProject.Application”)

Second:由于
Proj
被定义为
MSProject.Application
,为了使MS Project应用程序可见,只需使用
Proj.visible=True

代码

Option Explicit

Sub START()

' MS Project variables

Dim Proj             As MSProject.Application
Dim NewProj          As MSProject.Project

'File Name Variables
Dim FileOpenType     As Variant
Dim NewProjFileName  As String
Dim NewProjFilePath  As String
Dim NewProjFinal     As String

Set Proj = New MSProject.Application ' since you are using Early binding, you can use this type of setting a new MS-Project instance

MsgBox "Please Select MS Project File for Quality Checking"

'Select Project File
FileOpenType = Application.GetOpenFilename( _
               FileFilter:="MS Project Files (*.mpp), *.mpp", _
               Title:="Select MS Project file", _
               MultiSelect:=False)

If FileOpenType = False Then
   MsgBox "You Havent Selected a File"
   Exit Sub ' <-- use Exit Sub instead of GoTo EndPoint
End If

'Write the FileOpenType variant to two separate strings
NewProjFilePath = Left$(FileOpenType, InStrRev(FileOpenType, "\"))
NewProjFileName = Mid$(FileOpenType, InStrRev(FileOpenType, "\") + 1)

'Open Project File
Proj.FileOpen NewProjFilePath & NewProjFileName

Proj.Visible = True ' <-- Set MS-Project as visible application

End Sub
选项显式
次启动()
'MS项目变量
作为MSProject.Application的Dim项目
作为MSProject.Project的Dim NewProj
'文件名变量
Dim FileOpenType作为变量
Dim NewProjFileName作为字符串
将NewProjFilePath设置为字符串
Dim NewProjFinal作为字符串
Set Proj=New MSProject.Application'由于您使用的是早期绑定,因此可以使用这种类型设置新的MS Project实例
MsgBox“请选择MS项目文件进行质量检查”
'选择项目文件
FileOpenType=Application.GetOpenFilename(_
FileFilter:=“MS项目文件(*.mpp),*.mpp”_
标题:=“选择MS项目文件”_
多选:=假)
如果FileOpenType=False,则
MsgBox“您尚未选择文件”

Exit Sub“也许您需要使项目应用程序可见?就是这样!谢谢大家!@PootyToot请不要;如果您不能用答案修改代码,这将使其他用户无法了解问题的起因以及解决方法。