相关文章推荐
狂野的台灯  ·  Scala数组-CSDN博客·  4 月前    · 
风流倜傥的单杠  ·  PHP cURL: ...·  11 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to use ShellExecute to open a file in Excel. I was reading about the function on MSDN forums, and I found the folowing information about the handle, which is the first parameter:

"hwnd [in] A handle to the owner window used for displaying a user interface (UI) or error messages. This value can be NULL if the operation is not associated with a window."

I have also heard this referred to as the handle to the parent window. What is the parent/owner window? As you see below I am using NULL for the handle, but since the operation is indeed associated with a window, I probably need a handle, but I don't know what to use for the handle.

ShellExecute(NULL, "open" ,"Excel.exe", 
    "C:\\Documents and Settings\\Lab1\\My Documents\\Test.xls", 
    NULL, SW_SHOWNORMAL);
                did you try the double double quote properley ? It has actually three double quotes: " " " at the beginning and the end.
– VonC
                Nov 25, 2008 at 4:52
                If this path does exist (if you can make a 'dir' on it), what is left is to try every quote combinations you can think of ;)
– VonC
                Nov 27, 2008 at 9:39
  

hwnd : parent window that will receive a possible messagebox. This parameter is usually 0.

It refers to the top-level window: the window you are opening does not have any parent, and is the main window for the application being executed.

When you are switching between applications (ALT-TAB), you are displaying the next top-level window (the next app with a parent handle equals to 0) in the z-order (for instance).

Of course, the parent to your app can not be the Desktop Window itself:

If you create a child window whose parent is GetDesktopWindow(), your window is now glued to the desktop window. If your window then calls something like MessageBox(), well that's a modal dialog, and then the rules above kick in and the desktop gets disabled and the machine is toast.

For the path, I would advice double quotes surrounding simple quotes: " ' ... ' "

"'C:\\Documents and Settings\\Lab1\\My Documents\\Test.xls'"

Could work also (untested) with double double quotes : " "" ... "" "

"""C:\\Documents and Settings\\Lab1\\My Documents\\Test.xls"""

, as illustrated by this thread.

Actually, as mentioned in your other question by Andy and by Mesidin, and in ShellExecute Function manual, you can open the file, and pass its path in parameter.

ShellExecute( NULL, "open", 
              "Test.xls", "C:\\Documents and Settings\\Lab1\\My Documents\\", 
              NULL, SW_SHOWNORMAL);

That means Excel is the default application for opening .xls extension files though.

Thanks, that answers my question. However, when I compile the line I show above, it opens Excel, but it says it can't find the file "Test.xls". Do you have any idea why it's not working? – Shishiree Nov 24, 2008 at 21:51 If it does, it must be related with all the spaces in your path. May be adding simple quotes within your double quotes " ' ' ", or the contrary (' " ... " ') would help. Without the spaces of course: I added them to better see the quotes – VonC Nov 24, 2008 at 21:55 I tried the double double quote, and it resulted in an error. I also tried single double quote, and it had the same result as before. – Shishiree Nov 24, 2008 at 23:43 Wow, so I tried: ShellExecute(NULL, "open" ,"Test.xls", "C:\\Documents and Settings\\Lab1\\My Documents\\", NULL, SW_SHOWNORMAL); And it totally worked! Thanks for the help! – Shishiree Dec 3, 2008 at 21:41

When you see it described as the "handle to the parent window," it means it's the window that will be the parent window of whatever UI the function chooses to display. The handle you pass may or may not already be the parent of some other window at the time you call ShellExecute, but that doesn't matter.

When I invoke the "open" verb on the spreadsheet file, nothing is happening-- in fact excel doesn't even open. But maybe that is because of the spaces in the path, as was pointed out by VonC, with the thread that he linked. – Shishiree Nov 24, 2008 at 23:38

Is there any specific reason, you are using ShellExecute?

You can open an xls file using [System.Diagnostics.]Process.Start [name of the file]
I apologize, I didn't look at tags & assume that you are using .net

I am using ShellExecute simply because that was the only idea I had. I am using Borland Builder C++ – Shishiree Nov 24, 2008 at 23:39 That is not apparent, if you look at the question and tags. And thats the reason, I said - I apologize.... – shahkalpesh Nov 25, 2008 at 4:21

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.