Cannot get project document collections

S

stoods

Hi,

I am using c++ to export data into MS Project 2003. I can create a new
project without any problems, but just cannot get any collections of tasks or
resources into the project.

Even in the simple code I have below:-

// The one and only application object
#import "C:\\Program Files\\Microsoft Office\\OFFICE11\\MSPRJ.OLB"
rename_namespace("MSP") auto_search no_function_mapping

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit:):GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
::CoInitialize(NULL);
// TODO: code your application's behavior here.
MSP::_MSProjectPtr m_App;
m_App.CreateInstance(__uuidof(MSP::Application));

MSP::_IProjectDocPtr m_Project = m_App->Projects->Add();

MSP::ResourcesPtr Resources = m_Project->Resources;
TRACE("Resources %d\n", Resources->Count);

::CoUninitialize();
}

return nRetCode;
}

I get an access violation exception whenever I try to access the count
property of the resources.
 
R

Rod Gill

Your C++ code is really hard to read (C++ fault, not yours!)

VB or VBA is by far the quicker and easier to code. The following code works
in VBA for Excel provided you have set a reference for Project.olb under
Tools, References in the VBE:

Sub AddTaskAndResource()
Dim prjApp As MSProject.Application
Dim Tsk As MSProject.Task
Dim Res As MSProject.Resource
On Error Resume Next
Set prjApp = GetObject(, "MSProject.Application")
If prjApp Is Nothing Then
Set prjApp = CreateObject("MSProject.Application")
End If
prjApp.Visible = True
Set Tsk = prjApp.ActiveProject.Tasks.Add("New Task")
Set Res = prjApp.ActiveProject.Resources.Add("New Resource")
Tsk.Assignments.Add Tsk.ID, Res.ID
Set prjApp = Nothing
End Sub

I note your code creates a reference to
 
S

stoods

Hi Rod,

Thanks.

Its not that bad!!!!!

I see that you reply missed the terxt after "I note your code creates a
reference to....." Any chance of finishing that for me please?

Yes, I know that VB and VBA may be quicker - and I have done a few projects
with word using VB and VBA - , but I have found that using the #import in c++
is just as quick. The export is part of a larger app, in c++, and we have
over the years developed a number of tools etc so that we find c++ a natural
language for us to use.

Anyway, I have since found almost hidden away in the KB advice from MS NOT
to use #import with office automation, as the office automation objects have
reference counting bugs in them - go figure!!! See
http://support.microsoft.com/kb/q196776/

Anyway, I don't have VB 6 installed, and have decied to go down the c# road,
and am getting the results I need at this stage with c#, after downlaoding
the interop. I really would like to get this going in c++ but I may have to
let that go.

Thanks for you prompt reply,

Mike


Rod Gill said:
Your C++ code is really hard to read (C++ fault, not yours!)

VB or VBA is by far the quicker and easier to code. The following code works
in VBA for Excel provided you have set a reference for Project.olb under
Tools, References in the VBE:

Sub AddTaskAndResource()
Dim prjApp As MSProject.Application
Dim Tsk As MSProject.Task
Dim Res As MSProject.Resource
On Error Resume Next
Set prjApp = GetObject(, "MSProject.Application")
If prjApp Is Nothing Then
Set prjApp = CreateObject("MSProject.Application")
End If
prjApp.Visible = True
Set Tsk = prjApp.ActiveProject.Tasks.Add("New Task")
Set Res = prjApp.ActiveProject.Resources.Add("New Resource")
Tsk.Assignments.Add Tsk.ID, Res.ID
Set prjApp = Nothing
End Sub

I note your code creates a reference to

--

Rod Gill
Project MVP
Visit www.msproject-systems.com for Project Companion Tools and more


stoods said:
Hi,

I am using c++ to export data into MS Project 2003. I can create a new
project without any problems, but just cannot get any collections of tasks
or
resources into the project.

Even in the simple code I have below:-

