Hi!
I run a macro in Excel by using a bat file and VBScript.
In the macro, I call up folder placement as a variable. I would like to use this variable further in VBScript. The variable in question is "myPath".
See attached code.
1. Run.bat
2. script.vbs
3. Makro.xlsm
Reference: Loop Through All Excel Files In A Given Folder — The Spreadsheet Guru
I run a macro in Excel by using a bat file and VBScript.
In the macro, I call up folder placement as a variable. I would like to use this variable further in VBScript. The variable in question is "myPath".
See attached code.
1. Run.bat
Rich (BB code):
cscript script.vbs "C:\Users\%username%\Documents\Makro.xlsm"
2. script.vbs
Rich (BB code):
Dim args, objExcel
Set args = wscript.Arguments
Set objExcel = CreateObject("Excel.Application")
objExcel.workbooks.Open args(0)
objExcel.visible = True
objExcel.Run "Macro"
objExcel.Activateworkbook.Save
objExcel.Activateworkbook.Close(0)
objExcel.Quit
I will run more code from her, but I need the variabel myPath from Excel.
3. Makro.xlsm
VBA Code:
Sub Macro()
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
'Pop-velger
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'If cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xlx*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
[B] [/B][COLOR=rgb(97, 189, 109)][B]'xlsx, this section is under construction - not the question[/B]
Workbooks.OpenText Filename:=myPath & myFile, _
Origin:=65001, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=True, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1)), TrailingMinusNumbers:=True
ActiveWorkbook.SaveAs Filename:=myPath & myFile, _
FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close[/COLOR]
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
ActiveWorkbook.Save
Application.Quit
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayAlerts = True
ActiveWorkbook.Save
Application.Quit
End Sub
Reference: Loop Through All Excel Files In A Given Folder — The Spreadsheet Guru