Excel 2016 file ignoring links and grant access using applescript - excel

The problem i am having is that i am always receiving new spreadsheets and if i run the code below.
set resultWorkBook to "/Users/x/Desktop/myfile.xmls"
tell application "Microsoft Excel"
set resultWorkbook to open workbook workbook file name posix_file without update links
end tell
It will try to open the spreadsheet but then come up with the dialog window asking whether to grant access or not.
If I run the same spreadsheet like this.
tell application "Finder
set filecontents to file of folder Desktop of (path to home folder)
end tell
tell application "Microsoft Excel"
set display alerts to false
set resultWorkbook to open filecontents
end tell
It will open the workbook without asking for access but then comes up asking me to choose one of three options regarding links in the spreadsheet.
I really need the best of both worlds, and wondered if anyone could give me a solution.
Thanks

Just convert the posix path (a string) to a file object, like this
set posix_file to "/Users/x/Desktop/myfile.xmls" as POSIX file
tell application "Microsoft Excel"
set resultWorkBook to open workbook workbook file name posix_file without update links
end tell
Update; the difference is the as POSIX filecommand.

Related

Excel VBA read-only status explanation when a workbook set to this value

I want to make sure that affecting a file with this value is for the other users attempting to write inside the file.
My aim is to prevent my file (as a Server file) to be written by others on Windows Server whereas the Client file XLSM is using this Server file.
Is that correct ? Right now I'm developping the Macros application in stand-alone mode at home office. I cannot test the interaction with other users.
Thanks in advance.
Regards.
If you set a file as Read-only in the operating system then it cannot be changed by any user. If you open a file then it is read-only to any other user, unless filesharing is explicitly enabled. If you open a file Read Only (by using code like
Set wb = Workbooks.open(Filename:=myfile, ReadOnly:=True)
Then it is set to read-only for that user. None of this will prevent the user who opened it read-only from saving a copy of the file under a different name.
Is that what you wanted to know?

Excel 2013 vba created workbook opens for users in "Protected View"

I have VBA code that spins through a worksheet and creates workbooks that are emailed to clients. It's pretty simple, really, it writes the output files to My Documents. Here is the save code:
With wkbOutputBook
.CheckCompatibility = False
.Save
.Close
End With
When the worksbook is opened, Excel says:
PROTECTED VIEW Office has detected a problem with this file. Editing it may harm your computer. Click for more details.
OK I know what this means, for reasons unknown Excel thinks this file was sent through Outlook, or downloaded from the Internet. But it wasn't. I've tried the following:
Save in different versions/file formats
Use Save As instead of Save
Explicitly turn off protected mode. Protected mode isn't the same thing as Protected View though.
Surely "My Documents" isn't considered an "unsafe" location.
Thanks.
If you can accept files being saved in read only mode, this should probably eliminate an error message.
Dim path As String
path = "full-path-with-filename-and-extension"
With wkbOutputBook
.SaveCopyAs (path)
SetAttr (path), vbReadOnly
.Close
End With
You try .SaveCopyAs (path) method without code line involving setting read only, but I guess it won't help.
What format do you try to save your file as? Does the problem appear only on your PC or have You tested your macro / output files on other PCs? The problem can involve stuff in system registry.

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

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.

Open Excel workbook as read-only via VB6

I have an application written in VB6 that writes data to a spreadsheet. I'm using the MS Excel 11.0 Object library to create an instance of Excel and open the book:
Dim xlApp As Excel.Application, remoteBook As Workbook
Set xlApp = New Excel.Application
Set remoteBook = xlApp.Workbooks.Open(sheetName)
In addition to writing to the workbook "sheetName", the program also allows the user to launch the workbook in order to view the cumulative results.
There is a chance, however slim it may be, that a user could have the workbook open for viewing the results while someone else is trying to write to it. I want to give the user writing to the sheet priority. Is there a way I can launch the sheet for viewing as read-only? There is a read-only property of the excel application object, but it is (of course) read-only.
How can I set up my program to write data to the workbook even if someone has accidentally left the file open at their desk?
Simply do this:
Set remoteBook = xlApp.Workbooks.Open( sheetName, , true)
Where true is whether or not to open as Read Only. ReadOnly is the third parameter to this method.
I think you might be able to do it via the Workbook.ChangeFileAccess method as described here. Not sure if it will suit your circumstances though.
Let me make sure I have properly interpreted your issue:
Your app writes an excel file
The App launches the file in Excel
to the User
Now here's what I think you're saying:
Once the user is viewing the sheet, they may or may not want to edit that sheet.
In other words, you don't want to use
Set remoteBook = xlApp.Workbooks.Open( sheetName, , true)
100% of the time because the user viewing may want to change the data.
The downside is that this dastardly user may leave the file open to prevent other users from writing to that file.
Is that correct?
If so, it sounds like you may need to explicit state in your app to "Open for viewing" or "open for read-only" access and then toggle the Read Only property appropriately; which is probably undesirable.
However, you can't force a save on an office doc once someone else has it open.

Error 70 in Excel VBA

I am trying to upload directly a picture/chart from excel to a Sharepoint group URL. Here is the script:
Sub ExportChartJPG()
ActiveChart.Export Filename:="http://sharepoint.ap.xxxxxxxxxxxxxx.com/xxxxxx/xxxxxxxxxxxxxx/Pictures/MyChart.jpg", _FilterName:="jpeg"
End Sub
Is that possible? If it's not then can you suggest another way of doing it? Thanks
You can only export to a file, not to a URL. So, you could export to a temporary file on disk, and then submit the file to your web server. You would of course need the web server to have the ability to receive files.
Hang on, from the URL, it's a SharePoint server, yes? Presumably a SharePoint document library? In that case, you need to write some code to use one of the following techniques to upload the file:
SharePoint Web Service
WebDAV
FrontPage Extensions
If you want to do this in VBA, then the MSXML3 library may be useful, since it will let you do HTTP requests.
EDIT: OK, based on your comments, here's a simple VBScript script to get you started. This opens an Excel workbook at a known location, and exports the first chart sheet.
Save this as "test.vbs" and then double-click on it to run it (having created a suitable Excel file, etc.).
Dim oExcel : Set oExcel = CreateObject("Excel.Application")
Dim oWorkbook : Set oWorkbook = oExcel.Workbooks.Open("C:\test.xls")
Dim oChart : Set oChart = oWorkbook.Charts(1)
oChart.Export "C:\chart.jpg", "JPEG"
oWorkbook.Close False
oExcel.Quit
As I said in my comment, VBScript is very much like VBA, but the downside is that there's no strong typing, so you don't get Intellisense, etc. It might be easier to create the script in VBA where you do have Intellisense (and a debugger, etc.) and then "port" it to VBScript.

Resources