Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using ShellExecute() to Open a File in Excel 1

Status
Not open for further replies.

Shawnasie

Programmer
Aug 19, 2008
23
US
I am using Borland Builder C++ 2009. I want to open a particular file from Microsoft Excel during runtime. The following line of code opens Excel, but there's a message saying it cant find the file "Tool.xls" -- I have tried giving it the entire path for the file, and it still can't find it. Does anyone know how ShellExecute() works? I am wondering if I have filled the parameters correctly.

Code:
ShellExecute(NULL, "open" ,"Excel.exe", "C:\\Documents and Settings\\Lab1\\My Documents\\Tool.xls", NULL, SW_SHOWNORMAL);
 
Did you look at faq101-1952 and faq101-1953?


James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Thank you for those links. I had not read those, but I read about ShellExecute() on MSDN after posting this, and it was basically the same thing.

I am doing just what one of the examples said, but it still says it cannot find the file.
 
This is odd though -- when I put in the file name "Calculate Distance.xls", which is another file in excel that I have, it says that it cannot find "Calculate.xls". Then another box opens that says it cannot find "Distance.xls". Basically, it is reading them as two different files.
 
It just occured to me that I put in NULL for the handle. The link you gave me from the FAQs says that it should be the handle for the parent window -- what does that mean?
 
It just occured to me that I put in NULL for the handle. The link you gave me from the FAQs says that it should be the handle for the parent window -- what does that mean?
In Windows, every window has a name or handle. For ShellExecute it's usually not necessary, handle or NULL should work. If you want to try it, you can try
Code:
HWND MyFileHandle = Application->Handle;

What happens when you do
Code:
ShellExecute(handle, "open";, "Tool.xls", NULL, "C:\\Documents and Settings\\Lab1\\My Documents", SW_SHOWNORMAL);
or
Code:
ShellExecute(NULL, "open";, "Tool.xls", NULL, "C:\\Documents and Settings\\Lab1\\My Documents", SW_SHOWNORMAL);

These would use the default program for opening up an excel file.


James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Thanks, 2ffat. I tried both lines you gave. It doesn't do anything at all. Before, with the original line, it would at least open Excel. But if I take out "Excel.exe" it doesn't do anything.
 
Try something like this:
Code:
ShellExecute(handle, "open";, "Tool.xls", NULL, "\"C:\\Documents and Settings\\Lab1\\My Documents\"", SW_SHOWNORMAL);

James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Try:

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

Also, this would only work if Excel is the default program to open XLS files.
You could also use "short" filenames...
C:\Documen~1\Lab1\My~1\Tool.xls

not sure if it's supposed to be My~1 or MyDocu~1
see if any work, I don't have BC++ here.
 
Thank you everyone for the suggestions -- I can't tell you how much I appreciate having something to try. I am running out of ideas. So far nothing has worked. So I am looking into other options. Happy Thanksgiving!
 
You could always look into custom made Components, I know there is one called AppExec for Delphi, but I'm not sure it exists for C++B, it's at Torrys.
 
I don't understand why it won't work. Here is a code snippet from one of my programs and it works.
Code:
    String DriveStr = (String) C4DriveComboBox->Drive; // Drive letter
    String DirectoryStr = C4DirectoryListBox->Items->Strings[C4DirectoryListBox->ItemIndex];
    String SelectedFileStr = C4FileListBox->Items->Strings[C4FileListBox->ItemIndex]; // Selected file
    String WorkStr;
    if (DirectoryStr.Pos(":") == 0) WorkStr = DriveStr + ":\\" + DirectoryStr;// Directory if only drive letter is picked
    else WorkStr = DirectoryStr;// Directory

    if (WorkStr.Length() > 0) ShellExecute(NULL, NULL, SelectedFileStr.c_str(), NULL, WorkStr.c_str(), SW_SHOWNORMAL);

I use the program at least once a week. It opens a selected file using the file's default application.

What happens when you do this?
Code:
AnsiString FileNameStr = "Tool.xls";
AnsiString DirecStr = "C:\\Documents and Settings\\Lab1\\My Documents";

ShellExecute(NULL, NULL, FileNameStr.c_str(), NULL, DirecStr.c_str(), SW_SHOWNORMAL);


James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
What happens when you do this?

Code:
AnsiString FileNameStr = "Tool.xls";
AnsiString DirecStr = "C:\\Documents and Settings\\Lab1\\My Documents";

ShellExecute(NULL, NULL, FileNameStr.c_str(), NULL, DirecStr.c_str(), SW_SHOWNORMAL);

Absolutely nothing. I tried even that third line you gave with "open" instead of "NULL" in the second parameter and it still didn't work. Also, I tried as before with "excel" in the third parameter, "Tool.xls" in the fourth, and the rest as you gave me-- it opens excel but says that it cannot find "Tool.xls".
 
Thank you everyone for the suggestions!! I got it to work finally, and I feel silly because it is sooo simple:

Code:
ShellExecute(NULL, "open" ,"Waypoint Tool.xls", "C:\\Documents and Settings\\Lab1\\My Documents\\", NULL, SW_SHOWNORMAL);
 
I also should note that I added the excel file to the project-- that might have helped.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top