Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Linking Office Programs with VBA

Posted on 2014-02-16
4
Medium Priority
?
436 Views
Last Modified: 2014-02-16
I have an application written in MSAccess 2010.  It is a large and complex application and it works fine.  I distribute this application with the MSAccess runtime environment.  This environment only contains the Access runtime engine and no additional MS Office software.

I have a user request to add a feature to dynamically create an MSExcel spreadsheet with data from the application.  To include this feature I have to link the Microsoft Excel 14.0 object library so that calls to Excel.Application, and all the workbook, sheet, and cell functionality can be used.

The problem that I have is that if I include this library, then the software won't run on machines that have NO excel installed, even if I don't use this functionality.  It is just a matter of the dynamic linking at load time.  Even more so, if the user has excel 2007, 2003, or any other than 2010, the code won't load either.

So, what I'm looking for is a way to handle load time errors, just like I can handle runtime errors with an "on error" statement.  If I can handle these load time errors, then I can add conditional code that will allow calls to the appropriate routines if needed/allowed/desired.

Anybody have a clue?
0
Comment
Question by:samjomoore
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Norie
ID: 39863635
You don't need the reference if you use late-binding, so that would take care of the version problem.

Not sure about how you would handle the case when a user doesn't have Excel installed.

Actually what you could do is something like this.
Dim xl As Object

    On Error Resume Next
    Set xl = CreateObject("Excel.Application")
    On Error Goto 0

    If xl Is Nothing Then
        MsgBox "This function requires Excel to be installed and available.
        Exit Sub
    End If

Open in new window

0
 
LVL 1

Author Comment

by:samjomoore
ID: 39863638
Awesome!  How to I use late binding?
0
 
LVL 35

Accepted Solution

by:
Norie earned 2000 total points
ID: 39863664
Well one of the first things you do in late-binding is to change declarations like this,
Dim xl As Excel.Application
Dim ws As Excel.Workbook
Dim rng As Excel.Worksheet

Open in new window

to this.
Dim xl As Object
Dim wb As Object
Dim ws As Object

Open in new window


You would also change something like this,
Set xl = New Excel.Application

Open in new window

to this.
Set xl = CreateObject("Excel.Application")

Another important thing is that if your code uses any of the constants that the other application uses, eg xlUp, xlShiftToLeft etc, then you either need to declare them yourself or replace them with their values.

This is how you would declare xlUp and xlShiftToLeft.
[code]
Const xlUp = -4162
Const xlShiftToLeft = -4159

Open in new window


Those are the main things you need to do.
0
 
LVL 1

Author Closing Comment

by:samjomoore
ID: 39863667
Awesome!  Thank you so much!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
Quickbooks hosting can do wonders to your enterprise but considering the points elaborated in the article which will help you to better analyze the outcomes. So scan your business, its needs and then move to the new world of limitless benefits.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question