vb script Refreshall Error 424 - vbscript

I have a VB Script that opens a spreadsheet, executes RefreshAll to update the sheet from external data, then does SaveAs a .CSV I had the script working as it should, then moved job to run on another computer. The job is running unattended through the Task Scheduler and now my error log shows ERROR 424 and the script no longer updates the spreadsheet, nor saves the .CSV. The script does run as it should on this machine interactively. The Job is using my credentials to run unattended. Anyone have any help on the Error 424???
Here is code snippet:
dim sLogFile, logFSO, objLogFile
sLogFile = "F:\Feed\FeedLog.log"
Set logFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = logFSO.OpenTextFile(sLogFile, 8, True)
Dim objExcel
Dim objWorkBook, objWorksheet
Set objExcel = CreateObject("EXCEL.APPLICATION")
Set objWorkBook = objExcel.Workbooks.Open("F:\Feed\FeedOlap.xlsx")
Set objWorksheet = objWorkbook.Worksheets("Sheet6")
objLogFile.WriteLine (" " & DATE & " " & TIME & " Starting Refresh")
objworkbook.Refreshall
IF ERR THEN
objLogFile.WriteLine "Error" & Err.Number
End If
objLogFile.WriteLine (" " & DATE & " " & TIME & " Refresh Complete")
objExcel.DisplayAlerts=FALSE
objWorkBook.Save
objWorksheet.SaveAs "F:\Feed\FeedOlapCSV.csv", 6
objWorkBook.Close True
objExcel.Quit
Set objWorkBook = Nothing
Set objExcel = Nothing
objLogFile.WriteLine (" " & DATE & " " & TIME & " CSV Created")

Seeing as how you are refreshing from external data, there must be a missing link to one of your external data files... You'd probably need to investigate every single external data link and make sure you can still get to it in your new location. (e.g. privileges to every location, mapped drives for your domain user, etc.)

Related

VB SCRIPT The system can not find the file specified ON SOME COMPUTERS/USERS

Good day! I have this script that opens up an access application.
This script works on several users but one. This user is getting this error
"Error 80070002: The system can not find the file specified".
I'm quite sure there is nothing wrong with my script as only one person is encountering this issue.
Could there be a computer setting or update that is causing this problem?
Everything works except for the Open File part.
And this is for some computers/user only. Most of the Computer/users can execute this without any problem.
Thanks in advance!
Here's the script
'*******************************************************************************
'Find user name
'*******************************************************************************
Set WshNetwork = CreateObject("WScript.Network")
userName = WshNetwork.UserName
Set WshNetwork = Nothing
'*******************************************************************************
'Find version of master file
'*******************************************************************************
Set folder = objfso.GetFolder(folderPath)
For Each file In folder.Files
If InStr(file.Name, "AMSDshbd_M") = 1 Then
masterVersion = Mid(file.Name, 11, (InStrRev(file.Name, ".") - 11))
Exit For
End If
Next
'*******************************************************************************
'Find version of user file, if it exists
'*******************************************************************************
isUserFile = 0
For each file In folder.Files
If InStr(file.Name, "AMSDshbd_" & userName) = 1 Then
isUserFile = 1
userVersion = Mid(file.Name, (Len(userName) + 10), (InStrRev(file.Name, ".") - (Len(userName) + 10)))
Exit For
End If
Next
'*******************************************************************************
'Copy the file if no user file exists or if the user version is not current
'*******************************************************************************
sourceFile = folderPath & "AMSDshbd_M" & masterVersion & ".accde"
targetFile = folderPath & "AMSDshbd_" & userName & "_M" & masterVersion & ".accde"
isCopyNeeded = 1
if isUserFile = 1 then
if userVersion = masterVersion then
isCopyNeeded = 0
end if
end if
if isCopyNeeded = 1 then
objFSO.CopyFile sourceFile, targetFile, True
end if
'*******************************************************************************
'Open the file
'*******************************************************************************
sComTxt = Chr(34) & microsoftAccessFile & Chr(34) & " " & Chr(34) & targetFile & Chr(34)
'objShell.Run sComTxt
objShell.Run sComTxt,,true
Set objFSO = Nothing
Set objShell = Nothing
I figured out what happened. the variable "microsoftAccessFile" is the path to the MS Access EXE, some of the users have a different path to this Access EXE that's why it doesn't work for them. I identified the path where their Access EXE is stored and changed the script for them and it works now. Thanks for pointing out the variable

