Vba打开excel文件代码:Excel VBA-打开MS项目文件的代码不起作用

关于Vba打开excel文件代码的问题,在ms project application中经常遇到, 我写了一些代码,让我选择一个 MS 项目文件并打开它,但是当我运行代码时,什么都不会发生。

我写了一些代码,让我选择一个 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
1

只是几个笔记:

First,由于您使用的是 Early Binding 来引用MS-Project,因此可以使用Set Proj = New MSProject.Application代替用于 Late Binding 的Set Proj = CreateObject("MsProject.Application")

第二:由于Proj被定义为MSProject.Application,为了使 MS-Project 应用程序可见,使用Proj.Visible = True就足够了。

Code
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
0

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

Proj.Application.Visible = True

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(256)
Cpu是什么大学:DigitalOcean的CPU规格是什么
上一篇
程序锁忘记密码怎么解除:“忘记密码”限制(desitorrents)
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(16条)