// The one and only application object
#import "C:\\Program Files\\Microsoft Office\\OFFICE11\\MSPRJ.OLB"
rename_namespace("MSP") auto_search no_function_mapping

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit:):GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
::CoInitialize(NULL);
// TODO: code your application's behavior here.
MSP::_MSProjectPtr m_App;
m_App.CreateInstance(__uuidof(MSP::Application));

MSP::_IProjectDocPtr m_Project = m_App->Projects->Add();

MSP::ResourcesPtr Resources = m_Project->Resources;
TRACE("Resources %d\n", Resources->Count);

::CoUninitialize();
}

return nRetCode;
}

I get an access violation exception whenever I try to access the count
property of the resources.
 
R

Rod Gill

Hi,

I thought I had deleted that last sentence. I thought for one moment that
you were using the Project oledb driver which is read only. C# works fine,
but its IntelliSense is not as good as VB's and you have to provide every
parameter. In VB you only need to specify parameters that are relevant and
you can use named parameters that make the code quicker and easier to read
and write.

Still, good luck with C#. Try creating the macro in Project VBA by recording
a macro then copy paste to C# and translate to C# syntax.

--

Rod Gill
Project MVP
Visit www.msproject-systems.com for Project Companion Tools and more


stoods said:
Hi Rod,

Thanks.

Its not that bad!!!!!

I see that you reply missed the terxt after "I note your code creates a
reference to....." Any chance of finishing that for me please?

Yes, I know that VB and VBA may be quicker - and I have done a few
projects
with word using VB and VBA - , but I have found that using the #import in
c++
is just as quick. The export is part of a larger app, in c++, and we have
over the years developed a number of tools etc so that we find c++ a
natural
language for us to use.

Anyway, I have since found almost hidden away in the KB advice from MS NOT
to use #import with office automation, as the office automation objects
have
reference counting bugs in them - go figure!!! See
http://support.microsoft.com/kb/q196776/

Anyway, I don't have VB 6 installed, and have decied to go down the c#
road,
and am getting the results I need at this stage with c#, after downlaoding
the interop. I really would like to get this going in c++ but I may have
to
let that go.

Thanks for you prompt reply,

Mike


Rod Gill said:
Your C++ code is really hard to read (C++ fault, not yours!)

VB or VBA is by far the quicker and easier to code. The following code
works
in VBA for Excel provided you have set a reference for Project.olb under
Tools, References in the VBE:

Sub AddTaskAndResource()
Dim prjApp As MSProject.Application
Dim Tsk As MSProject.Task
Dim Res As MSProject.Resource
On Error Resume Next
Set prjApp = GetObject(, "MSProject.Application")
If prjApp Is Nothing Then
Set prjApp = CreateObject("MSProject.Application")
End If
prjApp.Visible = True
Set Tsk = prjApp.ActiveProject.Tasks.Add("New Task")
Set Res = prjApp.ActiveProject.Resources.Add("New Resource")
Tsk.Assignments.Add Tsk.ID, Res.ID
Set prjApp = Nothing
End Sub

I note your code creates a reference to

--

Rod Gill
Project MVP
Visit www.msproject-systems.com for Project Companion Tools and more


stoods said:
Hi,

I am using c++ to export data into MS Project 2003. I can create a new
project without any problems, but just cannot get any collections of
tasks
or
resources into the project.

Even in the simple code I have below:-

// The one and only application object
#import "C:\\Program Files\\Microsoft Office\\OFFICE11\\MSPRJ.OLB"
rename_namespace("MSP") auto_search no_function_mapping

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit:):GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
::CoInitialize(NULL);
// TODO: code your application's behavior here.
MSP::_MSProjectPtr m_App;
m_App.CreateInstance(__uuidof(MSP::Application));

MSP::_IProjectDocPtr m_Project = m_App->Projects->Add();

MSP::ResourcesPtr Resources = m_Project->Resources;
TRACE("Resources %d\n", Resources->Count);

::CoUninitialize();
}

return nRetCode;
}

I get an access violation exception whenever I try to access the count
property of the resources.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top