View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jialiang Ge [MSFT] Jialiang Ge [MSFT] is offline
external usenet poster
 
Posts: 118
Default More about trying to automation multiple Excel instances

Good afternoon, Sergei. Welcome to Microsoft Newsgroup Support Service! My
name is Jialiang Ge [MSFT]. It's my pleasure to work with you in this issue.

According to the post, you wonder how to automate a specific instance of
Excel. As far as I know, there are at least two ways that meet the
requirement. Sergei, you mentioned the "impossibility to automate multiple
Excel Instances". Would you mind sharing me with these posts? There might
be some special limits that I am not aware of. Thanks.

For your first question, all the Excel instances are registered in ROT
(Running Object Table). The instance that is first registered in the ROT is
typically the instance that is returned by GetActiveObject. If you want to
get an Automation Reference to a specific running instance of Word, Excel,
or Microsoft Access, we can consider
Method 1. use BindToMoniker with the name of the file that is opened in
that instance.
- or -
Method 2. IRunningObjectTable::EnumRunning

Please allow me to demonstrate these two methods one by one:

*Method 1. Use BindToMoniker with the name of the file that is opened in
that instance.*
There is a very good KB article that provides examples of BindToMoniker for
Office and explains the Multiuse and Singe Use mechanisms of Office
products to help you have a clearer picture of COM and ROT:

How to use Visual C# to automate a running instance of an Office program
http://support.microsoft.com/kb/316126

I also write a code snippet for your reference:
(Suppose two instances of excel.exe have loaded Book1.xls and Book2.xls
repectively)
The code sets a value to the workbooks in different instances of Excel.

[STAThread] //STA thread declaration is necessary.
static void Main(string[] args)
{
Excel.Workbook xlwkbook1, xlwkbook2;
Excel.Worksheet xlsheet1, xlsheet2;

//Get a reference to the Workbook object by using a file
moniker.
//The xls was saved earlier with this file name.
xlwkbook1 =
(Excel.Workbook)Marshal.BindToMoniker(@"D:\Test\Bo ok1.xls");
xlwkbook2 =
(Excel.Workbook)Marshal.BindToMoniker(@"D:\Test\Bo ok2.xls");

xlsheet1 = (Excel.Worksheet)xlwkbook1.ActiveSheet;
xlsheet1.Cells[1, 1] = 111;

xlsheet2 = (Excel.Worksheet)xlwkbook2.ActiveSheet;
xlsheet2.Cells[1, 1] = 222;
}

For more readings about moniker, I recommend the book "Essential COM" by
DON BOX. In its Chapter 3, "Classes", Moniker is introduced in detail.

*Method 2. IRunningObjectTable::EnumRunning*
I searched online, and also found a very good article for EnumRunning.
Although the article is for "automating a specific instance of Visual
Studio.net", the principle is the same for Office.
http://www.codeproject.com/KB/cs/aut...ualstudio.aspx
If you encounter any problem when converting its code to the use of Office,
please don't hesitate to let me know.

For your second question:
"Can we at least adjust somehow Excel behavior (using its own settings) for
it to start as single instance (like Word)?"

According to the KB article http://support.microsoft.com/kb/316126, Excel
is designed as a Single Use (Multiple Instances) server:
<quote
Multiple instances of Word (Winword.exe), Excel (Excel.exe), and Microsoft
Access (MSAccess.exe) can run simultaneously. Therefore, these servers are
defined as Single Use (Multiple Instances) servers. Only one instance of
PowerPoint (Powerpnt.exe) can run at any given time. Therefore, PowerPoint
is a Multiuse (Single Instance) server.
</quote
Therefore, as far as I know, there is no option to allow only one instance
of Excel in the machine. Even for Word, we are still able to create
multiple winword.exe instances by calling CreateObject("Word.Application")
for serveral times. Sergei, I will confirm this point with the product team
and get to you soon.

Please let me know if you have any other concerns, or need anything else.

Regards,
Jialiang Ge , remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.