Documents.Add fails

  • Thread starter Thread starter techie
  • Start date Start date
T

techie

I have a C# application that creates an instance of Word 2003 and executes a
macro. The macro tries to create a new document using the code below:

Application.Documents.add(Template:=strAppPath & "\" &
"ErrMsgUserguide.dot", NewTemplate:=False, DocumentType:=0)

Every time this line fails. What could be causing this error?

If I run the macro from the MS Visual Basic IDE in Word it works fine.
Obviously calling the same macro from the C# application has something to do
with the problem. Any ideas?
 
Hi,
any ideas?

well, if any idea would be welcome,
then I'd say:
Not that I know much about c#.
But if you want to control an application from outside
you'll need to declare an object
and set it to the application you want to control.
Whether this is ExcalVBA or VB or c# doesn't matter.

In the end it would like:
oWrd.documents.add

where oWrd is the Word Application.Object.

And there may be more appropriate groups than "docmanagement".

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
You have to create a Word object in the C# code and use that instead of
Application.
 
Howard Kaikow said:
You have to create a Word object in the C# code and use that instead of
Application.

I am creating a Word object in my C# code. Its seems that this creates a
lock on the Documents collection. The macro is unable to add a new document
in Documents.

I tried doing the same thing in my C# code and that works. Effectively I've
transferred that one line from the macro to my C# and that does the trick.

I still don't know what causes the problem. Is it a security issue? The C#
application is standalone application rather than a website.
 
I think you're missing the point of Helmut's and Howard's posts

Application.etc.

will not work unless "Application" is the name of your object variable which
points to the Word object you have created.
 
Hi Tony,
I think you're missing the point of Helmut's and Howard's posts

Application.etc.

will not work unless "Application" is the name of your object variable which
points to the Word object you have created.
No, I think you an Helmut misunderstood. "Techie" was calling a macro in the
Word environment - in a Word VBA project. Application is correct in that case.
The problem is that the WORD MACRO apparently can't execute Documents.Add
under the given circumstances.

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 :-)
 
Ah, I see. Interesting.

What is the failure, techie? Do you get an error code or message?
Does it work if you just code Documents.Add (w/o "Application")?
And what object pointers do you have set in your C# code?
 
techie said:
I have a C# application that creates an instance of Word 2003 and executes a
macro. The macro tries to create a new document using the code below:

Application.Documents.add(Template:=strAppPath & "\" &
"ErrMsgUserguide.dot", NewTemplate:=False, DocumentType:=0)

Every time this line fails. What could be causing this error?

If I run the macro from the MS Visual Basic IDE in Word it works fine.
Obviously calling the same macro from the C# application has something to do
with the problem. Any ideas?



To clarify my (mis)understanding:

You have a C# program that creates a Word Object,

Do you invoke the macro directly fom the C# program?
In which case, you have to replace Application with your5 Word object
variable.

If you are instead, using the Word object you created in C# to open an
extant Word document, and the nacro is then run WITHIN Word, then I
understand the problem.

When you create asn instance of Word in C#, that instance is NOT the same as
the Application instance used in the Word code run in Word. so you are
dealing with two different document collections.

If the latter is the problem, the solution depends on the details of your
code,
 
When you create asn instance of Word in C#, that instance is NOT the same
as
the Application instance used in the Word code run in Word.

What is it then? Is there something about C# that causes a second instance
of Word to be (implicitly) created under some circumstances?
 
Tony Jollans said:
same

What is it then? Is there something about C# that causes a second instance
of Word to be (implicitly) created under some circumstances?

It's not a C# issue.

Word is a Single Use app.
See MSFT KB article 288902,
 
No, I think you an Helmut misunderstood. "Techie" was calling a macro in the
Word environment - in a Word VBA project. Application is correct in that case.
The problem is that the WORD MACRO apparently can't execute Documents.Add
under the given circumstances.

Spot on.

I trapped the error in the Word macro. A rather strange error (number
5151):

Word was unable to read this document. It may be corrupt. Try one or more of
the following:
*Open and Repair the file.
*Open the filewith the Text Recovery converter.

If the word file is really corrupt it shouldn't have loaded at all. After
loading the document in Word I run a validation macro. This completes fine.
Then I call a second macro in another add-in and then this error occurs.

Any ideas?
 
Error 5151 probably means the Template can't be found, so what is in
variable strAppPath?
 

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

Back
Top