PDA

View Full Version : Visual Basic - MS Project Problems


nouveaux21
11-01-2004, 09:12 AM
Hello! I am trying to open an MSProject 2003 file, spit some data into it, then save and close (all from VB.Net)... So far, the code I have is as follows (with the .mpp file residing as c:\Project1.mpp)... All I get is "access denied" (I checked the security settings to make sure everyone can edit)... Am I even opening the file correctly? Any help will be GREATLY appreciated! Thanks! (And now for the code):


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim ms_proj_application As New Microsoft.Office.Interop.MSProject.Application
Dim ms_proj_task As Microsoft.office.interop.msproject.Task
Dim prj As Microsoft.Office.Interop.MSProject.Projects

upload(ms_proj_application, ms_proj_task, prj)
MsgBox("Project Data Uploaded", MsgBoxStyle.Information, "Project Data Uploaded")
End Sub

Private Sub upload(ByVal ms_proj_application, ByVal ms_proj_task, ByVal prj)
'from discussion boards...
'The add statement returns an object to the new project, so:
prj.BuiltinDocumentProperties("Subject") = "Project Subject"
prj.BuiltinDocumentProperties("Title") = "Project title"
ms_proj_task = prj.Tasks.Add.Name
ms_proj_task.Name = "Task Name"
prj.Resources.Add.Name = "jason thompson"
ms_proj_application.Add.SaveAs("c:/Project1.mpp", Microsoft.Office.Interop.MSProject.PjFileFormat.pjMPP)
End Sub
End Class

herilane
11-01-2004, 03:18 PM
Welcome to the forums. :)

We don't have any MS Project experts here, so this will really be the blind leading the blind... not to mention that I don't even have MS Project or VB.Net installed, and have never used either of them... But we can't let minutiae like that stop us, can we?

Step one. If you haven't yet read the Office Automation FAQ thread at the top of this forum, do so. It has some useful links, in particular to a tutorial that covers the general and recurring questions in Office Automation with .Net.

Step two. Understand the object model. Luckily, MS helpfully provides an overview here (http://msdn.microsoft.com/library/en-us/pjsdk/html/pjtocobjectmodelapplication.asp). From here we can see that the parent object is called Application; that each file opened in the Application is called a Project object, and each Project has Tasks, Resources etc. Next, read the help files, or the online reference material that the object model overview is linked to.

Step three. Think about the basic framework / algorithm - what you want to do, step by step, in the same order as you would do if you were to do this manually. This should be similar to how you would instruct a very intelligent monkey who can follow instructions but knows nothing about MS Project. Every step should do one thing, and one thing only. In this case, it would be (somewhat abbreviated):
Start MS Project
Start a new project
Change the project properties
Add a task to the project
Change the task's properties
Add a resource to the project
Change the resource's properties
Save the project
Quit MS Project
Step four. Add a reference to the MS Project object library.
Step five. Let us try translate the algorithm into real code... I would guess that it would look something like this:
Dim prjApp As New MSProject.Application
Dim prjProject As MSProject.Project
Dim prjTask As MSProject.Task
Dim prjResource As MSProject.Resource

prjProject = prjApp.Projects.Add
prjProject.BuiltinDocumentProperties("Subject") = "Project Subject"
prjProject.BuiltinDocumentProperties("Title") = "Project title"

prjTask = prjProject.Tasks.Add
prjTask.Name = "Task Name"

prjResource = prjProject.Resources.Add
prjResource.Name = "jason thompson"

prjProject.SaveAs("c:/Project1.mpp", MSProject.PjFileFormat.pjMPP)

prjApp.QuitStep six. Start debugging. :)
I am sure that this will have all kinds of errors... Unfortunately I have no way of knowing which ones. Try this, and let us know what happens; we'll try to sort out the errors as they occur.

nouveaux21
11-01-2004, 06:37 PM
...I must say, that has been one of the most useful, helpful and kind posts I have ever seen anywhere! Thank you VERY much for helping me out to not only get a better understanding of this, but also helping me get a jump-start on technology I never knew was possible until 2 days ago! I haven't solved the problem 100% yet, but I think I'm going to read over the documents you mentioned and see if I cant figure it out from here... If I do get it, I'll post my solution (the code you gave me still gives access denied, but it may be a file problem... I'll take a look at security settings too.)

^_^