Can not Open Word Application when the progrm is running

S

Sachin

Hi
I have written a program in C# which uses Office interops and convert Word
into PDF.
when i keep running this program on large data set and tries to open word
application , then the tool itself starts opening all the subsequent files
into external editor..

does anyone come across such situation any idea how to tackle it ?

thanks
Sachin
 
C

Cindy M.

Hi Sachin,
I have written a program in C# which uses Office interops and convert Word
into PDF.
when i keep running this program on large data set and tries to open word
application , then the tool itself starts opening all the subsequent files
into external editor..

does anyone come across such situation any idea how to tackle it ?

I'm afraid you don't provide enough information about how your application is
handling the Word application. Nor about what, exactly, the problem is. What
are the "subsequent file" and "external editor"?

In any case, this forum targets VBA and not C#. While it's a good place to
get object model help, generally, it's not the best place to get support on
how to design C# interop apps.

There is a Word.Programming newsgroup, and there are office.developer
newsgroups. Those would be more appropriate for such a discussion. But you do
have to provide a lot more detail on your application design, how it manages
interop with the Word application, what the expected behavior is and how it's
deviating from that. You should also always include the version of Word your
application is targeting. Without this information, no one can understand
your problem, so no one can help you.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
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 :)
 
S

Sachin

Hi
i think the resolution could be language neutral and this could be some
design flaw or problem with the interop itself


I have written a program in C# which uses interop and convert Word file
into PDF.
for that i have designed a singleton class which create a word application
once in a lifetime and all future requests uses the same word application.
The program input is directory and it iterate through the directory for each
directory i checks if i already have word application instance created if
not create it else use the same.

Everything works fine until someone externally uses word application
program.
in that case my program start opening the subsequent files into external
word application instead of handling it internally ..

any idea how this should happen ?

here are few snapshots of my program

// Creating Word application
class CWordConverter
{

private Microsoft.Office.Interop.Word.Document currentDocument;
private static CWordConverter instance;
private static Microsoft.Office.Interop.Word.ApplicationClass
wordApplication;


static CWordConverter()
{
instance = null;
wordApplication = new
Microsoft.Office.Interop.Word.ApplicationClass();
wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone;
wordApplication.FeatureInstall =
Microsoft.Office.Core.MsoFeatureInstall.msoFeatureInstallNone;
}

public static void CloseApplication()
{
Object donotsave = WdSaveOptions.wdDoNotSaveChanges;
Object format = WdOriginalFormat.wdOriginalDocumentFormat;
Object missing = Type.Missing;

wordApplication.Quit(ref donotsave, ref format, ref missing);
}
private CWordConverter()
{
}

public static CWordConverter GetInstance()
{
if (instance == null)
{
instance = new CWordConverter();
}

return instance;
}
}

// opening document
currentDocument = wordApplication.Documents.Open(
ref paramSourceDocPath, ref objConfirmConversions, ref
objOpenReadOnly,
ref paramMissing, ref objPasswordtoOpen, ref
objPasswordtoOpentemplate,
ref objRevert, ref ObjWritePasswordDocument, ref
WritePasswordTemplate,
ref paramMissing, ref paramMissing, ref paramMissing,
ref objRepair, ref paramMissing, ref paramMissing,
ref paramMissing);
 
C

Cindy M.

Hi Sachin,
Everything works fine until someone externally uses word application
program.
in that case my program start opening the subsequent files into external
word application instead of handling it internally ..

OK, yes, I do recognize this scenario. The problem has to do with how most
Office applications are tracked in the Windows (COM) ROT (table of running
applications).

The Office applications were designed to run a single instance, only, by
default. If Word receives on other instructions (and the end-user has no
options for this), it will open every document in the same application
instance. So if you've started an instance and the user chooses to open a
document, the user will pick up your instance.

The most usual way around this is for you to start *two* instances and use
only the second one. The ROT registers only the first instance. So when the
user opens a document, he will link to that first instance (that your
application is not using).

But keep in mind that this is COM - you will have to manage your objects
(the instances) very carefully and remember to destroy them both.
Otherwise, you'll be leaving "orphaned", invisible instances of WinWord.exe
in memory (which will leak resources like a bathtub drain).

You may find this set of articles useful

How To Attach to a Running Instance of an Office Application
http://support.microsoft.com/kb/238975/en-us

PRB: Visual C# .NET Error Attaching to Running Instance of Office
Application
http://support.microsoft.com/kb/316125/en-us

BUG: Starting Word Manually Uses Same Instance as Automation
http://support.microsoft.com/kb/188546/

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
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 :)
 
S

Sachin

Thanks that was really helpful
Cindy M. said:
Hi Sachin,


OK, yes, I do recognize this scenario. The problem has to do with how most
Office applications are tracked in the Windows (COM) ROT (table of running
applications).

The Office applications were designed to run a single instance, only, by
default. If Word receives on other instructions (and the end-user has no
options for this), it will open every document in the same application
instance. So if you've started an instance and the user chooses to open a
document, the user will pick up your instance.

The most usual way around this is for you to start *two* instances and use
only the second one. The ROT registers only the first instance. So when the
user opens a document, he will link to that first instance (that your
application is not using).

But keep in mind that this is COM - you will have to manage your objects
(the instances) very carefully and remember to destroy them both.
Otherwise, you'll be leaving "orphaned", invisible instances of WinWord.exe
in memory (which will leak resources like a bathtub drain).

You may find this set of articles useful

How To Attach to a Running Instance of an Office Application
http://support.microsoft.com/kb/238975/en-us

PRB: Visual C# .NET Error Attaching to Running Instance of Office
Application
http://support.microsoft.com/kb/316125/en-us

BUG: Starting Word Manually Uses Same Instance as Automation
http://support.microsoft.com/kb/188546/

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)


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

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