Results 1 to 5 of 5

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

Threaded View

  1. #1
    Registered User
    Join Date
    12-17-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    8

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

    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
    _______________________________________________________________________________________________
    Last edited by CSLim; 06-22-2014 at 10:50 PM.
    Best Regards,
    CSLim

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Open files from FTP server (works with "servername" but doesn't work with "ip address")
    By adammsu1983 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-24-2012, 04:30 PM
  2. Displaying "All Files" instead of "All Excel Files"
    By Ernesto in forum Excel General
    Replies: 0
    Last Post: 07-12-2012, 11:06 AM
  3. search directory for excel-files and put A136 from tab "1"-"20" in a table
    By Wim_VDW in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-30-2012, 10:33 AM
  4. Placing an "Execute" button in Excel...
    By ltipto1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2009, 04:47 AM
  5. Placing an "Execute" button on Excel ...
    By ltipto1 in forum Excel General
    Replies: 1
    Last Post: 06-18-2009, 09:25 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1