Can't initialize .exe properly

Greasy

Board Regular
Joined
Jan 25, 2013
Messages
55
Hey guys,

I have some code that copies some data, stores it in a text file, then calls an .exe file. I wrote the code for the executable as well, it is written in C++. The executable displays data that is in the text file then deletes the text file. When the program is complete, it won't work exactly like that but for this stage of the code that's all I need it to do.

The executable runs fine when I run it by myself, it completes the task. However, when the executable is initialized by my VBA code, the executable can't access the .txt file. Any ideas? I'm guessing maybe the .exe needs to be run as an admin or something? Is there any other way of running the .exe other than follow hyperlink?

Here is my code for reference...


Code:
VBA

Public Function ExportToText()


Dim wb As Workbook
Dim ExportFrom As Worksheet
Dim RawDataSheet As Worksheet
Dim RawData As Range
Dim LastRow
Dim newHour, newMinute, newSecond, waitTime
Dim RetVal


Set ExportFrom = Workbooks("NewDCD.xlsm").Sheets("Log")
Set RawDataSheet = Workbooks("NewDCD.xlsm").Sheets("Master")


LastRow = RawDataSheet.Range("A6:A10000").End(xlDown)


With RawDataSheet
    If .AutoFilterMode Then
        If .FilterMode Then
            .ShowAllData
        End If
    Else
        If .FilterMode Then
            .ShowAllData
        End If
    End If
End With


With RawDataSheet.Range("A6:A10000")
    .AutoFilter Field:=1, Criteria1:="<>" & "N/A", _
    Operator:=xlAnd, Criteria2:="<>" & ""
End With


LastRow = RawDataSheet.Range("A6:A10000").End(xlDown)


Set RawData = RawDataSheet.Range(("A6" & ":" & "A" & LastRow))


RawData.Copy


ExportFrom.Select
Range("A1").PasteSpecial (xlPasteValues)


'Make a copy of the sheet and create a new workbook with it to be saved
ExportFrom.Copy


'Use wb to create a pointer to the new workbook, the name of the workbook is not important
'because when it is created it automatically becomes the active workbook
Set wb = ActiveWorkbook


'Save the file as a text file
wb.SaveAs "C:\User\Temp\Test.txt", xlTextWindows


wb.Saved = True


wb.Close


newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 2
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime


RetVal = Shell("C:\User\Temp\FileDeleter.exe", 1)


End Function


And here is the code for my exe...

Code:
C++

#include <iostream>
#include <stdio.h>
#include <fstream>
#include <cstring>
#include <sstream>


using namespace std;




int main ()
{
    string myData = "";
    int x = 0;


    fstream myFile ( "Test.txt" );


    if ( myFile.is_open() )
    {


        while ( getline( myFile, myData ))
        {
        getline( myFile, myData );
        cout << myData << endl;
        }


    }
    else
    {
        cout << "An error occurred while opening the file." << endl;
        cout << "This may be caused because of an incorrect filename or directory." << endl;
    }


    myFile.close();


    if( remove( "Test.txt" ) != 0 )
    {
        for( x = 0; x == 5; x++ )
        {
            if ( remove( "Test.txt" ) != 0 )
            {
            cout << "Could not delete file." << endl;
            }
        }
    }
    else
    {
        cout << "File deleted." << endl;
    }


    cin.ignore();


  return 0;
}


Any and all help is appreciated, thanks in advance!</sstream></cstring></fstream></stdio.h></iostream>
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Does it help if you include the full path to the file in the .exe, or pass the file path as a parameter?
 
Upvote 0
Does it help if you include the full path to the file in the .exe, or pass the file path as a parameter?

Good idea,

I tried including the full path inside the C++, but I didn't try passing the file path as a parameter from Excel. How would I go about doing that?
 
Upvote 0
SOLVED:

In C++ you have to use "\\" rather than "\" in the file path, when I tried the file path earlier in C++ it didn't work because I only used one. Thanks for the help!
 
Upvote 0

Forum statistics

Threads
1,203,485
Messages
6,055,686
Members
444,807
Latest member
RustyExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top