How to get excel to insert a new image with Applescript? - excel

I have the following Applescript:
tell application "Microsoft Excel"
tell active workbook
make new picture at end with properties {save with document:true, file name:"/Users/yuval/Pictures/foobar.jpg"}
end tell
end tell
According to several sources, this should work.
However, every time I run the script I get the message
Microsoft Excel got an error: Can’t make class picture.
In the description, I see the error
error "Microsoft Excel got an error: Can’t make class picture." number
-2710 from picture to class
After two hours of searching, I have absolutely no clue what to do. Any help would be greatly appreciated!
Details:
Running Microsoft Excel 2011 on Mac OS X 10.7 Lion

Two things, though the caveat here is that I am working in Excel 2008, but Applescript implementations don't change substantially without fanfare:
First, make sure you are using the proper file path format. Applescript historically works with HFS paths, not POSIX
set theFilePath to "Macintosh HD:Users:UserName:Path:To:File.pic" --choose file
Second, make sure you are acting on the appropriate object. I was able to make this work with active sheet because active workbook wasn't a property in the Dictionary:
tell application "Microsoft Excel"
tell active sheet
make new picture at end with properties {file name:theFilePath}
end tell
end tell
The above code combined worked for me.

Related

Error handling for excel addin that cannot find networking drive

I have made an addin and placed it on a shared networking drive.
The addin contains a seperat sub that calls the addin whenever an excel document is opened, so the addin will be available in excel all the time.
The addin works fine when i am in my office and when i am connected, however if i for example work from home, and opens excel, then excel gives me the following error:
"Sorry we couldn't find . Is it possible it was moved, renamed or deleted?"
This is because i am not connected and excel cannot retrieve the path i am trying to call the addin from.
Is there a way to handle this error, so the message dont show up? I have tried to ignore the error so the message dont show up but that does not work. How can i exit the sub before the error appears? or an even better solution is to call custom made addins without using a specific networking drive as location.
I have tried the following code to ignore the error.
Sub Open_up ()
Application.DisplayAlerts = False
On Error Resume Next
Call 'name of the addin
End Sub ```
Hope it makes sense
It looks like the add-in was not loaded and not accessible from the shared drive. I'd suggest creating an installer for the add-in, so it could be installed on the end user machines. See Deploy an Office solution by using Windows Installer for more information.

How to tell if your application is Project or Excel with VBA?

I have created a custom class that adds references based on what the macros need to run on the particular Project or Excel file. This class works for both MS Project and Excel. The problem I am having is in the code how can I determine if the application is a Project or Excel file? Currently my code works by default assuming that it is in an Excel file, if an error occurs I handle the error by switching the code from "ActiveWorkbook" to "ActiveProject". Is there any way I can avoid using error handling and just run a check to see what I am in.
Thanks!
#Jeeped's comment points to a simple answer--test the Name property of the Application object. Not in the Immediate window, of course, but in an If statement: If Application.Name = "Microsoft Excel" Then....

Can I access and query web-based Crystal Report Viewer through Excel VBA?

I download reports through web-based access to a Crystal Report Viewer. (Admittedly, my first problem is that I am not at all proficient with Crystal.) I generally have success using Excel VBA in automating IE navigation and HTML form manipulation, but I've run into a wall with this Crystal Report Viewer.
As an example, I can download a report through the Crystal Report Viewer by specifying for which of the fifty states I want the report. I am trying to automate it so that it will export all fifty reports at once (or, rather, in succession while I, say, go to lunch).
I've pulled the outerHTML of the site in question. It includes an OBJECT tag at the beginning containing 23 PARAM NAME tags followed by the following VBScript:
Sub window_onLoad()
Page_Initialize()
End Sub
Sub Page_Initialize
On Error Resume Next
Dim webBroker
Set webBroker = CreateObject("WebReportBroker.WebReportBroker")
If err.number <> 0 then
window.alert "The Crystal ActiveX Viewer is unable to create resource objects."
CRViewer.ReportName = "[a URL...redacted]"
Else
Dim webSource0
Set webSource0 = CreateObject("WebReportSource.WebReportSource")
webSource0.ReportSource = webBroker
webSource0.URL = "[a URL...redacted]"
webSource0.PromptOnRefresh = True
webSource0.AddParameter "password", "[somepassword]"
webSource0.AddParameter "user", "[someuser]"
CRViewer.ReportSource = webSource0
End if
CRViewer.ViewReport
End Sub
This script was followed by three more OBJECT tags, each making reference to codebases. I navigated to these references to find DLLs, which I would assume indicate the references I need to invoke in my VBA. On a hunch, I moved the VBScript into Excel VBA (is this dumb?) and placed it after my usual login and navigating code. I get a "Compile Error: Variable not defined" on the lines
window.alert "The Crystal ActiveX Viewer..." 'highlighting the word "window"
CRViewer.ReportName = "[a URL...redacted]" 'highlighting "CRViewer"
CRViewer.ReportSource = webSource0 'highlighting "CRViewer"
CRViewer.ViewReport 'highlighting "CRViewer"
This is where I run into problems. First, I may be going about this the wrong way, or it may not even be possible; but I wouldn't know. Second, if I am on the right track, there are A LOT of Crystal references listed in Excel VBA's available references. I have no idea which ones to use. BTW: the codebases make reference to
crviewer.dll
sviewhlp.dll
swebrs.dll
xqviewer.dll
cselexpt.ocx
crviewer.dep
crviewer.oca and
reportparameterdialog.dll
Any help would be appreciated. I realize I may not have provided all the necessary information here. Please let me know if more is needed. Thanks for reading.
Is the Web viewer part of a wider Business Objects setup, or something more basic that simply launches a viewer with the report?
If it's the former, you already have easier options for automating distribution via the web-based CMC, wherein report refreshes can be scheduled with a variety of distribution options, including to UNC paths.
If its the later, or for some reason the standard CMC/BO options aren't suitable, you might consider using a code library made for this purpose. The splinter library for Python is one such, and my preferred package for web automation: http://splinter.cobrateam.info/
If VBA is your coding comfort zone, there are a few ways of integrating python into VBA, all merely a google away... I won't detail them here since that's a sideline to this, and my answer is more of an alternative than a direct answer.

Excel 2003 on 64-bit Windows 7 automatically changes reference to SysWOW64\MSCOMCTL.OCX so 32-bit Excel complains

In an Excel 2003 VBA project I am using controls from MSCOMCTL.OCX. That is the VBA project has a reference to System32\MSCOMCTL.OCX.
When I open that project in Excel 2003 on my 64-bit Windows 7 system, Excel automatically changes the reference to SysWOW64\MSCOMCTL.OCX (which is the correct location).
However, when I send that project to my client who is using 32-bit Windows XP, the project complains during opening because SysWOW64\MSCOMCTL.OCX does not exist on his system.
Here are the (unsatisfactory) solutions I came up with so far:
Instruct the client to manually change the reference back to the correct location on his system (System32\MSCOMCTL.OCX).
This does not really work for the following reasons:
When Excel 2003 32-bit opens the sheet and it cannot find the reference to MSCOMCTL, it removes all the controls that came from the library (e. g. TreeCtrl) from the forms :-(
Client is struggling with the procedure and it is quite cumbersome for him.
Automatically correct the reference using VBA's VBProject.References.AddFromFile/AddFromGuid.
Same problem as above: When compilation of VBA during opening of workbook fails, Excel will remove all controls that it could not find from the forms.
Automatically add the reference (as in 2.) and use dynamic binding to add all the relevant controls during runtime.
This could actually work, however currently I am struggling with binding the event handlers to the controls (but that will be separate question ;-)
Approaches 1. and 2. do not really solve anything and solution 3 is a lot of work.
Any ideas would be greatly appreciated.
What if you automatically turned the reference off when the workbook closed? that way the reference wouldn't be 'broken' when the workbook is opened, and all your control should still be good.
i.e. :
Private Sub Workbook_Open()
'use environ variable for folder locs
If os = "64bit" Then
Me.VBProject.References.AddFromFile ("C:\WINDOWS\SysWOW64\MSCOMCTL.OCX")
Else
Me.VBProject.References.AddFromFile ("C:\WINDOWS\system32\MSCOMCTL.OCX")
End If
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
For Each ref In Me.VBProject.References
If ref.Name = "MSComctlLib" Then
Me.VBProject.References.Remove ref
End If
Next ref
End Sub
I did a quick test with the ADODB dll and it seemed to work,but I am not sure how you're using that DLL specifically; let me know if that works, though! Sure a lot better than option 3!

Check if Excel workbook is open using VBScript

I have a vbs script which writes to an Excel spreadsheet. To be able to save the spreadsheet I need to ensure that it is not already open.
Can someone suggest the best way to do this?
Some research:
I tried to create a Word Tasks objects to show me all running processes, but on Windows 7 it only tells me whether Excel is running or not.
Creating an Excel.Application object does not give me access to all running instances of Excel.
I thought about opening the spreadsheet for writing but the only way I can think of doing this is with the File System Object OpenTextFile method which does not seem to be the correct approach as the Excel file is a binary.
Any other ideas?
Since the Excel.Application object doesn't give you access to all instances of Excel, what about using a simple error trap? ie. if it is unable to save the file, throw a warning message, save as another file name, etc.

Resources