#1   Report Post  
Posted to microsoft.public.word.vba,microsoft.public.word.docmanagement
techie
 
Posts: n/a
Default Documents.Add fails

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?


  #2   Report Post  
Posted to microsoft.public.word.docmanagement
Helmut Weber
 
Posts: n/a
Default Documents.Add fails

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"


  #3   Report Post  
Posted to microsoft.public.word.vba,microsoft.public.word.docmanagement
Howard Kaikow
 
Posts: n/a
Default Documents.Add fails

You have to create a Word object in the C# code and use that instead of
Application.

--
http://www.standards.com/; See Howard Kaikow's web site.
"techie" wrote in message
...
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?




  #4   Report Post  
Posted to microsoft.public.word.vba,microsoft.public.word.docmanagement
techie
 
Posts: n/a
Default Documents.Add fails


"Howard Kaikow" wrote in message
...
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.


  #5   Report Post  
Posted to microsoft.public.word.vba,microsoft.public.word.docmanagement
Tony Jollans
 
Posts: n/a
Default Documents.Add fails

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.

--
Enjoy,
Tony


"techie" wrote in message
.. .

"Howard Kaikow" wrote in message
...
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.






  #6   Report Post  
Posted to microsoft.public.word.vba,microsoft.public.word.docmanagement
Cindy M -WordMVP-
 
Posts: n/a
Default Documents.Add fails

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 :-)

  #7   Report Post  
Posted to microsoft.public.word.vba,microsoft.public.word.docmanagement
Tony Jollans
 
Posts: n/a
Default Documents.Add fails

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?

--
Enjoy,
Tony


"Cindy M -WordMVP-" wrote in message
news:VA.0000b848.00a0eb40@speedy...
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 :-)



  #8   Report Post  
Posted to microsoft.public.word.vba,microsoft.public.word.docmanagement
Howard Kaikow
 
Posts: n/a
Default Documents.Add fails

"techie" wrote in message
...
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,
--
http://www.standards.com/; See Howard Kaikow's web site.


  #9   Report Post  
Posted to microsoft.public.word.vba,microsoft.public.word.docmanagement
Tony Jollans
 
Posts: n/a
Default Documents.Add fails

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?

--
Enjoy,
Tony



  #10   Report Post  
Posted to microsoft.public.word.vba,microsoft.public.word.docmanagement
Howard Kaikow
 
Posts: n/a
Default Documents.Add fails

"Tony Jollans" My Forename at My Surname dot com wrote in message
...
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?


It's not a C# issue.

Word is a Single Use app.
See MSFT KB article 288902,




  #11   Report Post  
Posted to microsoft.public.word.vba,microsoft.public.word.docmanagement
Howard Kaikow
 
Posts: n/a
Default Documents.Add fails

Has the problem in this thread been resolved?


  #12   Report Post  
Posted to microsoft.public.word.vba,microsoft.public.word.docmanagement
techie
 
Posts: n/a
Default Documents.Add fails


"Cindy M -WordMVP-" wrote in message
news:VA.0000b848.00a0eb40@speedy...

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?


  #13   Report Post  
Posted to microsoft.public.word.vba,microsoft.public.word.docmanagement
Tony Jollans
 
Posts: n/a
Default Documents.Add fails

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

--
Enjoy,
Tony


"techie" wrote in message
...

"Cindy M -WordMVP-" wrote in message
news:VA.0000b848.00a0eb40@speedy...

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?




Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Word 2003 mailmerge to HTML mail fails Testingcase Mailmerge 6 July 9th 06 03:47 PM
OpenDataSource fails Laurel Mailmerge 3 January 26th 06 09:43 PM
Normal.dot fails to save anything swanskerry Microsoft Word Help 3 December 8th 05 06:52 PM
Word 2003 fails to set up Avery labels accurately, why??? TerenceR Microsoft Word Help 1 May 25th 05 09:50 PM
Mouse wheel fails to scroll help frame in Word 2003. jthomas Microsoft Word Help 0 December 22nd 04 09:29 PM


All times are GMT +1. The time now is 04:33 AM.

Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"