VbScript unknown runtime error on opening workbook - excel

I'm not able to open any-work while working with QTP using vbscript to open Excel workbook to read input data. History of the problem is as - on last weekend my organization decided to install Excel 2010 on my machine. After that, when i checked i had two version of Excel (2007 & 2010) installed on my machine which started the problem.
I removed excel 2010 and re-install excel 2007 but the problem is still persisting.
Code which is giving the error is while opening workbook as 'Unknown Runtime Exception'
completeFilePath = "C:\Users\rahul.singh\Desktop\TC1.xls"
gv_SanityTradeDataSheet = "TCFlow"
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.WorkBooks.Open(completeFilePath)
Set objDriverSheet = objWorkbook.Worksheets(gv_SanityTradeDataSheet)
wscript.echo objDriverSheet.UsedRange.Rows.Count
objWorkbook.Close
Set objDriverSheet = Nothing
Set objWorkbook = Nothing
objExcel.quit
Set objExcel = Nothing
Any idea what's the issue/fault and how to come out of it
Thanks,
Rahul

Related

can't create excel file using vb6.0 code in all windows pcs

I have developed application using vb 6.0 that creates excel file to export data. I have installed that application on more than 1000 computers that run windows. It is working fine on many computers. But around one out of 50 users complaints that this export facility doesn't work.
Below is my sample code.
Dim objExcel As Excel.Application
Dim objWorkBook As Excel.Workbook
Dim objWorkSheet As Excel.Worksheet
Dim selectedrange As Excel.Range
Set objExcel = New Excel.Application
Set objWorkBook = objExcel.Workbooks.Add
Set objWorkSheet = objWorkBook.Worksheets(Tag)
Set objWorkSheet = objExcel.ActiveSheet
objWorkSheet.Range("A1", "P" & 1000).Font.Name = "Arial"
objWorkSheet.Range("A1", "P" & 1000).Font.Size = 10
objWorkSheet.Cells(2, 1) = "ABCD"
objExcel.Visible = True
objWorkBook.Close (True)
If exporting to excel using VB6.0 is not working on any pc, it never worked after many try on that pc. I have tried 1) re-installing application, 2)registering msexcel35.dll file in windows/system32 folder and windows/syswow64 folder, etc.
but that never worked for me on such computers.
I want that export facility should run on all windows pcs of my application. pls guide me what to do?

Open Excel and Run Macro using VBScript [duplicate]

This question already has answers here:
Run Excel Macro from Outside Excel Using VBScript From Command Line
(8 answers)
Closed 2 years ago.
I'm trying to use this VBScript to open a file and run a macro. Ideally I'd open it from my personal workbook, but happy to run from anywhere as a compromise.
Option Explicit
On Error Resume Next
RunExcelMacro
Sub RunExcelMacro()
Dim xlApp
Dim xlBook
If CheckAppOpen("excel.application") Then
'MsgBox "App Loaded"
Set xlApp = GetObject(, "Excel.Application")
Else
' MsgBox "App Not Loaded"
Set xlApp = CreateObject(,"Excel.Application")
End If
xlApp.visible = True
set xlBook = xlApp.Workbooks.Open ("C:\Users\....\PERSONAL.xlsb", 0, True)
xlApp.Run "Module1.My Macro"
xlApp.Quit()
xlBook = Nothing
xlApp = Nothing
End Sub
The script does not open any Excel file, independent of if it being on a server or in my C drive.
There's been a fair number of posts on this. I did some research.
I tried:
Set xlApp = GetObject("Excel.Application")
Set xlApp = CreateObject("Excel.Application")
as well as
Dim Filename as string
Filename = "C:/....."
set xlBook = xlApp.Workbooks.Open (Filename)
I've tried opening .xls, .xlsm, .xlsb all to no avail.
I am not getting any error messages. The cmd console opens and then closes with this
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
Thanks to #AlexK. Turned out to be a comma in the wrong place.
Also thanks to this post changed:
xlApp.Run "Module1.MyMacro"
to:
xlApp.Run xlBook.name & "!Module1.MyMacro"

R save excel file with user input as name