Continuously looping VBScript needs to be more robust to errors

I'm writing a program to open and run multiple VBA modules on a server to upload new data to the cloud. I need to run the modules every 5 minutes at least (preferably every 2 minutes) without stopping 24/7 for an extended period of time. So far, my code will work for 1000-4000 loops, but fails every 1-4 days. How can I make my program more robust?
I'm pretty new to VBScripts, but have pieced together some code that works pretty well from other examples I've found through my research. I've added some error handling (On Error Resume Next and an error check after each major operation) but there are still occasional errors that sneak through and stop the program.
The errors I've been seeing include "unknown VBA runtime error," permission errors (when opening the local log), and server errors. I've tried to mitigate or ignore these with pings and error handling, but some still manage to stop the code from looping.
I've also tried using the Windows Task Scheduler, but that can only go at most every 5 minutes and I couldn't get it to run reliably.
On Error Resume Next
Set WshShell = CreateObject("WScript.Shell")
Dim objFSO
Dim myLog
Dim myLocalLog
Dim pingResultV
Dim pingResultN
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objExcel = CreateObject("Excel.Application")
objExcel.DisplayAlerts = False
objExcel.Visible = True
Set myLog = objFSO.OpenTextFile("location of log on server", 8)
Set myLocalLog = objFSO.OpenTextFile("location of log on local hard drive", 8)
Dim i
i = 0
Do While i < 1000000
'Ping the server to check if the connection is open
PINGFlagV = Not CBool(WshShell.run("ping -n 1 server",0,True))
pingResultV = "Null"
'if the ping was successful, open each file in sequence and run the VBA modules
If PINGFlagV = True Then
timeStamp = Now()
Set Wb = objExcel.WorkBooks.Open("excel file on server",,True)
objExcel.Application.Run "VBA module to copy data to cloud"
objExcel.Application.Quit
If Err.Number <> 0 Then
myLocalLog.WriteLine(timeStamp & " Error in running VBA Script: " & Err.Description)
Err.Clear
End If
Set Wb2 = objExcel.WorkBooks.Open("second excel file on server",,True)
objExcel.Application.Run "VBA module to copy this data to cloud"
objExcel.Application.Quit
If Err.Number <> 0 Then
myLocalLog.WriteLine(timeStamp & " Error in running VBA Script 2: " & Err.Description)
Err.Clear
End If
'Write to the server log that it succeeded or failed
pingResultV = "Server Ping Success"
myLog.WriteLine(timeStamp & " i=" & i & " --- " & pingResultV)
Else
pingResultV = "Server Ping Failed"
timeStamp = Now()
End If
'Write to a local log whether the server was available or not
myLocalLog.WriteLine(timeStamp & " i=" & i & " --- " & pingResultV)
If Err.Number <> 0 Then
myLog.WriteLine(timeStamp & " Error in writing to local log: " & Err.Description)
Err.Clear
End If
'Time between iterations in milliseconds. 120000 ms = 2 minutes
WScript.Sleep(30000)
i = i + 1
Loop
I'm not asking for someone to overhaul my code, but I would appreciate any suggestions on improvements. I'm also open to methods other than VBScript if they're more robust.
Some additional background on the system that might be helpful: I have 2 computers that upload data to a server every few minutes, but these computers can't be connected to the internet for security reasons. I need to upload the data these computers generate to an internet database every few minutes so it can be accessed remotely. The code I wrote runs on a separate computer that's connected to the server and internet, and it periodically opens the excel files on the server in read-only mode and runs a VBA script that uploads new data lines to the cloud.
It's hard to tell whether your VBScript or your VBA module or both fail. From your error descriptions, I somewhat expect the latter to be the case. So I'd also suggest to check your VBA coda (again). As for this script, one thing I'd suggest to get rid off the permission / log file error would be to move the logging to its own method, e.g.
' *** VBScript (FSO) ***
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
' Log file
Const LOG_FILE = "C:\MyData\MyLog.txt"
Sub WriteLog (ByVal sLogFile, ByVal sText)
Dim oFso
Dim oLogFile
Set oFso = CreateObject("Scripting.FileSystemObject")
Set oLogFile = oFso.OpenTextFile(sLogFile, ForAppending, True)
' Prefix log message with current date/time
sText = Now & " " & sText
oLogFile.WriteLine sText
oLogFile.Close
' Free resources
Set oLogFile = Nothing
Set oFso = Nothing
End Sub
And call it like
If Err.Number <> 0 Then
WriteLog LOG_FILE, "Error in writing to local log: " & CStr(Err.Number) & ", " & Err.Description
Err.Clear
End If
I also suggest to release all resources at the end of the loop, e.g. the Excel object, for which you need to move the object creation into the loop:
Dim i
i = 0
Do While i < 1000000
' Start of loop
Set objExcel = CreateObject("Excel.Application")
objExcel.DisplayAlerts = False
objExcel.Visible = True
' Do your stuff ...
' End of Loop -> free resources
Set Wb = Nothing
Set Wb2 = Nothing
objExcel.WorkBooks.Close
Set objExcel = Nothing
' Time between iterations in milliseconds. 120000 ms = 2 minutes
WScript.Sleep(30000)
i = i + 1
Loop
And as I'm not an Excel expert, I wonder if objExcel.Visible = True does make sense here or if it would better be set to False?

