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
Related
I've seen some similar problems described by others before but no real solution. And I'm sure there is one.
I have a .XLA-add in configured to be loaded when I open up Excel. It works fine when I open documents or Excel it self. However, when my BI-system programmatically creates and opens an Excel-file the add-in does not get loaded. The BI-system opens Excel in a new instance so it does not help to have opened Excel on beforehand (and thereby the .XLA-add in)
If i Check Options-Add Ins it looks like the add-in is loaded but it is not!
What I've tried (and that does work) is to insert this function into the created excel-file and "reload" the add-ins, but I want to find an automated solution!
Function ReloadXLAddins(TheXLApp As Excel.Application) As Boolean
Dim CurrAddin As Excel.AddIn
For Each CurrAddin In TheXLApp.AddIns
If CurrAddin.Installed Then
CurrAddin.Installed = False
CurrAddin.Installed = True
End If
Next CurrAddin
End Function
Is there any way to load my Add ins automatically when instancing excel programmatically?
Any tips, solutions or workarounds are highly appreciated!
Best regards.
This may not be possible in VBA. I have come across this problem before, and used the implementation found here: Excel interop loading XLLs and DLLs
However, that solution is for C#. The steps required may not be possible when coming from inside of an Excel VBA script. Perhaps there are some VBA implementations you can look at, but wanted to give you some sort of starting place because I know that is a frustrating place to be.
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.
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!
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.
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.