PootyToot
PootyToot

Reputation: 329

Excel VBA - Code to open MS Project File not working

I wrote some code to allow me to select an MS Project file and open it, however when I run the code, nothing happens.

Zero errors, it just exits, any suggestions with what i'm doing wrong here?

Code below

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

Upvotes: 0

Views: 2478

Answers (2)

Shai Rado
Shai Rado

Reputation: 33682

Just a couple of notes:

First, since you are using Early Binding to refer to MS-Project, so instead of setting Set Proj = CreateObject("MsProject.Application"), which is used for Late Binding, you can use Set Proj = New MSProject.Application.

Second: since Proj is defined as MSProject.Application, in order to make the MS-Project application visible, it's enough to use 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

Upvotes: 1

PootyToot
PootyToot

Reputation: 329

Resolved by adding the following line, edited code to show

Proj.Application.Visible = True

Upvotes: 0

Related Questions