View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Jialiang Ge [MSFT] Jialiang Ge [MSFT] is offline
external usenet poster
 
Posts: 118
Default Attaching to Running Process

Hello,

From your post, my understanding on this issue is: you want to know how to
attach to the current running Excel process, instead of creating a new
Excel process, and open a workbook with it. If I'm off base, please feel
free to let me know.

According to the KB article http://support.microsoft.com/kb/122787:

When the user double-clicks a document, the File Manager calls
ShellExecute() with filename. ShellExecute() checks the Registration
Database for an entry that associates that file extension with a particular
application. If an entry exists and does not specify DDE commands, then
ShellExecute() launches the application as specified in the registry. If
the registry specifies to use DDE commands, ShellExecute() attempts to
establish a DDE conversation with that application using the application
topic. If an application responds to the DDE connections, *ShellExecute()
sends a DDE Execute command, as specified in the registry. It is up to the
application to define the specifics on this conversation*, particularly the
service and topic name to connect to, and also the correct DDE execute
command syntax to use. However, if attempts to establish the conversation
fail, ShellExecute() launches the application specified in the registry and
tries to establish the DDE connection again.

Excel internally receive the DDE execute command and make sure that the
workbook is open with the existing Excel process. Therefore, in order to
programmatically open a workbook and attach it to the original Excel
process, we need to use the ShellExecute API.

The KB http://support.microsoft.com/kb/170918 tells how to use ShellExecute
in .NET, and http://support.microsoft.com/kb/122787 gives an example. If
you are using C#, please refer to the following sample code:
class Program
{
public enum ShowCommands : int
{
SW_HIDE = 0,
SW_SHOWNORMAL = 1,
SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_MAXIMIZE = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_FORCEMINIMIZE = 11,
SW_MAX = 11
}

[DllImport("shell32.dll")]
static extern IntPtr ShellExecute(
IntPtr hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
ShowCommands nShowCmd);

static void Main(string[] args)
{
// Asks default mail client to send an email to the specified
address.
IntPtr result = ShellExecute(IntPtr.Zero, "open", "test.xls",
"", "", ShowCommands.SW_SHOWNORMAL);
}
}

The return value of ShellExecute indicate whether the operation fails or
not. The dictionary of all the possible return values could be found at
http://support.microsoft.com/kb/170918
Besides, the codeproject article
http://www.codeproject.com/csharp/csdoesshell2.asp gives a small class that
lets us use the function easily.

If you want to attach to the Excel process and automate it with Office
Automation (not just open a workbook), we could use Marshal.GetActiveObject
method. But it requires to reference Office Interop Assemblies. Here is a
sample code for your reference:

Microsoft.Office.Interop.Excel.Application oXL;

try
{
//Try to reuse an excel application object
oXL = Marshal.GetActiveObject("Excel.Application") as
Microsoft.Office.Interop.Excel.Application;
}
catch(Exception)
{
//Create one
oXL = new Microsoft.Office.Interop.Excel.Application();
}

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

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

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

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.