How to execute the ".bat" files using excel VBA !?

CSLim

New Member
Joined
Jun 19, 2014
Messages
2
Dear all,

I was created a macro VBA to run ".BAT" files so I can copy some documents with different formats into excel.
When I try on my ".BAT" files created by execute it manually was running fine without any problems.
However, I was failed to get my results when I tried to execute the ".BAT" files with excel macro VBA.

Can anyone helps me on this? Below is my VBA codes created.

Thanks in advance.

__________________________________________________________________________
Option Explicit

#If VBA7 Then
Private Declare PtrSafe Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long

Private Declare PtrSafe Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, _
lpExitCode As Long) As Long
#Else
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long

Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, _
lpExitCode As Long) As Long
#End If

Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const STILL_ACTIVE = &H103

Sub LF_Click()
Dim BatFileName As String
Dim BatTxtTExcel As String
Dim NMFFileName As String
Dim XLSFileName As String
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim DefPath As String
Dim BatPath As String
Dim Wb As Workbook
Dim oApp As Object
Dim oFolder
Dim foldername

'Create two temporary file names
BatFileName = DefPath & _
"\CollectNMFData" & Format(Now, "dd-mm-yy-h-mm-ss") & ".bat"
BatTxtTExcel = DefPath & _
"\NMFDataText2Excel" & Format(Now, "dd-mm-yy-h-mm-ss") & ".bat"
NMFFileName = DefPath & _
"\AllTXT" & Format(Now, "dd-mm-yy-h-mm-ss") & ".txt"

'Name of the Excel file with a date/time stamp
XLSFileName = DefPath & "MasterTXT " & _
Format(Now, "dd-mmm-yyyy h-mm-ss") & FileExtStr

'Path to save temp bat & files
DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If

'Browse for the NMF file needed
Set oApp = CreateObject("Shell.Application")
Set oFolder = oApp.BrowseForFolder(0, "Select the NMF files", 512)
If Not oFolder Is Nothing Then
foldername = oFolder.Self.Path
If Right(foldername, 1) <> "\" Then
foldername = foldername & "\"
End If

'Declare a variable as a FileDialog object.
Dim fd As FileDialog
'Create a FileDialog object as a File Selection dialog box.
Set fd = Application.FileDialog(msoFileDialogOpen)

With fd
.FilterIndex = 1
.AllowMultiSelect = False
.InitialFileName = foldername 'Worksheets("NMF_Checking").Range("D2").Value
If .Show <> -1 Then
'The user pressed Cancel.
Set fd = Nothing
Exit Sub
End If
End With

BatPath = "C:\"
'Create the bat file
Open BatFileName For Output As #1
Print #1, "cd .." & BatPath
Print #1, "call ." & BatTxtTExcel & " " & fd.SelectedItems(1)
Close #1
Open BatTxtTExcel For Output As #2
Print #2, "@echo off"
Print #2, "copy %1 %1.xls > nul"
Print #2, "excel %1.xls"
Close #2

Dim RunBat As Double
Dim sYourCommand As String
sYourCommand = BatFileName
RunBat = Shell("cmd /c " & sYourCommand, vbNormalFocus)

End Sub
_______________________________________________________________________________________________​

:(:(:(
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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