Hi,
I have been struggeling now with an issue that I cannot understand.
I have a VBA macro that I need to run from a VB Script. The Macro reades from a file, copies into a spreadsheet, copies to another workbook and then saves it to a csv file.
If I only run the VBA macro within excel, all works fine. But if I call the VBScript, it seems to perform only parts of the script,not writing .
I post my code here , grateful for any feedback on this issue.
Best, Mia
VBScript:
---------
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open("C:\testData.xlsm")
objExcel.DisplayAlerts = False
objExcel.Application.Visible = True
objExcel.sheets("sheet1").range("A1").activate
objExcel.Application.Run "Module4.OpenUpdateCopy()"
For Each objWrkBook in objExcel.Workbooks
objWrkBook.Saved = True
objWrkBook.Close
Next
objExcel.Application.Quit
objExcel.Application.DisplayAlerts = True
objExcel.Quit
Set objExcel = Nothing
------
VBA macro called from the script:
------
Sub OpenUpdateCopy()
Dim FilePath As String
FilePath = "C:\Input_mini.csv"
Open FilePath For Input As #1
row_number = 0
Worksheets("Sheet1").Columns(1).ClearContents
Worksheets("Sheet1").Columns(2).ClearContents
Worksheets("Sheet1").Activate
Range("A1").Activate
Range("A1").Selecte
Do Until EOF(1)
Line Input #1, LineFromFile
Items = Split(LineFromFile, ";")
ActiveCell.Offset(row_number, 0).Value = Items(0)
ActiveCell.Offset(row_number, 1).Value = Items(1)
row_number = row_number + 1
Loop
Close #1
' part two
Set read_range = Sheets("Sheet1").Range("B1")
Set Write_range = Sheets("Sheet1").Range("C1")
Dim Name As String
i = 0
While Not read_range.Offset(i, 0).Value = ""
Name = CStr(read_range.Offset(i, 0).Value)
Write_range.Offset(i, 0).Value = """" & Name & """"
i = i + 1
Wend
Dim Pathname As String
Pathname = "C:\List.csv"
Set NewBook = Workbooks.Add
Workbooks("testData.xlsm").Worksheets("Sheet1").Range("A:C").Copy
NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial (xlPasteValues)
'Application.DisplayAlerts = False
'With ActiveWorkbook
NewBook.SaveAs Filename:=Pathname, FileFormat:=xlCSV
NewBook.Close SaveChanges:=True
'End With
'Workbooks("testData.xlsm").Close SaveChanges:=False
'Application.DisplayAlerts = True
End Sub
I have been struggeling now with an issue that I cannot understand.
I have a VBA macro that I need to run from a VB Script. The Macro reades from a file, copies into a spreadsheet, copies to another workbook and then saves it to a csv file.
If I only run the VBA macro within excel, all works fine. But if I call the VBScript, it seems to perform only parts of the script,not writing .
I post my code here , grateful for any feedback on this issue.
Best, Mia
VBScript:
---------
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open("C:\testData.xlsm")
objExcel.DisplayAlerts = False
objExcel.Application.Visible = True
objExcel.sheets("sheet1").range("A1").activate
objExcel.Application.Run "Module4.OpenUpdateCopy()"
For Each objWrkBook in objExcel.Workbooks
objWrkBook.Saved = True
objWrkBook.Close
Next
objExcel.Application.Quit
objExcel.Application.DisplayAlerts = True
objExcel.Quit
Set objExcel = Nothing
------
VBA macro called from the script:
------
Sub OpenUpdateCopy()
Dim FilePath As String
FilePath = "C:\Input_mini.csv"
Open FilePath For Input As #1
row_number = 0
Worksheets("Sheet1").Columns(1).ClearContents
Worksheets("Sheet1").Columns(2).ClearContents
Worksheets("Sheet1").Activate
Range("A1").Activate
Range("A1").Selecte
Do Until EOF(1)
Line Input #1, LineFromFile
Items = Split(LineFromFile, ";")
ActiveCell.Offset(row_number, 0).Value = Items(0)
ActiveCell.Offset(row_number, 1).Value = Items(1)
row_number = row_number + 1
Loop
Close #1
' part two
Set read_range = Sheets("Sheet1").Range("B1")
Set Write_range = Sheets("Sheet1").Range("C1")
Dim Name As String
i = 0
While Not read_range.Offset(i, 0).Value = ""
Name = CStr(read_range.Offset(i, 0).Value)
Write_range.Offset(i, 0).Value = """" & Name & """"
i = i + 1
Wend
Dim Pathname As String
Pathname = "C:\List.csv"
Set NewBook = Workbooks.Add
Workbooks("testData.xlsm").Worksheets("Sheet1").Range("A:C").Copy
NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial (xlPasteValues)
'Application.DisplayAlerts = False
'With ActiveWorkbook
NewBook.SaveAs Filename:=Pathname, FileFormat:=xlCSV
NewBook.Close SaveChanges:=True
'End With
'Workbooks("testData.xlsm").Close SaveChanges:=False
'Application.DisplayAlerts = True
End Sub