I am working on making a simple vbs that will open and close an excel file and save as it closes. This is to update the values inside the excel file after they are modified by a separate python script, and I need to do it this way for many dumb reasons outside my control.
I don't have a lot of experience with VBS but what I researched about this, I don't see the error in my code. However, I get an error whenever I try to run the code below. There error is:
Line: 7
Char: 39
Error: Expected statement
Code: 800A0400
Source: Microsoft VBScript compilation error
Any help would be appreciated, thanks!
dim fso
set fso = CreateObject("Scripting.FileSystemObject")
if(fso.FileExists("C:\Users\Public\Documents\Templates\testfile.xlsx")) then
set xlapp = createobject("Excel.Application")
xlapp.Workbooks.Open "C:\Users\Public\Documents\Templates\testfile.xlsx"
xlapp.ActiveWorkbook.SaveAs Filename:="C:\Users\Public\Documents\Templates\testfile_test.xlsx", FileFormat:=51, ConflictResolution:=2
xlapp.ActiveWorkbook.Close SaveChanges:=True
end if
If you are using standalone VBS, the following lines won't work because VBS isn't VBA. VBA supports Named Argument Syntax in function calls whereas its not supported by VBS
xlapp.ActiveWorkbook.SaveAs Filename:="C:\Users\Public\Documents\Templates\testfile_test.xlsx", FileFormat:=51, ConflictResolution:=2
xlapp.ActiveWorkbook.Close SaveChanges:=True
You have to provide the arguments to the function. So Function ArgName:=ArgValue becomes Function ArgValue
xlapp.ActiveWorkbook.SaveAs "C:\Users\Public\Documents\Templates\testfile_test.xlsx",51,,,,,,2
xlapp.ActiveWorkbook.Close True
Related
I am a Java programmer, and I do not know anything about VBScript. I needed a way to modify the target path of a certain shortcut, and I according to my research, only VBScript can help me. I tried it, but there were many errors.
I tried to resolve them by a butt ton of googling, but I think there is some other problem.
Here is the code:
Set sh = CreateObject("WScript.Shell")
Set shortcut = sh.CreateShortcut("C:\Users\pedne\Desktop\Zoom.lnk")
shortcut.TargetPath = "C:\Users\pedne\AppData\Roaming\Zoom\bin\Zoom.exe ""--url=zoommtg\://zoom.us/join?action=join&confno=955 1234 1234&pwd=12345"""
shortcut.Save
I do not know whether I need any prerequisites or external libraries for writing VBScript code, and I wrote it on notepad.I would also like to know whether there is any 100% Java or any other workaround to this. (Totally up to you)
Please excuse me if I do not understand anything about your answers as I copied the code from Change a shortcut's target from command prompt
The last error I got was at Line 3 char 1, Invalid procedure or call
Do not append the arguments to the target path. Use the Arguments property instead:
shortcut.TargetPath = "C:\Users\pedne\AppData\Roaming\Zoom\bin\Zoom.exe"
shortcut.Arguments = "--url=zoommtg\://zoom.us/join?action=join&confno=955 1234 1234&pwd=12345"
shortcut.Save
At the top of most of my VBscripts (.vbs files) I have the following code:
Option Explicit
Const ForReading = 1
Dim objFSO, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading)
Execute objFile.ReadAll()
This code allows me to use another vbs file as a library. In this case somefile.vbs would be my library and have all my subs and functions defined that are called from the script the above code is called from (I call this the calling script).
This issue: Every once in a while, one of the scripts seems to delete the code in Z:\somepath\somefile.vbs (the library script read by the calling script).
I think this because if a wscript.exe is listed in my Task Manager Processes tab and I restore the Z:\somepath\somefile.vbs file from a backup location, almost immediately, when I open Z:\somepath\somefile.vbs again, there is no code in that file. But if I kill the wscript.exe process, the file is fine. I can't reproduce the behavior because it only occurs when our network has a hiccup of some kind (I think).
My first thought is that the create setting is wrong when I use this line:
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading)
But according to this link, the default create value should be false:
https://msdn.microsoft.com/en-us/library/aa265347(v=vs.60).aspx
Note, coincidentally, I am also using objFile and objFSO variables in the file somefile.vbs for things that aren't related to what I am doing in the calling script. For example, the objFile in the somefile.vbs file has a completely different name and location and is created this way:
Set objFile = objFSO.OpenTextFile("z:\differentpath\differentname.vbs", ForAppending, True)
I am guessing this is the issue, but I don't understand it. Can someone shed some light on this? Is the create or append setting getting reset in the calling script? How does that work?
Not knowing what else to do I have change the variable names in the somefile.vbs file to oFSO, oFile and in the calling script they are still objFSO, objFile. I also changed the line of code in the calling script to include false for the create setting like this:
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading,false)
Going out on a limb (since you posted only partial code) I'm going to assume that you don't explicitly close your library script after reading, so your main script keeps the file open until it terminates. Either add a line objFile.Close after the Execute statement, or (better yet) change
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading)
Execute objFile.ReadAll()
to
code = objFSO.OpenTextFile("Z:\somepath\somefile.vbs").ReadAll
Execute code
or just
Execute objFSO.OpenTextFile("Z:\somepath\somefile.vbs").ReadAll
so that the file is automatically closed after being read.
Learned from https://ss64.com/vb/execute.html
Execute takes a group of statements and executes them in local scope, ExecuteGlobal executes them in global scope.
However, if the same Execute statement is invoked outside of a procedure (i.e., in global scope), not only does it inherit everything in global scope, but it can also be called from anywhere, since its context is global.
Does the issue remain if you call the Execute form a procedure?
I am trying to automate a .application file to open automatically on an end users machine. The code I found online says to do this:
Dim my_file
my_file = "c:/location/example.application"
ShellExecute 0, vbNullString, my_file, vbNullString, vbNullString, vbNormalFocus
but when I try to run it I get a type mismatch error on line 4 which is the ShellExecute line. How can I fix this?
ShellExecute() is a method of a Shell Application object. You'll need to create an instance of the class before you can call one of its methods:
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute my_file
Most of the parameters to ShellExecute() are optional and can be omitted, if you'd like.
See this page for information.
Also note that constants defined by external type libraries (as is the case here) are not immediately available to VBScript. That means your script will not understand vbNormalFocus. The easiest way to overcome this is to just define the constants yourself.
Const vbNormalFocus = 1
hopefully this will be an easy one? I've got a VBS file which I am trying to schedule every week to do a XML refresh. It simply calls an ASPX page. But I cannot get the thing to work! Even when I try to double click the VBS file I just get an error message. The actual code can be found all over the place - it seems to be standard code for this purpose.
The code is (fetch.vbs)
Call LogEntry()
Sub LogEntry()
'Force the script to finish on an error.
On Error Resume Next
'Declare variables
Dim objRequest
Dim URL
Set objRequest = CreateObject("Microsoft.XMLHTTP")
'Put together the URL link appending the Variables.
URL = "http://mywebsite/fetchXML.aspx"
'Open the HTTP request and pass the URL to the objRequest object
objRequest.open "POST", URL , false
'Send the HTML Request
objRequest.Send
'Set the object to nothing
Set objRequest = Nothing
End Sub
The error message I get from windows (when I double click the VBS file is)
Script: fetch.vbs
Line: 1
Char: 1
Error: Invalid Character
Code: 800A0408
Source: Microsoft VBScript Compilation Error
Any ideas?!
Problem was solved by saving as ANSI format (not UTF-8 as default)
ref: VBScript Invalid Character 800A0408 compilation error
it looks like it may be the encoding. especially for line 1 char 1.
I have the following vbs script from Microsoft Support for adding Excel add-in:
Dim oXL As Object
Dim oAddin As Object
Set oXL = CreateObject("Excel.Application")
oXL.Workbooks.Add
Set oAddin = oXL.AddIns.Add("c:\Program Files\MyApp\MyAddin.xla", True)
oAddin.Installed = True
oXL.Quit
Set oXL = Nothing
I save the above script to a file called as addin.vbs and run it from a command console:
C:\...>cscript addin.vbs
I got the following error:
c:\...\addin.vbs(1, 9) Microsoft VBScript compilation error: Expected end of statement
Not sure how I can run it from cmd console?
I am running it from Windows XP.
Visual Basic for Applications (VBA, which your code is written in) and Visual Basic Scripting Edition (VBS) are not the same language.
Windows Scripting Host (WSH, i.e. cscript.exe and wscript.exe) only handles Active Scripting languages (in most installs, VBScript and JScript). VBA can only be run within the application that is destined to host it.
Just follow the directions on the Microsoft Support page you have and add the script to Excel.
The error occurs because of the As Object clause. Unlike VBA, VBScript has only one data type — Variant, so you don't specify the data type when declaring a variable. Remove the As Object clauses and the script should work fine:
Dim oXL, oAddin
Set oXL = CreateObject("Excel.Application")
oXL.Workbooks.Add
Set oAddin = oXL.AddIns.Add("c:\Program Files\MyApp\MyAddin.xla", True)
oAddin.Installed = True
oXL.Quit
Set oXL = Nothing