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...
And here is the code for my exe...
Any and all help is appreciated, thanks in advance!</sstream></cstring></fstream></stdio.h></iostream>
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>