Calling a function in another file in VB Script - vbscript

I am using a Test Driver script (testset Driver.vbs) in QTP and in it I am trying to call a function in another file. I thought I could add this to the testset driver.vbs:
Function IncludeAOA
Dim objFSO, objFile, AR1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("\\Server1\QTP Files\Community\Driver Scripts\AOAReg.vbs", 1)
AR1 = objFile.ReadAll
objFile.Close
ExecuteGlobal AR1
End Function
I call the IncludeAOA function which seems to work. But, when it gets to the ExecuteGlobal AR1 line it fails with an
Error: Invalid Character, Code: 800A0408.
Can anyone see what I missed?

AR1 - i.e. the code in AOAReg.vbs - is to blame. Try to 'run' it with cscript AOAReg.vbs. If that does not show the error (including the line number), post the code or check the encoding of that file.

Related

Trying to run an Excel macro via vbs script

The vbscript below is intended to trigger a macro named "Test" in the file listed in the script. It follows examples I've seen on this site, but it does not run the macro. Probably a syntax issue. Maybe related to the desktop where the file is. Any help is appreciated.
On Error Resume Next
ExcelMacroExample
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Users\smithj01\Desktop\Alteryx Demo\'Command Line Test.xlsm'", 0, True)
xlApp.Run "Test"
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
Set objFSO = Nothing
End Sub

vbs(25, 3) Microsoft VBScript runtime error: Invalid procedure call or argument

I wrote a script to grab the list for folders from a file and it will check and delete for files that are more than 90 days old.
The script was able to delete the files older than 90 days. However I keep on getting an error saying:
D:\cleanup90days.vbs(25, 3) Microsoft VBScript runtime error: Invalid procedure call or argument
I don't have an idea what I have missed. Any help will be appreciated.
Below is my script:
Dim days
Dim inputFolderList, ObjFolder, Files, objFileAge
If Not WScript.Arguments.Count = 2 Then
Wscript.Echo "Invalid number of arguments. Arg1: Daily or Weekly. Arg2: Remove all files older then this"
WScript.Quit(-1)
End If
days = WScript.Arguments.Item(1)
inputFileList = "D:\FileGrep2.txt"
Set Fso = CreateObject("Scripting.FileSystemObject")
Set objTextFile = fso.OpenTextFile(inputFileList, 1)
Do Until objTextFile.AtEndOfStream
sFolderName = objTextFile.ReadLine
getfoldernames(sFolderName)
Loop
Function getfoldernames(sFolderName)
Set ObjFolder = fso.GetFolder(sFolderName)
Set Files = ObjFolder.Files
For Each Check In Files
objFileAge = DateDiff("n", Check.DateLastModified, Now)
If objFileAge > 90 Then
WScript.Echo Now & "the following will be deleted " & Check.Path
Check.Delete
End If
Next
End Function
Probably, you've got an empty line in your input file "D:\FileGrep2.txt" causing the Set ObjFolder = fso.GetFolder(sFolderName) line throwing this error.

VBScript create and open a new file

I try to make a script in VBScript for PowerAMC.
And I've got an error.
I checked all elements to make a file with the content (XSD file):
private Sub writeInFile(pathFolder, pathFile, val)
Output "WriteInFile["&pathFolder&pathFile&"]"
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile(pathFolder&pathFile, true)
If (fso.FileExists(pathFolder&pathFile)) Then
MyFile.WriteLine(val)
Else
ouput "File can't be create"
End If
MyFile.Close
end Sub
And the file exists with good content, but if I try to read it with:
public Function readFile(path)
'Declare variables
Dim objFSO, objReadFile, contents
'Set Objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objReadFile = objFSO.OpenTextFile(path, 1, false)
'Read file contents
contents = objReadFile.ReadAll
'Close file
objReadFile.close
'Cleanup objects
Set objFSO = Nothing
Set objReadFile = Nothing
readFile = contents
End Function
I get that : "ÿþ<" for only content, but if I try to read a file that is not created by the previous function, it runs perfectly.
I think the problem comes from Unicode format,
take a look at this => FileSystemObject - Reading Unicode Files

Error with Loop and recursive function

I am trying to create a VbScript file that will read a text file that has a list of folder names in it.
From these folder names I need to create a second text file that prints out all the files with a specific extension.
I have used this code to do the second part of the task
Option Explicit 'force all variables to be declared
Const ForWriting = 2
Dim objFSO 'File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objTS 'Text Stream Object
Set objTS = objFSO.OpenTextFile("C:\Output.txt", ForWriting, True)
Call Recurse("C:\")
objTS.Close()
Sub Recurse(strFolderPath)
Dim objFolder
Set objFolder = objFSO.GetFolder(strFolderPath)
Dim objFile
Dim objSubFolder
For Each objFile In objFolder.Files
'only proceed if there is an extension on the file.
If (InStr(objFile.Name, ".") > 0) Then
'If the file's extension is "pfx", write the path to the output file.
If (LCase(Mid(objFile.Name, InStrRev(objFile.Name, "."))) = ".exe") Then _
objTS.WriteLine(objfile.Path)
End If
Next
For Each objSubFolder In objFolder.SubFolders
Call Recurse(objSubFolder.Path)
Next
End Sub
I have tried to put this in a loop but when I do I get a syntax error for this line Sub Recurse(strFolderPath)
Any help you can give me would be appreciated
One interpretation of
I have tried to put this in a loop but when I do I get a syntax error
for this line Sub Recurse(strFolderPath)
is that the structure of your resulting script looks like:
Do Until tsIn.AtEndOfStream
p = tsIn.ReadLine
Sub Recurse(p)
End Sub
Call Recurse(p)
Loop
output:
cscript 27537600-B.vbs
..\27537600-B.vbs(3, 4) Microsoft VBScript compilation error: Syntax error
VBScript does not allow nested Sub/Function definitions, especially in loops (you may get away with mixing simple statements and Sub/Function definitions in top-level code, but that's more a bug than a feature). If you re-structure the script like
Do Until tsIn.AtEndOfStream
p = tsIn.ReadLine
Call Recurse(p)
Loop
Sub Recurse(p)
End Sub
you won't get a syntax error on the Sub line.

Overriding CreateObject Function in VBScript

I want to override the default CreateObject() function in VBScript with my own.
Basically this example in VB6:
http://www.darinhiggins.com/the-vb6-createobject-function/
I cannot figure out is this line:
Set CreateObject = VBA.CreateObject(Class$, ServerName$)
How do I refer to "VBA" in VBSript?
This quick test seems to work...
Function CreateObject(className, serverName)
'---- override the CreateObject
' function in order to register what
' object is being created in any error message
' that's generated
Dim source, descr, errNum
WScript.echo "In custom CreateObject"
If Len(serverName) > 0 Then
Set CreateObject = WScript.CreateObject(className, serverName)
Else
Set CreateObject = WScript.CreateObject(className)
End If
End Function
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject", "")
path = fso.GetAbsolutePathName(".")
WScript.echo path
No guarantees! ;-)
I don't think you can override it so that all code will use it, only YOUR code.
In which case, it doesn't matter what it's called (unless you have tons of existing code you can't change). Can you call it CreateObjectEx() or ExCreateObject() or something like that? Have this function add all your error handling and such and then turn around and call the main/core CreateObject() method

Resources