Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Attaching to Running Process

My C# application allows the user to open a file with excel. It is
implemented like this:

Process.Start(excel.exe,FileName);

It seems to work fine, it starts Excel, which opens FileName and allows the
user to process it. The problem is that if Excel is already running, it opens
another instance of Excel, instead of adding it as another worksheet in the
existing instance, which would be faster. So I want to detect whether Excel
is running, and if it is, I want to open FileName as a new worksheet in that
instance. If anyone can suggest how to do this, I would appreciate it.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 63
Default Attaching to Running Process

Hi Richard,

I'm not sure if this will help, but is it possible to try to open the sheet
without starting a process? I am not used to C#, but using VBScript I can
create a Shell object and use something like objShell.Run and then enter the
path and filename, and then the Shell will deal with opening the file. This
way it behaves as if I double click the file itself, if Excel is not open
then it gets opened, if it is already open then the open process is used.

Sean.
--
(please remember to click yes if replies you receive are helpful to you)


"Richard MSL" wrote:

My C# application allows the user to open a file with excel. It is
implemented like this:

Process.Start(excel.exe,FileName);

It seems to work fine, it starts Excel, which opens FileName and allows the
user to process it. The problem is that if Excel is already running, it opens
another instance of Excel, instead of adding it as another worksheet in the
existing instance, which would be faster. So I want to detect whether Excel
is running, and if it is, I want to open FileName as a new worksheet in that
instance. If anyone can suggest how to do this, I would appreciate it.

  #3   Report Post  
Posted to microsoft.public.excel.programming
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.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Attaching to Running Process

Follow up: it seems that ShellExecute was what I wanted, since I did not want
DDE automation. The first problem I had with ShellExecute was that it runs by
file extension. My file was not actually an xls file, it was a prn file to be
parsed by Excel. So I used regedit to tell Windows to use Execel on prn
files. When I ran the ShellExecute there were two problems: it still started
a second copy of Excel, and it ignored the prn file and started Excel with no
document. I am going to work on it again at some point, and see if I can see
what is wrong with it. Thanks for the help.


"Jialiang Ge [MSFT]" wrote:

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.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 118
Default Attaching to Running Process

Hello,

PRN is a kind of file created when we choose "Print to file" in file Print
dialog. Based on my test, it is not a supported file format in Excel. When
I open a prn directly in Excel, it gives the the warning that "This file is
not in a recognizable format". I guess it is why ShellExecute also fails to
open it. When you have time, would you also have a try to open your PRN
directly with Excel, and let me know the result?

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

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Attaching to Running Process

When I run it with Process.Start(excel.exe,abc.prn) it works fine, it
converts the prn to a worksheet for you to edit. The only problem with it is
that it always starts a new session of Excel, which is what I had hoped to
overcome with ShellExecute().


"Jialiang Ge [MSFT]" wrote:

Hello,

PRN is a kind of file created when we choose "Print to file" in file Print
dialog. Based on my test, it is not a supported file format in Excel. When
I open a prn directly in Excel, it gives the the warning that "This file is
not in a recognizable format". I guess it is why ShellExecute also fails to
open it. When you have time, would you also have a try to open your PRN
directly with Excel, and let me know the result?

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

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 118
Default Attaching to Running Process

Hello,

I make a try to call Process.Start("excel.exe", "abc.prn") on a prn file,
but it still fails and gives the alert "The file is not in a recognizable
format". Generally speaking, the prn file created when printing is a binary
format file and Excel does not know how to read it. Would you let me know
how the prn file was created in your system? Or would you send a copy of
the prn file to my mail box , remove 'online.')
so that I could reproduce it in my side? Would you try to double click
(open) the prn file directly with Excel your Windows Explorer?

You said you might not have time for the issue in these days. Please feel
free to get back any time you want. I will always stand by and do my best
to help you.

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

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 118
Default Attaching to Running Process

Hello Rechard,

Would you also help to check if the prn file type is register to use DDE?
If it is not using DDE, the system will not know that it should use the
existing Excel process to open the workbook. About how to register a file
type with DDE, please refer to the KB article
http://support.microsoft.com/kb/122787. It introduces three ways for your
reference.

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

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Starting excel and running it on the same process C#_Programmer[_2_] Excel Programming 0 September 14th 07 01:04 AM
Excel Process Still Running DeveloperSQL Excel Programming 4 May 24th 07 06:00 PM
How to Check if a Process is Running Chaplain Doug Excel Programming 0 May 24th 05 05:29 PM
How to count process running time ( process not finished) miao jie Excel Programming 0 January 13th 05 09:23 AM
How to count process running time ( process not finished) miao jie Excel Programming 2 January 12th 05 06:01 AM


All times are GMT +1. The time now is 05:36 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"