I try to create object in UFT:
Dim xlApp
Dim xlBook
Dim xlSheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\1.xls")
Set xlSheet = xlBook.Sheets(1)
Creating fails with Syntax error in
The test run cannot continue due to a syntax error.
Syntax error
Line (242): Set xlApp = CreateObject("Excel.Application")
Does anybody know how to repair it?
Thanks for the help
According to your comment, you are using somthing like:
Class MyClass
Dim xlApp
Dim xlBook
Dim xlSheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\1.xls")
Set xlSheet = xlBook.Sheets(1)
End Class
Then, you get a syntax error in the xlApp assignment.
And right so, because the assignment is invalid in that scope (which is no callable scope at all).
First of all, set option explicit on.
Then, make sure you define all instance variables with Dim.
Also, create a constructor, or as in the following sample, a callable Sub, which initializes the instance variables, like this:
Option Explicit
Class MyClass
Dim xlApp
Dim xlBook
Dim xlSheet
Public Function SetParam ()
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\1.xls")
Set xlSheet = xlBook.Sheets(1)
End Function
End Class
This would not generate a syntax error, and might be closer to what you intended.
This is because inside a Class...End Class construct, you cannot have anything else but definitions. No statements. And assignments are statements. (Initialization using "=" in a variable definition is not supported by VBScript.)
Related
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
i am trying create a bar chart using the vb script.
my script is
Set objExcel = CreateObject("Excel.Application")
Set objReadWorkbook = objExcel.Workbooks.Open("D:\Excel_Macro_Proj\Create_Barchart.xlsx")
Set oExcelReadWorkSheet = objReadWorkbook.Worksheets(1)
objReadWorkbook.Sheets("Sheet1").Activate
objExcel.Visible = True
Sub GraphCreate ()
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveSheet.Range("D11:D14,F11:F14,H11:H14")
End Sub
GraphCreate
objReadWorkbook.SaveAs("D:\Excel_Macro_Proj\barchart_create1.xls"),-4143
objExcel.Quit
when i run the above macro i am getting an error as,
"object required ActiveSheet". i am not getting why this error. any one please help me on this.
vbscript cant recognize ActiveSheet object directly, it available under Excel.Application object..in this case objExcel.ActiveSheet and objExcel.ActiveChart
Try this code
Set objExcel = CreateObject("Excel.Application")
Set objReadWorkbook = objExcel.Workbooks.Open("D:\Excel_Macro_Proj\Create_Barchart.xlsx")
Set oExcelReadWorkSheet = objReadWorkbook.Worksheets(1)
objReadWorkbook.Sheets("Sheet1").Activate
objExcel.Visible = True
Sub GraphCreate ()
objExcel.ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
set xlRange = objExcel.ActiveSheet.Range("D11:D14,F11:F14,H11:H14")
objExcel.ActiveChart.SetSourceData(xlRange)
End Sub
GraphCreate
objReadWorkbook.SaveAs("D:\Excel_Macro_Proj\barchart_create1.xls"),-4143
objExcel.Quit
So I have been trying to parse the temperature from weather.com and have managed to do it, but now I am stuck trying to save the temperature to a file (temperature.txt). I feel like this should work but it returns the error:
Line: 11 Char: 1 Error: Type mismatch: 'Write' Code: 800A000D
This is my code please help!!!
Dim nm, em, FSO, oFile
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = false
xmlDoc.load("http://xml.weather.com/weather/local/USUT0225?cc=*&unit=farenheit&dayf=0")
Set temp = xmlDoc.selectsinglenode ("/weather/dayf/day/part/t")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.OpenTextFile("temperature.txt", 2, True)
oFile.Write(temp)
oFile.Close
Set oFile = Nothing
Set FSO = Nothing
To further the answer since I was having an issue with your node I tried something like this
Dim nm, em, FSO, oFile
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = false
xmlDoc.load("http://xml.weather.com/weather/local/USUT0225?cc=*&unit=farenheit&dayf=0")
Set temp = xmlDoc.selectSingleNode ("/weather/cc/tmp")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.OpenTextFile("temperature.txt", 2, True)
oFile.Write(temp.text)
oFile.Close
Set oFile = Nothing
Set FSO = Nothing
While checking the type of temp it keep returning Nothing. I went to look at the XML file via the link and I couldnt follow "/weather/dayf/day/part/t" you had there ( Might be because I'm in Canada and was getting redirected). Either way I updated the node you were looking for to "/weather/cc/tmp" and outputted the .text to the file. As of right this moment the contents of my text file are 79
Your
Set temp = xmlDoc.selectsinglenode ("/weather/dayf/day/part/t")
assigns a (node) object to temp (great variable name, btw). The .Write method of the TextStream object can't serialize objects, it can write only strings. So .Write the node's XML content:
oFile.Write temp.xml
(and loose those ())
I am trying to open a user form which I have created in a PPTM file via VBScript. Code for VB script is as below. This does seem to be working. It is simply opening that macro PPTM and closing it. Any suggestions?
Option Explicit
Dim pptApp, pptPresentation, CurrentDirectory
dim fso: set fso = CreateObject("Scripting.FileSystemObject")
CurrentDirectory = fso.GetAbsolutePathName(".")
Set pptApp = CreateObject("PowerPoint.Application")
Set pptPresentation = pptApp.Presentations.Open(CurrentDirectory + "\Revison Macro V1.pptm",True)
On Error Resume Next
pptApp.Run "Revision"
If Err Then
End If
pptPresentation.Close
pptApp.Quit
Set pptPresentation = Nothing
Set pptApp = Nothing
WScript.Quit
A Few code revisions
Set pptPresentation = pptApp.Presentations.Open(CurrentDirectory + "\Revison Macro V1.pptm",True) >> VBScript uses "&" rather than "+" even though this worked fine, it's better to stick to the correct string handling.
The userform needs to be indirectly called to pause the vbscript. So create a separate Sub and call it "Call_Revision". The code will be simple and straightforward as follows:
Sub Call_Revision
Revision.Show
End Sub
When you execute the .Run command, it needs to know how to find the code to run the UserForm. So now that we have established our sub, we can use that to show the form.
From: pptApp.Run "Revision"
To: pptApp.Run "Revison Macro V1.pptm!Module1.Call_Revision"
If you are waiting for the user to close out the userform to execute the rest of the code and exit the PPTM file, you can apply the following OnClose event within the Userform:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Application.Quit
End Sub
And the Full Code:
Option Explicit
Dim currppt : currppt = "Revison Macro V1.pptm"
Dim ModuleName: ModuleName = "Module1"
Dim OpenUF : OpenUF = "Call_Revision"
Dim pptApp, pptPresentation, CurrentDirectory
dim fso: set fso = CreateObject("Scripting.FileSystemObject")
CurrentDirectory = fso.GetAbsolutePathName(".")
Set pptApp = CreateObject("PowerPoint.Application")
Set pptPresentation = pptApp.Presentations.Open(CurrentDirectory & "\" & currppt,True)
On Error Resume Next
pptApp.Run currppt & "!" & ModuleName & "." & OpenUF
msgbox "Done"
pptPresentation.Close
pptApp.Quit
Set pptPresentation = Nothing
Set pptApp = Nothing
I have 2 vbs files.
A.vbs:
Class test
public a
public b
End Class
B.vbs:
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "C:\Users\shanmugavel.chinnago\Desktop\Test3.vbs"
Dim ins
Set ins = new test 'Here throws "Class not defined: test"
ins.a = 10
ins.b = "SCS"
msgbox ins.a
msgbox ins.b
Now I want to achive this like in B.vbs file. But it throws error while creating instance for the class availble in A.vbs. Any help?
.Running a .vbs won't make the code usable in another one. A simple but extensible strategy is to use .ExecuteGlobal on the 'libraries'. Given
Lib.vbs:
' Lib.vbs - simple VBScript library/module
' use
' ExecuteGlobal goFS.OpenTextFile(<PathTo\Lib.vbs>).ReadAll()
' to 'include' Lib.vbs in you main script
Class ToBeAShamedOf
Public a
Public b
End Class ' ToBeAShamedOf
and main.vbs:
' main.vbs - demo use of library/module Lib.vbs
' Globals
Dim gsLibDir : gsLibDir = ".\"
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
' LibraryInclude
ExecuteGlobal goFS.OpenTextFile(goFS.BuildPath(gsLibDir, "Lib.vbs")).ReadAll()
WScript.Quit main()
Function main()
Dim o : Set o = New ToBeAShamedOf
o.a = 4711
o.b = "whatever"
WScript.Echo o.a, o.b
main = 1 ' can't call this a success
End Function ' main
you'll get:
cscript main.vbs
4711 whatever
(cf. this answer for a seed of a useful class)
Your b script doesn't have contact with youyr a script, you need to include the code like so, then you can use code from a like it were present in b
call Include("a.vbs")
Sub Include (Scriptnaam)
Dim oFile
Set oFile = oFso.OpenTextFile(Scriptnaam)
ExecuteGlobal oFile.ReadAll()
oFile.Close
End Sub
This is the code that we use to do this.
Sub Include(sInstFile)
Dim f, s, oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
If oFSO.FileExists(sInstFile) Then
Set f = oFSO.OpenTextFile(sInstFile)
s = f.ReadAll
f.Close
ExecuteGlobal s
End If
On Error Goto 0
Set f = Nothing
Set oFSO = Nothing
End Sub
Include("c:\files\SSDConnection.vbs")
Include("c:\files\SSDTable.vbs")
Works flawless for our team
You can convert B.vbs into a Windows Script File which will allow you to include A.vbs.