Does the VBA Run Method Actually work?

M

mobe

I've ended up in a roundabout way trying to run a macro/subroutine from
managed code (a .net addin to ms project). A roundabout way, as the direct
way I was trying to perform some tasks resulted in intermittent com interop
exceptions when trying to set timephased actuals (no reason detectable
reasons). So I tried putting the code that set the timephased data into a
macro subroutine WITH parameters and calling it from the c# (and also tried
VB.net) code. Well, the Application.Macro method works fine, but of course,
no parameters. Application.Run on the other hand executes, but does NOT
execute the macro ro subroutine. I've tried .Run with a macro (no
parameters) and even that doesn't work.

So I set up a SIMPLE bit of vba where I have a macro call a subroutine
"testme":

Public Sub whatever()

Run "Global.MPT!testme", 22
Run "'Global.MPT'!testme", 22
Run "!testme", 22
Run "Global.MPT!webSLAM.testme", 22

End Sub

Public Sub testme(ByVal temp As Double)
temp = temp + 1#
Message ("hello")
End Sub

And guess what? It doesn't work. Step through it with the debugger and
nothing. Exactly the same behavoir as doing it from .net (no errors, but it
doesn't run the code).

So, is this a problem in msproject vba only? Looking on the net I see
examples of people using application.run sucessfully in excel. But not with
project.


And as far as the interop exception go, damned if know what could cause
that. The code is reasonably simple, and using exactly the same test data
and conditions, it might work 30 times and fail on the 31st, or it might
fail on the 12, or the 10th, with exactly the same data. Oh, it fails on the
"timeScaleValue.Value =" as randomly, the timescale.value object does not
get set properly (the Value object is a ComInteropException class object
rather than a String or Double, as it failed getting the data)

TimeScaleValues timeScaleValues = assignment.TimeScaleData(
date,
date.AddDays(1),
PjAssignmentTimescaledData.pjAssignmentTimescaledActualWork,
PjTimescaleUnit.pjTimescaleDays,
1);
foreach (TimeScaleValue timeScaleValue in timeScaleValues)
{
timeScaleValue.Value = (double)(hours * 60.0);
break;
}

I've tried a lot of variants of this code, like not setting the value, and
deleting the timescalevalue and just .Add-ing a new one, but same deal -
random cominterop errors.
 

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

Top