Attempting to use automation to add/delete tasks from an MS Project file. The following code is giving me problems...in the On Delete event of a form, I want to check to be sure a corresponding task in the MSProject file exists, and if so delete it:
Private Sub Form_Delete(Cancel As Integer)
Dim ProjectNo As String
ProjectNo = Forms![frmShopOrders]![ProjectNo]
Dim prjApp As MSProject.Application
Dim prjProject As MSProject.Project
Dim prjTask As MSProject.Task
Dim tName As String
tName = ProjectNo & ", " & Forms![frmShopOrders]![ShopOrderNo] & "-" & Me.TrackingDescription
Set prjApp = CreateObject("MSProject.Application")
prjApp.FileOpen "C:\Documents and Settings\jlw\My Documents\Copy of Mach Backlog.mpp"
Set prjProject = prjApp.ActiveProject
'Check to make sure the task exists in Project before attempting to delete.
For Each prjTask In prjProject.Tasks
If prjTask.Name = tName Then
'Task Exists, OK to delete
MsgBox "The task was not found in the Project file." 'alert user that the task was not found in the project file.
The error is highlighting the If prjTask.Name=tName line, and if I hover over prjTask, it says "prjTask=Nothing" so I'm guessing it isn't realizing what it is, but I declared it above (prjTask=MSProject.Task). Any help would be appreciated.