ASP Creating Text File ObjFileSys.CreateTextFile

I am trying to create a textfile in a temp directory...
I don't understand what I am doing wrong...
My Error is:
Microsoft VBScript runtime error '800a004c'
Path not found
/racklabels/desktop/printLabel.asp, line 128
There is a temp directory on my server I am running this from...
strFileNameQAD = "C:\temp\" & strFileNameRBB
'Create the files, write to them & close them.
If bBackFlush = True Then
Set filQAD = objFileSys.CreateTextFile(strFileNameQAD)
filQAD.WriteLine ("H::" & strPart & strLocation & strSite & strQty & strSerial & strRef & strUserID & strAccount & strSubAccount & strCostCenter & strEffDate & strYes)
filQAD.WriteLine ("D::" & strFromLocation & strNo & strUserID)
filQAD.Close
Set filQAD = Nothing
End If
The FileSystemObject is complaining about the path contained in the strFileNameQAD, try placing a Response.Write strFileNameQAD : Response.Flush before the offending line to see what strFileNameQAD outputs as.
Note: Response.Flush causes the server to write the response headers and buffer, so even if your script errors the output will be shown first.
strFileNameQAD = "C:\temp\" & strFileNameRBB
'Create the files, write to them & close them.
If bBackFlush = True Then
'Output strFileNameQAD variable to check content
Response.Write strFileNameQAD : Response.Flush
Set filQAD = objFileSys.CreateTextFile(strFileNameQAD)
filQAD.WriteLine ("H::" & strPart & strLocation & strSite & strQty & strSerial & strRef & strUserID & strAccount & strSubAccount & strCostCenter & strEffDate & strYes)
filQAD.WriteLine ("D::" & strFromLocation & strNo & strUserID)
filQAD.Close
Set filQAD = Nothing
End If
Does the strFileNameQAD variable contain the expected path? My guess would be that strFileNameRBB is empty so the FileSystemObject sees C:\temp\ but no file to create, hence the error.

How to check the current windows command prompt is hidden by using vbscript or command lines

