That's interesting - and may hit a point.
I have a handler that's called when a button is cliked (for the sake of
clarity - Fill-In Fields)
What's interesting is that even the function _protectDocument failes (more
precisly: _thisApplication.ActiveDocument.Protect( type, ref objNoReset, ref
objPass, ref objMissing, ref objMissing ); )
As you can see in my comments, I get the CAPI on the TrackRevisions, but
interestingly the Product fails with a different exception - I get an error
stating that this function cannot be performed because a macro is already
running.)
Just so that you know even more about what's going on, I have a generic
button handler for all commandbar clicks. Each button has an OnAction
propery that is a text (of the actual function) - I use reflection to invoke
this function. But to get to the point, I launch this in it's own thread.
Because the handler's aparement is MTA, and I need STA for some
functionality, plus I get a better UI performance when I use a seperate
thread. I've include my button handler class as well. You should be able
to tell, that I use the MethodInfo from reflection as a parameter to my
thread object (also below) so that I can perform these tasks centrally (code
reuse).
Hopefully this gives you plenty to work from, any help would be grealty
appreciated...
thanks Cindy!
Fruber
Function impl below:
private void _fillInForm()
{
Debug.WriteLine( "_fillInForm()" );
_setButtonState( LegalLibrary.CommandBarControl.TBTAG_FILLIN_FIELDS,
Office.MsoButtonState.msoButtonDown );
_setButtonState( LegalLibrary.CommandBarControl.TBTAG_EDIT_TEXT,
Office.MsoButtonState.msoButtonUp );
_setButtonState( LegalLibrary.MenuBarControl.MBTAG_FILLIN_FIELDS,
Office.MsoButtonState.msoButtonDown );
_setButtonState( LegalLibrary.MenuBarControl.MBTAG_EDIT_TEXT,
Office.MsoButtonState.msoButtonUp );
// TODO: was getting CAPI exception on next line?
//_thisApplication.ActiveDocument.TrackRevisions = false;
// TODO: was getting exception because a macro is currently running?
//_protectDocument( Word.WdProtectionType.wdAllowOnlyFormFields );
}
public class ButtonHandler
{
static public void handleClickEvent( MethodInfo mi, object objClass )
{
Thread t = CThread.CreateThread( new CThread.Start( _handleClickEvent ),
new object[]{mi,objClass} );
t.ApartmentState = ApartmentState.STA;
t.Name = String.Format( "{0}_Thread", mi.Name );
t.Start();
}
static private void _handleClickEvent( object[] args )
{
MethodInfo mi = args[0] as MethodInfo;
object objClass = args[1];
mi.Invoke( objClass, new object[]{} );
}
}
public class CThread
{
public delegate void Start( object[] o );
private class Args
{
public object[] o;
public Start s;
public void work(){ s(o); }
}// end of private class Args
public static Thread CreateThread( Start s, object[] args )
{
Args a = new Args();
a.o = args;
a.s = s;
Thread t = new Thread( new ThreadStart( a.work ) );
return t;
}// end of CreateThread
}// end of class CThread