I have requirement that I'm not really sure on how to go about it. I have a file Doc.xml, this is in Microsoft XML format. I need to create a VB script that will change/convert the Doc.xml to Doc.xlsx, so when the user tries to open the file it will open as an Excel file.
One of the requirements is that this script will be run from the Windows Scheduler.
Any ideas or recommendation will be really appreciated.
This is the script I created and is working, but when I try to change the SaveAs extension to ".csv" the file is not being saved correctly. I guess I need to find out what the code is for saving in CSV.
Dim objXLApp, objXLWb, objXLWs
Set objXLApp = CreateObject("Excel.Application")
objXLApp.Visible = True
Set objXLWb = objXLApp.Workbooks.Open("C:\Users\jmejia\Desktop\XML_F\ZOOSHR_130622.xml")
'Do nothing with File, just open it to be save agains as a new file format
objXLWb.SaveAs "C:\Users\jmejia\Desktop\XML_F\ZOOSHR_130622.xlsx", 51
objXLWb.Close (False)
Set objXLWs = Nothing
Set objXLWb = Nothing
objXLApp.Quit
Set objXLApp = Nothing
If your file was created and exported from Excel > 2006? then it will have the tags in it such that double clicking in explorer on a windows machine with any Excel that supports xml format will automatically open it in Excel.
Your file is likely to start with something like:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
Const xlXLSX = 51
REM 51 = xlOpenXMLWorkbook (without macro's in 2007-2013, xlsx)
REM 52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2013, xlsm)
REM 50 = xlExcel12 (Excel Binary Workbook in 2007-2013 with or without macro's, xlsb)
REM 56 = xlExcel8 (97-2003 format in Excel 2007-2013, xls)
dim args
dim file
dim sFile
set args=wscript.arguments
dim wshell
Set wshell = CreateObject("WScript.Shell")
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open( wshell.CurrentDirectory&"\"&args(0))
objExcel.DisplayAlerts = FALSE
objExcel.Visible = FALSE
objWorkbook.SaveAs wshell.CurrentDirectory&"\"&args(1), xlXLSX
objExcel.Quit
Wscript.Quit
Related
I am using this VBS to run a macro on an excel document that has several macros in it. Is there a way to run more than one macro on a single VBS or will I have to create several?
This is the code I am using.
strPath = "C:\Users\michael\Desktop\sced.xlsm"
strMacro = "Macro3"
Set objApp = CreateObject("Excel.Application")
objApp.Visible = True
Set wbToRun = objApp.Workbooks.Open(strPath)
objApp.Run strMacro
wbToRun.Save
wbToRun.Close
objApp.Quit
I was thinking that I would just be able to list the macros;
strMacro = "Macro3"
strMacro = "Macro4"
but it only runs the last macro on the list.
Thanks in advance.
Simplest solution for your needs is this:
strMacro1 = "Macro3"
strMacro2 = "Macro4"
strPath = "C:\Users\michael\Desktop\sced.xlsm"
Set objApp = CreateObject("Excel.Application")
objApp.Visible = True
Set wbToRun = objApp.Workbooks.Open(strPath)
objApp.Run strMacro1
objApp.Run strMacro2
wbToRun.Save
wbToRun.Close
objApp.Quit
I must create a text file with ANSI encoding using vbs.
If I just create it, without to write any text, using this code...
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFileTxt = objFSO.objFSO.CreateTextFile("filename.txt", True, False)
Set ObjFileTxt = nothing
Set objFSO = nothing
... I get an ANSI text file, but if I try to write some text, for example...
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFileTxt = objFSO.CreateTextFile("filename.txt", True, False)
ObjFileTxt.Write "ok"
Set ObjFileTxt = nothing
Set objFSO = nothing
... I get an UTF8 text file.
Any help?
It's not possible that the code you posted would create a UTF8-encoded output file. The CreateTextFile method supports only ANSI (windows-1252) and Unicode (little endian UTF-16) encodings.
The only ways to create UTF8-encoded files in VBScript are the ADODB.Stream object:
Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type = 2 'text
stream.Position = 0
stream.Charset = "utf-8"
stream.WriteText "some text"
stream.SaveToFile "C:\path\to\your.txt", 2
stream.Close
or writing the individual bytes (including the BOM).
I know this is already old, but for future references...
Believe it or not, I had the same problem until I converted my VBS to ANSI (was UTF-8). Then, every new text file created with it was also ANSI and not UTF-8 any more.
I am running an automated test script using UFT 12.52. I am wondering if there is a way to export results from within a function in the UFT Script. The idea is to call the function and export the run results.
I can do it externally by creating a .vbs file which launches the script in uft and runs and exports the result, but i cannot figure out how to do it from within a UFT Script as function.
Below is my code for exporting results externally.
Thanks
Dim qtApp
Dim qtTest
Dim qtResultsOpt
Dim qtAutoExportResultsOpts
Set qtApp = CreateObject("QuickTest.Application")
qtApp.Launch
qtApp.Visible = True
qtApp.Options.Run.ImageCaptureForTestResults = "OnError"
qtApp.Options.Run.RunMode = "Fast"
qtApp.Options.Run.ViewResults = False
qtApp.Open "Z:\D:\paperlessEnhancements\", True
Set qtTest = qtApp.Test
qtTest.Settings.Run.IterationMode = "rngIterations"
qtTest.Settings.Run.StartIteration = 1
qtTest.Settings.Run.EndIteration = 1
qtTest.Settings.Run.OnError = "NextStep"
Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions")
qtResultsOpt.ResultsLocation = "C:\Tests\Test1\Res1" n
Set qtAutoExportResultsOpts = qtApp.Options.Run.AutoExportReportConfig
qtAutoExportResultsOpts.AutoExportResults = True
qtAutoExportResultsOpts.StepDetailsReport = True
qtAutoExportResultsOpts.DataTableReport = True
qtAutoExportResultsOpts.LogTrackingReport = True
qtAutoExportResultsOpts.ScreenRecorderReport = True
qtAutoExportResultsOpts.SystemMonitorReport = False
qtAutoExportResultsOpts.ExportLocation =
"C:\Documents and Settings\All Users\Desktop"
qtAutoExportResultsOpts.UserDefinedXSL = "C:\Documents and Settings\All
Users\Desktop\MyCustXSL.xsl"
qtAutoExportResultsOpts.StepDetailsReportFormat = "UserDefined"
qtAutoExportResultsOpts.ExportForFailedRunsOnly = True
qtTest.Run qtResultsOpt
MsgBox qtTest.LastRunResults.Status
qtTest.Close
Set qtResultsOpt = Nothing
Set qtTest = Nothing
Set qtApp = Nothing
Set qtAutoExportSettings = Nothing
I also tried this :
Dim qtResultsOpt
Dim qtAutoExportResultsOpts
Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions")
qtResultsOpt.ResultsLocation = "C:\Temp\Notepad1"
Set qtResultsOpt = Nothing
#Lukeriggz :Attach a function library to all your script and the function library should be called in first place in your script(Either you can call the lines in your current library function itself. But the significance is to set the attribute at the first place and start with the execution). The content of the library should be the one which you have shown the code except the Open,run statement and releasing the objects(Primarily the configuration statements should be there). This will make your result location always pointed to your desired path and you can view the results. While configuration of the script have the script name in a variable to create the result file name to act is as dynamic
Another Implementation
We can easily identify where the results are getting saved Using the inbuilt environment variable. So programmatically we can copy the folder using file system objects
enter code here
executionpath=Environment.Value("ResultDir")
path_to_save_the_results= "Type your path where the results should be saved"
fso.CopyFolder executionpath, path_to_save_the_results
I'm using the following script to convert an Excel file into a CSV tab delimited txt file.
xls = "C:\Ristken Data Load\Wade SPIFF log file"
csv = "c:\Ristken Data Load\Wade SPIFF log file"
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(xls)
oBook.Worksheets(2).Activate
oBook.Worksheets(2).Rows("1:4").Delete
oBook.SaveAs csv, -4158
oBook.Close True
oExcel.Quit
I'd like to add something to this script so that, it overwrites the txt file each time it runs without having the popup box asking if you want to overwrite the existing file.
Easier then I thought it would be.
xls = "C:\Ristken Data Load\Wade SPIFF log file"
csv = "c:\Ristken Data Load\Wade SPIFF log file"
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(xls)
oBook.Application.DisplayAlerts = False
oBook.Worksheets(2).Activate
oBook.Worksheets(2).Rows("1:4").Delete
oBook.SaveAs csv, -4158
oBook.Close True
oExcel.Quit
First, I need to delete the top 4 rows from the sheet either before or after the conversion. There currently isn't anything in this script that will delete rows from the Excel file or from the CSV file that it creates.
Second, I'd prefer to pass the source and destination in this script rather then passing them later. Currently this script requires a command line to pass the source and destination it looks something like this.
C:\exceltocsv "source.xls" "destination.csv"
Instead of requiring source.xls and destination.csv to be provided as commandline arguments I'd rather have them resolved in the VBScript itself. Is this possible?
if WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.SaveAs WScript.Arguments.Item(1), 6
oBook.Worksheets(2).Activate
oBook.Close False
oExcel.Quit
WScript.Echo "Done"
You can use the InputBox function to prompt for user input. Rows can be removed from an Excel worksheet via <range>.EntireRow.Delete.
Something like this should do what you want:
xls = InputBox("Enter source file.")
csv = InputBox("Enter destination file.")
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(xls)
oBook.Sheets(1).Range("1:4").EntireRow.Delete
oBook.SaveAs csv, 6
oBook.Close False
oExcel.Quit
WScript.Echo "Done"
Edit: If you want hardcoded paths, simply define them as strings:
xls = "C:\path\to\input.xls"
csv = "C:\path\to\output.csv"
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(xls)
oBook.Sheets(1).Range("1:4").EntireRow.Delete
oBook.SaveAs csv, 6
oBook.Close False
oExcel.Quit
WScript.Echo "Done"
Thanks to Ansgar Wiechers I was able to come up with this script
xls = "C:\[Path]"
csv = "c:\[Destination]"
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(xls)
oBook.Worksheets(2).Activate
oBook.Worksheets(2).Rows("1:4").Delete
oBook.SaveAs csv, 6
oBook.Close False
oExcel.Quit
And it runs like a champ in cmd. Thanks a million!
However, I went to implement this into my process and found out that SQL Server Agent has major issues in executing VBScripts.
cscript is the command that is suggested to execute the script, but it doesn't work. After more research I found that other people have had similar issues and they all suggest writing the script in VB.NET instead of VBScript. This way SSIS can process the script.
Ansgar I marked your response as the answer since it answered my question, but sadly it only lead me to a dead end with VBScript.