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
Related
I am writing a macro that opens and closes a PowerPoint Presentation from Excel.
Now I have the issue that when I am trying to save the PowerPoint file I get a pop up Message Box. To take care of that issue I used:
Application.DisplayAlerts = False
However this works in some cases, as I loop through multiple Presentations but not in all. So I tried instead this:
pptPres.Application.DisplayAlerts = False
But this just caused the Display Alerts to always pop up.
So I tried to replicate that using
Application.DisplayAlerts = True
which did not work. So I am quite confused what I am doing wrong. I need to turn the DisplayAlerts off otherwise my macro is stopped.
The issue occurs as I am opening a PowerPoint with macros so a *.pptm file which on saving I am asked whether or not to save with macros.
This is my current code, maybe you can replicate the issue:
Dim pptPres As PowerPoint.Presentation
Set pptApp = CreateObject("powerpoint.Application")
Set pptPres = pptApp.Presentations.Open(strPfad & strDat, False, True, True)
pptPres.Application.DisplayAlerts = False
strFirma = "Test123"
pptPres.SaveAs strPfad + "\Berichte" & "\" & strFirma & ".pptx"
pptPres.Close
Any help is greatly appreciated.
Powerpoint's Application.DisplayAlerts is slightly different. It has two options: ppAlertsAll and ppAlertsNone.
Try pptApp.DisplayAlerts = ppAlertsNone. Note that you have a mix of early- and late-binding and probably should be consistent. If you go with late-binding, ppAlertsAll's corresponding value is 2, and ppAlertsNone's corresponding value is 1.
Thanks to #Matthieu Guindon for pointing out that since you're running this from Excel, Application refers to Excel.Application, which is not your instance of PowerPoint. Hence Application.DisplayAlerts will not affect PowerPoint's alert setting at all. You want to work with pptApp, the instance of PowerPoint.
This VBScript errs as "unknown runtime error", on the VBScript line that calls the application object's RUN method, which I'm quite sure I'm using correctly, syntactically.
With only one workbook open under the application object, I probably don't even need to prefix it with the Workbookname.
It appears the previous line (which sets the wb variable to the application object's Workbooks.Open method, does not open. Nor does it err, nor does it "eventually" open. The path is correct, the filename is correct, there is no On Error Resume Next in my VBScript.
How can the workbook not open and not error?
I hope I am wrong in thinking there is a "wait/pause" issue. Acting directly on the Excel object model, it seems like the VBScript code waiting until the excelapp object has opened the workbook should be a given?
dim excelapp, wb
set excelapp = createobject("excel.application")
excelapp.enableevents=true
excelapp.visible=true
set wb = excelapp.workbooks.open("C:\Users\John\Desktop\Scheduled Jobs from Isaac\Availability.xlsb")
'MSGBOX "SHOULD BE OPEN"
excelapp.windowstate = -4137 'value for constant xlMaximized acccording to msdn
excelapp.caption = "Running AVAILABILITY - Please WAIT..."
excelapp.run "Availability.xlsb!ChangeAndCopyFile"
final vbscript code i used, for better or worse:
dim excelapp, wb
'set excelapp = createobject("excel.application")
'excelapp.enableevents=true
'excelapp.visible=true
'set wb = excelapp.workbooks.open("C:\Users\John\Desktop\ScheduledJobsfromIsaac\Availability.xlsb")
'although I don't know why the above line is silently failing, i'm going to use an alternate method I read on S.O.:
set wb = GetObject("C:\Users\John\Desktop\ScheduledJobsfromIsaac\Availability.xlsb")
set excelapp = getobject(,"excel.application")
excelapp.enableevents=true
excelapp.visible=true
excelapp.windowstate = -4137 'value for constant xlMaximized acccording to msdn
excelapp.caption = "Running AVAILABILITY - Please WAIT..."
excelapp.run "Availability.xlsb!ChangeAndCopyFile"
wb.close(true)
excelapp.caption = "10 seconds to close - please WAIT..."
wscript.sleep 10000
excelapp.displayalerts=false
excelapp.quit
probably, it helps if you can describe where and how you execute your vbscript above? is this coded in a .vbs file and execute with wscript.exe or cscript.exe, or embedded in a HTML or HTA?
If this is scripted in HTML, what browser are you using to view that? Please take note that almost all modern browsers will just ignore the vbscript silently.
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
The usual approach to create a Word document from Excel VBA:
Set WD = CreateObject("Word.Document")
results in an error when run with Excel 2011.
Any idea how a Word document can be created in Excel 2011 with VBA?
(I do not want to use AppleScript as I want the program to be able to run on PCs also.)
The following code, based on the suggestion by bretville, seems to work both on mac (tested on Excel2011) and on pc (tested on Excel2007) and can be run repetedly, thus allowing creation of multiple Word files. What is required for the code to work under Excel2011 (mac) vba is a means to test if Word already is running or not. The approach is perhaps not the most elegant, but it works.
Dim word As Object
Dim doc As Object
On Error Resume Next
Set word = GetObject(, "word.application") 'gives error 429 if Word is not open
If Err = 429 Then
Set word = CreateObject("word.application") 'creates a Word application
Err.Clear
End If
word.Visible = True
Set doc = word.documents.Add
You may interchange the two Set word statements, which might seem preferable as no error code handling becomes necessary when run on pc, but the code for some reason then runs terribly slowly on the mac.
For a PC I would do this:
Dim word As Object: Set word = CreateObject("word.application")
word.Visible = True
Dim doc As Object: Set doc = word.documents.Add
The first part is now working [
I have the following which just seems to hang; the part that adds/deletes the module works when running in VBA
I note that I'm prompted with a dialog saying 'this workbook contains links to other data sources' which I ok to, then it hangs
So I tried setting the second argument to 0 and also tried 2 but still it hangs
(2nd arg is UpdateLinks as can be found here )
]
dim objExcel
dim objWorkbook
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open( "H:\M\X\C.xls", 0 , , ,"PASSWORD!" )
Const modpath = "H:\M\V\"
Const modtest = "TEST.cls"
Const modname = "TEST"
On Error Resume Next
Dim vbcomp
Set vbcomp = ActiveWorkbook.VBProject.VBComponents(modname)
objWorkbook.VBProject.VBComponents.Remove vbcomp
objWorkbook.VBProject.VBComponents.Import modpath & modtest
objWorkbook.Save
objWorkbool.Close
set vbcomp = nothing
set objworkbook = nothing
set objExcel = nothing
edited again 14/04/2009
I have now also allowed the 'tools - macro - security - vbproject access'
The script now finishes, however, when trying to open the xls to see if the changes have been made, I get a message informing me that the sheet is locked by "account used to run script"; open 'read only'/notify
Why isn't it releasing control correctly**?**
First thought: Does your workbook already contain a reference (within VBA) to the "Microsoft Visual Basic for Applications Extensibility" library? You'll need it to be able to talk to the VBProject object. Actually, you probably do have the reference if your code works in VBA.
Second thought: ActiveWorkbook is probably not defined outside of an actual workbook. Try replacing it with your objWorkbook object.
Here's a third thought. Did you try setting the Application's DisplayAlerts property to FALSE before you include the module?
The edited script works.
The problem was caused by the fact that I was supplying the password at the workbook level and not at the VBA project level.
A quick search on the web reveals that it is not possible to do this anyway (sendkeys etc) so after manually removing the password on the project, the problem is solved