So I am having a problem uploading files from RStudio to Excel for MATLAB processing.
I had this problem before with formulas not being populated, so I made a script to open, save, and close the Excel file which then worked fine for populating the formulas and loading the numerical values back into RStudio. However I can not figure out how to open multiple .csv files that have changing names depending on our sample ID.
Heres my script that I tried to have open up multiple files:
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\PCRdata\*.csv")
## Also tried Set objWorkbook = objExcel.Workbooks.Open("C:\PCRdata\"& "*.csv")
objExcel.Application.Visible = True
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
WScript.Echo "Your Excel Spreadsheet was Updated, Open these files in Matlab"
WScript.Quit
However the script doesnt like the * to call upon all files, is there another way I can do this? or a better way to have RStudios come out with data that is usable to matlab.
MATLAB Error:
Error using netest/testingBrowseButton_Callback (line 63)
Cannot concatenate the table variables 'AKAP8L' and 'ARAF', because their types are double and cell.
Error while evaluating UIControl Callback
Well did some digging on the website and found an alternative post that used a script to change change xls to xslx so I just made it so it went from csv to csv. Hope this helps anyone else that had this problem.
Set app = CreateObject("Excel.Application")
app.Visible = False
app.DisplayAlerts = False
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("C:\PCRdata").Files
If LCase(fso.GetExtensionName(f)) = "csv" Then
Set wb = app.Workbooks.Open(f.Path)
wb.Save
wb.Close True
End if
Next
app.Quit
Set f = Nothing
Set app = Nothing
Set fso = Nothing
wScript.Quit

WinWrap CreateObject("Excel.Application") Multiple Instances

Not sure how many people know about WinWrap Basic language which supports the .Net framework. But that is where my problem is. Hoping someone out there know about the language.
I am using it to communicate with Excel 2010. Basically I am extracting data for a proprietary data format and spitting it out to Excel. The following execute perfectly when no additional instances of Microsoft Excel 2010 are active during script run.
xExcel = CreateObject("Excel.Application")
xBook = xExcel.Workbooks.Open(XLFilePath)
xSheet = xBook.Worksheets(“Sheet1”)
xExcel.Visible = False
xSheet.Cells(1,1).Value = "Study Name"
However, if an additional Excel 2010 instance is activated manually by the user during the following script run, script errors out.
xExcel = CreateObject("Excel.Application")
xBook = xExcel.Workbooks.Open(XLFilePath)
xSheet = xBook.Worksheets(“Sheet1”)
xExcel.Visible = False
Do
xSheet.Cells(1,1).Value = "Study Name"
Loop
The guys at WinWrap say that CreateObject() in WinWrap is a simple code that calls the CoCreateInstance API. I am sort of lost there. Can someone help me understand how create multiple Excel instances.
Edit
I get the Runtime 50290 Application Specific Error.
Have you tried GetObject?
Dim objExcel As Object
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")
With objExcel
'do something
End With

How to run VBScript GetObject while the computer is locked?

For a few reasons discussed here I can't open an excel worksheet like this:
Set excel = CreateObject("Excel.Application")
Set workbook = excel.Workbooks.Open(file_path.xls)
excel.Visible = True
So I'm trying to open it like this:
CreateObject("WScript.Shell").Run "excel.exe"
WScript.Sleep 5000
CreateObject("WScript.Shell").Run "excel.exe"
WScript.Sleep 5000
Set excel = GetObject(,"Excel.Application")
Set workbook = excel.Workbooks.Open( excel_file_path, 3)
excel.Visible = True
This runs just fine when my computer isn't locked. And usually VBScripts can run even when a computer is locked. However, only when the computer is locked, at the line GetObject(,"Excel.Application") I get the following runtime error:
Error: ActiveX component can't create object: 'GetObject'
Code: 800A01AD
Source: Microsoft VBScript runtime error
I tried changing my Windows 7 settings to never lock after some inactivity, but my company's IT department has enforced that setting.
Is there a way to run this script from a locked screen, or is there an alternative you recommend?
Edit:
It has something to do with the way the excel file is being opened... If I open a new blank excel page than lock the screen, GetObject will work just fine getting that document, but if I open it using the shell then log out, it won't...
In the section that does not seem to work, add this line after setting visible = TRUE, and see if your calculations then work as expected:
CreateObject("WScript.Shell").AppActivate objExcel.Name
Not sure if it will work, but I am doing similar things in some code and this is what's there.
I must have copied or read about it somewhere (wish I could give credit to where).
FYI - I have a vbscript that opens Excel, starts a new workbook, reads a file in as text via a querytable object, then does some automated edits. The code that works for me is:
...
set objExcel = CreateObject("Excel.application")
' Commented out the following line, so the user can see something is happening.
' objExcel.application.screenupdating = false ' Don't update the screen while shucking and jiving is going on.
objExcel.visible = true
objExcel.application.cursor = 2 ' change cursor to the hourglass.
CreateObject("WScript.Shell").AppActivate objExcel.Name
' Create a new workbook and worksheet
objExcel.workbooks.add
objExcel.worksheets.add
etc
Try the code:
CreateObject("WScript.Shell").Run "excel.exe """ & excel_file_path & """"
WScript.Sleep 5000
CreateObject("WScript.Shell").Run "excel.exe"
WScript.Sleep 5000
Set excel = GetObject(,"Excel.Application")

Resources