-1

When I click directly in the .exe file "PrintUsers.exe", the output is correct. But when I do that through VBA using Shell the result is different. It tries to find the text file in another directory. Why? See figure:

figure

SOLUTION: I am now using: GetModuleFileName(NULL, szEXEPath, 2048) instead of GetCurrentDir(buff, FILENAME_MAX);

Community
  • 1
  • 1
dmiranda
  • 3
  • 5
  • Doesn't Look like the link went through. – JustinCoplin Dec 18 '17 at 20:23
  • what link? please add some explanation on what exactly you are trying to achieve – Yahya Hussein Dec 18 '17 at 20:24
  • What does the Active Workbook Path return? If there are spaces in it, you might have some issues. If that is the case, surround the entire string on either side with Chr(34) which corresponds to " – JustinCoplin Dec 18 '17 at 20:27
  • 1
    Why do you keep editing the question to remove the image every time I edit it to display it?!? – YowE3K Dec 18 '17 at 20:35
  • Sorry, I was updating the image to make it clearer. – dmiranda Dec 18 '17 at 20:53
  • OK - I have edited the question once more to include the picture. (Next time, you are on your own.) I notice in the new image that the program is looking in a different location for the txt file than in the original image - how does the program determine which path to look in for the file? – YowE3K Dec 18 '17 at 21:04
  • I generated the .exe using C++. The folder is not specified so I understand it looks at the same folder of the calling file. It works when executing it directly by clicking or using command prompt. I do not understand why “shell” changes de behavior of the .exe file. – dmiranda Dec 18 '17 at 21:32
  • If you believe the program is meant to find the file in the directory where the program is located, rather than just looking in the current directory, then you will need to post the source code for that program so that we can help you fix it. But, AFAIK, most languages require programs to do something special to look somewhere other than the current directory. – YowE3K Dec 18 '17 at 23:07
  • As a quick test to see whether your program (when not invoked from VBA) is looking in the current directory, or whether it is looking in the directory where the program is located, go to a command prompt and then enter `C:`, then enter `CD \ `, then enter `\Users\Douglas\University\Demo\PrintUsers.exe`, and see whether it works or not. After that, enter `CD \Users\Douglas\University\Demo` and then enter `PrintUsers.exe` and see if that works. – YowE3K Dec 18 '17 at 23:11
  • The first test you suggested did not work BUT the second worked. What`s the difference? Thanks. – dmiranda Dec 19 '17 at 11:21
  • C++ code. Even using the full path the proble is still the same. /// `char buff[FILENAME_MAX]; ` `GetCurrentDir(buff, FILENAME_MAX); ` `char* folderAddress = buff; //""; ` `std::vector vtext; ` `char * fileInstanceName = "\\doNotEdit.txt"; ` `std::string fileInstanceName_str(fileInstanceName); ` `char buffer[256]; ` `strcpy_s(buffer, folderAddress); ` `strcat_s(buffer, fileInstanceName); ` `char *filename = buffer; ` `std::ifstream ifile; ` `ifile.open(filename); ` – dmiranda Dec 19 '17 at 11:52
  • I don't know c++ but, judging by the name, the statement `GetCurrentDir(buff, FILENAME_MAX)` is getting the **current** directory (not the directory where the program is located), and you then concatenate that value with `doNotEdit.txt` to generate the filename to use. I don't know what the c++ code would be to obtain the program directory instead of the current directory so you have two choices ... (1) use the workaround in my answer to change the current directory to be the one where the program is, or (2) raise a new question asking how to change your c++ code to get the program directory. – YowE3K Dec 19 '17 at 18:49
  • It worked, thank you YowE3K. I am now using: `GetModuleFileName(NULL, szEXEPath, 2048)` – dmiranda Dec 19 '17 at 21:28

2 Answers2

1

It appears that PrintUsers.exe expects to find the file doNotEdit.txt in the current directory.

The best solution is to change that program to look for the file in the same directory as the program itself is located but, if that is not possible, get Excel to change the current directory before running the program, i.e. insert

ChDir ActiveWorkbook.Path

prior to invoking Shell.


Also, as Yahya Hussein mentioned in a comment, spaces inside paths can cause issues. There aren't any in your specific situation but, to ensure you don't have problems in future, consider using something like

myFile = """" & ActiveWorkbook.Path & "\PrintUsers.exe"""
ChDir ActiveWorkbook.Path
Shell myFile, vbNormalFocus
YowE3K
  • 23,852
  • 7
  • 26
  • 40
0

SOLUTION: I am now using: GetModuleFileName(NULL, szEXEPath, 2048) instead of GetCurrentDir(buff, FILENAME_MAX);

dmiranda
  • 3
  • 5