Starting Word from within an Excel Add-in... And vice versa

B

Boris Drajer

Hi,

I have a COM Add-in (written in C#) that works (well... kind of :)) in
Excel 2003 and Word 2003. It is connected to a document repository from
which it can download and open files. Now, what I want is to be able to
open both DOC and XLS files from either of the two applications. To be
more clear, here's a simple scenario: I fire up Excel, then click on a
menu which opens my addin's window. There I select a DOC file from a
list. This file is then opened in Word.

So, the first question is: what is the proper way to instantiate Word
(or Excel)? I tried something like this:


using Microsoft.Office.Interop;

Word.ApplicationClass word = new Word.ApplicationClass();
word.Visible = true;
word.Documents.Open(ref filename, ref missing, .... etc ....)


This opens a Word window, but if I do this twice, it causes a conflict
over Normal.dot, so I suppose this is not the proper way. What should I
do? Should I call ShellExecute()? Note that I want to be able to
communicate with the Word instance afterwards.


Thanks in advance!
 
F

Fermo Vignetti

I thing you cannot open the same document twice.
Before open a new document you should test the word variable (if it is
nothing) and test if the document is already opened.

Fermo
 
B

Boris Drajer

Fermo, thanks for your reply. It seems I was a bit unclear in my
description. The problem is, if I open *two* (different) files, upon
closing the first I get a message like "Cannot save Normal.dot. It is
being used by another application.". I guess the other Word instance has
locked it.

I'm unsure if I should make the "word" variable static and keep a
reference to my Word instance. Could it make some kind of a garbage
collection problem or something?
 
C

Cindy M -WordMVP-

Hi Boris,

The classic-VB interface the Office applications support has a function
GetObject. Look that up in the Office help to get a firm idea how it's
used. The example will show it in combination with CreateObject, but you
can use the New keyword, instead.

You'll see examples where GetObject is used to open a file, directly. That
works great with Excel, but not so well for Word. For Word, you'd always
need to try GetObject, trap the '429' error and use New if GetObject
doesn't return an open application.

Word also has a Tasks.Exists which is pretty handy; unfortunately, Excel
does not have the same.
Fermo, thanks for your reply. It seems I was a bit unclear in my
description. The problem is, if I open *two* (different) files, upon
closing the first I get a message like "Cannot save Normal.dot. It is
being used by another application.". I guess the other Word instance has
locked it.

I'm unsure if I should make the "word" variable static and keep a
reference to my Word instance. Could it make some kind of a garbage
collection problem or something?

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

Boris Drajer

Yes, that seems to be it :-O. Obviously, I'm not very much into VBA :)

For all concerned: in C#, the code would be something like

object newInstance =
Microsoft.VisualBasic.Interaction.GetObject("filename.doc", null);

(you have to manually add a reference to Microsoft.VisualBasic.dll -
it's in the C:\WINNT\Microsoft.NET\Framework\v1.1.4322\ folder)

Now I have to do some COM Interop black magic to cast the newInstance
into something useful - but that's probably off-topic for this thread.

Thanks very much!
 

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