How to run word 2003 macros from asp.net?

J

John Dolinka

I am trying to run a word macro via automation from an asp.net application.
The document loads well enough, but I get an "Unable to run the specified
macro" error when attempting to run the macro. I am using authentication
"no anonymous", so it should be able to get the right Normal.dot from my
profile (correct assumption?). I am aware of article
http://support.microsoft.com/default.aspx?scid=kb;EN-US;288366 but that
does not really work (using Word 2003 and reg key only is only good to
2002).

Additonal information.
The exception returns -532459699 for the _COMPlusExceptionCode
And the "help url" is @"C:\Program Files\Microsoft
Office\OFFICE11\1033\wdmain11.chm#37401" which is unhelpful since url is
bogus.

Snippet
object readOnly = false;
object isVisible = true;
Word.Document oWordDoc=null;
Word.ApplicationClass oWordApp=null;
object missingValue = Type.Missing;
try{
oWordApp = new Word.ApplicationClass();
}
catch(Exception exc){
ExceptionManager.Publish(exc);
}

try{
oWordDoc = oWordApp.Documents.Open(ref fileName, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref isVisible, ref missingValue, ref missingValue,
ref missingValue, ref missingValue);
}
catch(Exception exc){
ExceptionManager.Publish(exc);
}


oWordDoc.Activate();

oWordApp.Selection.TypeText("This is the text");
oWordApp.Selection.TypeParagraph();
oWordDoc.Save();
try{
string go="NewMacros.HelloWorld";
---failes after executing next line-----
oWordApp.Run(go,ref missingValue,ref missingValue,ref missingValue,ref
missingValue,ref missingValue,ref missingValue,ref missingValue,ref
missingValue,ref missingValue,ref missingValue,ref missingValue,ref
missingValue,ref missingValue,ref missingValue,ref missingValue,ref
missingValue,ref missingValue,ref missingValue,ref missingValue,ref
missingValue,ref missingValue,ref missingValue,ref missingValue,ref
missingValue,ref missingValue,ref missingValue,ref missingValue,ref
missingValue,ref missingValue,ref missingValue);
}
catch(Exception exc){
ExceptionManager.Publish(exc);
}

Thanks,

John Dolinka
 
C

Cindy M -WordMVP-

Hi John,
I am trying to run a word macro via automation from an asp.net application.
The document loads well enough, but I get an "Unable to run the specified
macro" error when attempting to run the macro.
The first thing I'd ask myself is whether macro security is allowing macros
to be run in the project in question?

What does oWordapp.AutomationSecurity return?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
J

John Dolinka

Thanks for Response.
msoAutomationSecurityLow is what oWordapp.AutomationSecurity returns.

John Dolinka
 
C

Cindy M -WordMVP-

Hi John,
msoAutomationSecurityLow is what oWordapp.AutomationSecurity returns.
OK, then all macros ought to be enabled...

Application.Run is "funny". Sometimes, you can get away with just the
macro name. Other times, you need to fully qualify the macro with
project.module.procedure (you have module.procedure, according to the
code you posted). In yet other cases, one needs to use the full file
path in order for Word to find the macro.

I'm assuming from your original message context the macro in question is
in (or should be in) the Normal.dot template? If you're unsure whether
it's the right Normal.dot, you should be able to query
NormalTemplate.FullName to get the full file path.

You might also try having the code present the application window to
you, and you see if you can run the macro over Tools/Macros. This would
at least help you narrow down whether Word is in a state where it cannot
find/run the macro, or if the problem is more likely in the
communication between your app and Word.

Another thing I'd try is to assign the macro to a toolbar button. Then
use CommandBars("Toolbar name").Controls(index).Execute and see if the
macro runs.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
J

John Dolinka

Thanks for the response.

The problem was it was using the template stored under the system profile.
Apparently coding it to use another template does not work when running
under the asp.net account

Running under the asp.net proved to be too treacherous for debugging the
word VB since it worked differently when automated. It also generated
orphaned asp.net and word processes when the user disconnected and I did not
want to write a "word killer" service. So had to do the "interface" as a
word document.

Wish there was a 3rd party control or wish Microsoft would have a "run as
service" capability so I could utilize words powerful search and replace and
formatting capabilities on large amounts of documents, yet still use asp.net
on the front end.

John Dolinka
 

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