Open Excel workbook as read-only via VB6 - excel

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.

Related

Set A Connection String In Excel To A Cell Value

I'm creating a workbook for some folks to be able to manipulate some data, get blessings from all involved, then hand off to someone else to manually enter back into the system... Don't ask, we are afraid of scripting back to the system.
I am attempting to do this in the Data Connections Properties Window.
Data > Connections > Properties > Definition (I can't attach a screen shot cause I'm a noob I guess)
I have many different work arounds. I have even created a VB script that will do this, allow users to change server targets, pass different security credentials, but, just this last Monday there was some kind of office patch that screwed up all activeX and I had to go wipe out a temp file to correct it. I can't do that with a bunch of users that are less than tech savvy that are geographically separated, I don't have RDP authority.
For example, the connection string currently looks like:
DRIVER={iSeries Access ODBC Driver};
System=myserver.network.net;
I was hoping to set it to look like:
DRIVER={iSeries Access ODBC Driver};
System="Overview!D9";
The cell, Overview!D9, could then be changed by who ever to the correct system string, like myserver.network.net or server1.network.net.
Near as I can tell from my research, the connection properties window is pretty cut and dry and this might not be possible. But figured I would ask around.
Assuming System is just a string, then getting the value is simple.
' Assuming System is a String
Dim System As String
System = ThisWorkbook.Worksheets("Overview").Range("D9").Value
If System is part of another string, its much the same
' Get the value to use in the connection string
Dim System As String
System = ThisWorkbook.Worksheets("Overview").Range("D9").Value
Dim ConnectionString As String
ConnectionString = "DRIVER={iSeries Access ODBC Driver}; SYSTEM=" & System & ";"
If security is a problem, and your people generating/manipulating the data don't NEED the macros to do their work (ie, just the person entering the data back into the system does) then you don't need to put the macros in their workbooks.
You can add your macro's to the "Default Workbook" on the data entry person's computer. These macros will then be available to all workbooks they open on their computer.
Your data entry person gets the benefit of scripted automation and your users don't have to worry about email or anti-virus problems.

Excel crashes, VBA userform cannot save

I have a userform in Excel in which the user enters information and then hits an "add stock" button. Upon pressing this button, the information is entered into a spreadsheet and then the spreadsheet is saved with "ActiveWorkbook.Save".
The problem is that the work computers are old and Excel has a tendency to crash. When the spreadsheet is autorecovered, the add stock function no longer works, it crashes with a code 75 error. It seems that ActiveWorkbook.Save doesn't work in this case, until the user manually hits CTRL-S. The boss is adamant that our users are not computer savvy enough to manage this so I need to somehow check if Excel has crashed and if so automatically save the file before they start using it.
How would I check if we're in an autorecovery state, and then save it (without ActiveWorkbook.Save) so that the user can continue using the form without issues? Many thanks.
I can't find a direct way to check this, but here are a couple of kludgy options. It seems that if there are two states a workbook that an Autorecovered workbook can be in: Last saved by user or Autosaved. The caption reflects which state it's in. This function will check for the existence of that phrase in the caption.
Public Function IsInAutoRecoverMode(wb As Workbook)
Dim wn As Window
Set wn = wb.Windows(1)
IsInAutoRecoverMode = _
wn.Caption Like "*[Autosaved]?" Or _
wn.Caption Like "*[Last saved by user]?"
End Function
I'm not sure if there are more states that just this and this will certainly fail in non-English environments, so use with care.
When Last saved by user, the Workbook.Path property is where the file is stored. When Autosaved, the Workbook.Path property is equal to Application.AutoRecover.Path. Well, they're equal on my machine. That may just be a coincidence, but I doubt it.
Because the Last saved by user Path isn't distinctive, you can't use it to determine if you're in Autorecover mode. But if the user open the Autosaved workbook, you probably don't want to just Save, but rather Save As in the right location. You might need to use both these techniques to get the solution you want.
Make sure you tell your boss that these work-arounds because you're so industrious and inventive, but they are not documented and might fail at any time. At least, I wouldn't use them without understanding that they're based on some guesses. Let us know what you end up with.

INsert value with Asp to excelsheet and call function?

I've got a real problem..
I got an webpage writen in traditionel Asp. What i need to do now is to insert a value to a field in a excelsheet. The thing is that the excelfile also contains som sub routines and i also what to call one of thoose routines after i inserted the value in the field.. Is this even posible?
Best regards
It's all possible (although you may have problems with security in automating Office from ASP, since the IUSR user generally does not have permission to do much), but it's not a great idea, since Office is not designed to be used in a multi-threaded / multi-user environment. If this is for an intranet, then it may be OK. If it's for a public website, I'd suggest finding some other route.
Those issues aside, driving Excel from ASP is no different from driving it from VBA, VBScript or VB. You can open workbooks, make changes, even call methods defined as VBA within the workbook. For the latter, use the Application.Run method.
EDIT: some sample code (from memory - may be full of syntax errors!)
Set oExcelApp = CreateObject("Excel.Application")
Set oWorkbook = oExcelApp.Open("file.xls")
Set oWorksheet = oWorkbook.Worksheets(1)
oWorksheet.Cells(1,1).Value = "foo"
oExcelApp.Run "'file.xls'!macroname"
oWorkbook.Save
oWorkbook.Close
oExcelApp.Quit

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.

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