VBS to run Multiple Macros - vbscript

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

Related

Using HP-UFT, is Is there anyway to tell whether or not an object in the object repository is being used in other tests?

I'm currently using UFT -- I have a GUI test, and there's a web element object in one of my tests I'd like to delete/update, but I'm worried it's being referenced by another test in our test suite. (I am coming into a test suite that someone else built)
Is there anyway to tell whether or not an object in the object repository is being used in other tests? (Without having to go into each individual test and action to find out?)
My way would be simple recursive file search.
Open EditPlus
Search -> Find In Files
Find What =
File Type = *.mts | *.vbs | *.qfl
Folder =
Select the Include Sub Folder Check Box
Click Find
You can use Search>View>Find (or ctrl+F) from UFT and select to look in entire solution
Open "Script.mts" file from every action and search for your object name. If you find the object, write the script name and line number where your object exists, in a file.
Use the below code:
'strScriptsPath is the path where your test script folders are placed.
Set strScripts = objFSO.GetFolder(strScriptsPath).SubFolders
For Each Script In strScripts
strAction = strScriptsPath & "\" & Script.Name & "\Action1\Script.mts"
If objFSO.FileExists(strAction) Then
'Open Script in NotePad
Set strFile = objFSO.OpenTextFile(strAction, 1)
Do While Not (strFile.AtEndOfStream)
strLine = strFile.ReadLine
If InStr(1, strLine, strObjectName) > 0 Then
iVerificationCount = objSheet.UsedRange.Rows.Count
iCurrentRow = iVerificationCount + 1
objSheet.Cells(iCurrentRow, 1) = Script.Name
objSheet.Cells(iCurrentRow, 2) = strLine
If strFile.AtEndOfStream Then
objSheet.Cells(iCurrentRow, 3) = strFile.Line
Else
objSheet.Cells(iCurrentRow, 3) = strFile.Line - 1
End If
End If
Loop
strFile.Close
Set strFile = Nothing
End If
Next
Set strScripts = Nothing
To be able to use this code, declare objFSO object and write a piece of code to create an excel and get objSheet.
You can also replace the object name using the below code:
Use the For Each Loop as mentioned above
strScript = strScriptsPath & "\" & strScriptName & "\Action1\Script.mts"
strFind = "Old Object Name"
strReplace = "New Object Name"
Set strFile = objFSO.OpenTextFile(strScript, 1)
strData = strFile.ReadAll
strNewData = Replace(strData, strFind, strReplace)
strFile.Close
Set strFile = Nothing
Set strFile = objFSO.OpenTextFile(strScript, 2)
strFile.Write strNewData
strFile.Close
Set strFile = Nothing
** You just need to write this entire code in a .vbs file and run that file.

Format Function Causing Undefined Variable Error in VBA for Task Scheduling

I am new to the forum but an active searcher and reader of this site for coding solutions.
I am enclosing some code that is part of a task i have scheduled which will open a file, refresh the data (line removed), and save it with a time stamp.
Option Explicit
Dim Xlapp, Xlbook,wbnam,ext,dt
Set Xlapp = CreateObject("Excel.Application")
xlapp.visible = TRUE
Set XlBook = xlapp.Workbooks.Open("C:\Users\newtonm\Box Sync\Matthew.Newton\Campaign Deliverables\Donor Dev\Weekly 10k\WklyTest.xlsx")
wbnam = "C:\Users\newtonm\Box Sync\Matthew.Newton\Campaign Deliverables\Donor Dev\weekly 10k\wklytest"
dt = format(date,"yyyy_mm_dd")
ext = ".xlsx"
xlbook.SavecopyAs wbnam&dt&ext
xlbook.Close false
xlapp.quit
set xlbook = Nothing
set xlapp = Nothing
When I execute the code - I am getting the following error.
I am presuming that 1 of 2 things is going on
1) The syntax of the Format Function is awry or.
2) Creating the Dim for DT is missing something.
Any help for this would be quitre helpful as i have been spinning my wheels for a bit on this.

AutoExport Run Results from UFT Function

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

If folder exist create shortcut this folder in MyDocuments VBS

I want to create a shortcut in my documents only if will be existed a network share.
I'm trying to solve for a long time, but i still have problem with this.
Any help or suggestions would be greatly appreciated.
Dim strSkanSou
Dim objMyDocuments
strSkanSou = "\\Network\Scan\%username%"
IF strSkanSou.FolderExists then
Set objShell = CreateObject("WScript.Shell")
objMyDocuments = objShell.SpecialFolders("MyDocuments")
Set objLink = objShell.CreateShortcut(objMyDocuments & "\Skaner.lnk")
objLink.Description = "Skaner"
objLink.TargetPath = strSkanSou
objLink.Save
End If
You've got most of it solved already. You just need to create a FileSystemObject to check for the existence of your folder. Replace:
IF strSkanSou.FolderExists then
With:
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strSkanSou) Then
Also, I see you're using the prefixes str for string and obj for object, which is great, but you may want to use strMyDocuments instead of objMyDocuments, since this is actually a string and not an object.

Converting Microsoft XML file format to Excel file

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

Resources