I want to detecting if the current running bat script is hidden by the caller, that means (nCmdShow=0) for example.
There is windows API to get this information, GetStartupInfo, but it can not be called by command prompt or VBScript(without third party libraries).
The following script can retrieve the startup information, but the problem is that's only works under WinXp, it's doesn't work under Win7. I am looking for a way can support across winxp - win8.
Dim wmiService
Set wmiService = GetObject("winmgmts:\\.\root\cimv2")
Dim startupInfo
Set startupInfo = wmiService.Get("Win32_ProcessStartup")
The following code works fine under xp, but doesn't work under win7, it's show all the startup information.
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ProcessStartup",,48)
For Each objItem in colItems
Wscript.Echo "CreateFlags: " & objItem.CreateFlags
Wscript.Echo "EnvironmentVariables: " & objItem.EnvironmentVariables
Wscript.Echo "ErrorMode: " & objItem.ErrorMode
Wscript.Echo "FillAttribute: " & objItem.FillAttribute
Wscript.Echo "PriorityClass: " & objItem.PriorityClass
Wscript.Echo "ShowWindow: " & objItem.ShowWindow
Wscript.Echo "Title: " & objItem.Title
Wscript.Echo "WinstationDesktop: " & objItem.WinstationDesktop
Wscript.Echo "X: " & objItem.X
Wscript.Echo "XCountChars: " & objItem.XCountChars
Wscript.Echo "XSize: " & objItem.XSize
Wscript.Echo "Y: " & objItem.Y
Wscript.Echo "YCountChars: " & objItem.YCountChars
Wscript.Echo "YSize: " & objItem.YSize
Next
There is a way of calling API calls in VBS or batch.
appactivate between multiple internet explorer instances
Although the sample given doesn't work 7 and later because of a name conflict. Rename sendmail to something else for 7 and later.
If a limited user you need to manually add the registry entries to hkcu\software\classes.

VB script to run a Query

I am trying to connect command prompt through VB script and further its connecting with Oracle enviroment to execute some reports of Oracle discoverer.
But the problem is with this VB script only.
line 2: for establishing the connection.
line 7:fetching the current REQUEST_ID.
line 16:XXDIS_EXPORT_CMD_V is a view and cmd is a column.which select a value like this for corresponding REQUEST_ID.
/CONNECT DISCADMIN:"FAI Financials Intelligence"/discbi#deverp /OPENDB "1 Scheduling" /SHEET "Sheet_1" /EXPORT HTML o27673334.out /LOGFILE l27673334.log /BATCH
In the end i want to execute this cmd using VBScript.
Error coming :
"In line 32 tkgoShell was not
recognized"
This is My code:
' Process job
Set objADO =CreateObject("ADODB.Connection")
objADO.Open "Driver={Microsoft ODBC for Oracle}; CONNECTSTRING=deverp; UID=apps; PWD=apps11i;"
MsgBox "Connection Established to Server.", vbExclamation + vbOKOnly, "System"
Do While True
' Check if there is a job to process
Set moRS=objADO.execute("SELECT apps.xxdis_schedule_pkg.start_job REQUEST_ID FROM dual")
moRS.MoveFirst
msRequest = moRS("REQUEST_ID")
'MsgBox msRequest,msRequest
' If no jobs then exit
' If msRequest = "0" Then
Exit Do
' End If
loop
Set moRS=objADO.execute("SELECT cmd EXPORT_CMD FROM apps.xxdis_export_cmd_v " & _
"WHERE request_id = " & msRequest)
MsgBox msRequest,msRequest
moRS.MoveFirst
msExpCmd = moRS("EXPORT_CMD")
' write command into a temporary file
msCmdFile = "r" & msRequest & ".cmd"
dim moOutputStream,filesys,msCommand
Set filesys = CreateObject("Scripting.FileSystemObject")
Set moOutputStream = filesys.CreateTextFile(msCmdFile, True)
' Substitute $SAMBA$ and $TNS$ locally configured variables
moOutputStream.Write Replace(Replace(msCmd, "$SAMBA$", gsOutDir),_
"$TNS$", gsInstance) & vbCRLF
moOutputStream.Close
' Call Discoverer to process the command
msCommand = gsBinDir & gsDiscoExe & " /EUL " & gsEUL & " /CMDFILE " & msCmdFile
Call tkgoShell.Run (msCommand, 1, true)
If I understand your code correctly I can't see anywhere where you're actually creating the tkgoShell.
Try inserting the following 2 rows before your last line:
Dim tkgoShell
Set tkgoShell = WScript.CreateObject ("WScript.Shell")
See here for more information about Shell.Run.

Resources