473,698 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Open Excel from Access

I have an Access XP Database. On one of the forms I have a button that
opens and Excel Spreadsheet as follows:

Dim xl As Excel.Applicati on

Set xl = New Excel.Applicati on
xl.Application. Workbooks.Add
xl.Cells(1,1) = "Some Text From the database"

etc.

I have set a reference to the Excel 10 Object Library.

The problem I have is that I have to send this application to different
users in our company, and some have Excel 2000, in which case they get an
error re the Excel 10 Object Library. Is there a way that the code will
work regardless of what version of Excel is on the target pc.

Thanks
Nov 13 '05 #1
3 17475
M Fisher wrote:
I have an Access XP Database. On one of the forms I have a button that
opens and Excel Spreadsheet as follows:

Dim xl As Excel.Applicati on

Set xl = New Excel.Applicati on
xl.Application. Workbooks.Add
xl.Cells(1,1) = "Some Text From the database"

etc.

I have set a reference to the Excel 10 Object Library.

The problem I have is that I have to send this application to different
users in our company, and some have Excel 2000, in which case they get an
error re the Excel 10 Object Library. Is there a way that the code will
work regardless of what version of Excel is on the target pc.


Use late binding:

Dim xl as object
Set xl = CreateObject("E xcel.Applicatio n")

You lose the intellisense while coding so a tip is use the early bound
method you originally used with the reference then change it once you've
finished coding (and remove the reference as you still get a compile
error even if it's not used).

--
[Oo=w=oO]

Nov 13 '05 #2
That works fine - thanks

"Trevor Best" <no****@besty.o rg.uk> wrote in message
news:42******** **************@ news.zen.co.uk. ..
M Fisher wrote:
I have an Access XP Database. On one of the forms I have a button
that opens and Excel Spreadsheet as follows:

Dim xl As Excel.Applicati on

Set xl = New Excel.Applicati on
xl.Application. Workbooks.Add
xl.Cells(1,1) = "Some Text From the database"

etc.

I have set a reference to the Excel 10 Object Library.

The problem I have is that I have to send this application to different
users in our company, and some have Excel 2000, in which case they get an
error re the Excel 10 Object Library. Is there a way that the code will
work regardless of what version of Excel is on the target pc.


Use late binding:

Dim xl as object
Set xl = CreateObject("E xcel.Applicatio n")

You lose the intellisense while coding so a tip is use the early bound
method you originally used with the reference then change it once you've
finished coding (and remove the reference as you still get a compile error
even if it's not used).

--
[Oo=w=oO]

Nov 13 '05 #3
I know of 2 ways to handle this.

1. Do your development from an environment where the MS Office version
is the lowest. In this case, it may be Office 2000. When users with
later versions of Office open the application, the references will
automatically be reset to the later version (i.e., Excel.Applicati on.9
goes to Excel.Applicati on.10). This doesn't seem to work in reverse
(i.e., Excel.Applicati on.10 won't be downgraded to Excel.Applicati on.9
and the application will fail).

2. Remove your reference to Excel. Upon application startup, detect
the current excel version from the registry (uses API call), then add
the appropriate reference programatically . Code for this is below.
You may have to modify it to suit your needs.

Bill Ehrreich

_______________ _______________ _______________ ______
Const REG_SZ = 1 ' Unicode nul terminated string
Const REG_BINARY = 3 ' Free form binary
Const HKEY_CLASSES_RO OT = &H80000000
Const HKEY_CURRENT_US ER = &H80000001
Const HKEY_LOCAL_MACH INE = &H80000002
Const HKEY_USERS = &H80000003

Private Declare Function RegCloseKey Lib "advapi32.d ll" (ByVal hKey As
Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.d ll" Alias
"RegOpenKey A" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult
As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.d ll" Alias
"RegQueryValueE xA" (ByVal hKey As Long, ByVal lpValueName As String,
ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As
Long) As Long
Sub SetExcelReferen ces()
On Error GoTo Trapper
Select Case GetExcelCurrent Version
Case "Excel.Applicat ion.9"
Application.VBE .ActiveVBProjec t.References.Ad dFromFile
("C:\Program Files\Microsoft Office\Office\E xcel9.olb")
Case "Excel.Applicat ion.10"
Application.VBE .ActiveVBProjec t.References.Ad dFromFile
("C:\Program Files\Microsoft Office\Office\E xcel10.olb")
'Add additional cases here
End Select
Exit Sub
Trapper:
If Err.Number = 32813 Then
'The reference already exists. Exit.
Exit Sub
Else
MsgBox Err.Description
End If
Exit Sub
End Sub
Function GetExcelCurrent Version() As String
'Get a string from the registry
GetExcelCurrent Version = GetString(HKEY_ CLASSES_ROOT,
"Excel.Applicat ion\CurVer", "")
End Function
Function GetString(hKey As Long, strPath As String, strValue As String)
Dim Ret
'Open the key
RegOpenKey hKey, strPath, Ret
'Get the key's content
GetString = RegQueryStringV alue(Ret, strValue)
'Close the key
RegCloseKey Ret
End Function
Function RegQueryStringV alue(ByVal hKey As Long, ByVal strValueName As
String) As String
Dim lResult As Long, lValueType As Long, strBuf As String,
lDataBufSize As Long
'retrieve nformation about the key
lResult = RegQueryValueEx (hKey, strValueName, 0, lValueType, ByVal
0, lDataBufSize)
If lResult = 0 Then
If lValueType = REG_SZ Then
'Create a buffer
strBuf = String(lDataBuf Size, Chr$(0))
'retrieve the key's content
lResult = RegQueryValueEx (hKey, strValueName, 0, 0, ByVal
strBuf, lDataBufSize)
If lResult = 0 Then
'Remove the unnecessary chr$(0)'s
RegQueryStringV alue = Left$(strBuf, InStr(1, strBuf,
Chr$(0)) - 1)
End If
ElseIf lValueType = REG_BINARY Then
Dim strData As Integer
'retrieve the key's value
lResult = RegQueryValueEx (hKey, strValueName, 0, 0,
strData, lDataBufSize)
If lResult = 0 Then
RegQueryStringV alue = strData
End If
End If
End If
End Function

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
35549
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet and extract information from specific worksheets and cells. I'm not really sure how to get started with this process. I ran the COM Makepy utility from my PythonWin (IDE from ActiveSTate),
115
14107
by: TheAd | last post by:
At this moment I use MsAccess and i can build about every databound application i want. Who knows about a serious open source alternative? Because Windows will be a client platform for some time, i prefer a solution that (also) supports Windows. On the net I found a number of products that i looked at, but none of them gave me the impression of a serious candidate at this moment (KNoda, Gnome DB Manager, InterBase...). 2 additional...
1
3698
by: Bon | last post by:
Hello all I create a form with three buttons in MS Access 2000. They are Open Excel Template, Save Draft and Save Final. When I click the Open Excel Template button, the Excel template will be opened. Inside the Excel template, I have assigned a draft watermark to the Print icon. When the Print icon is clicked, the draft watermark and print dialog box is shown. After the user print/edit data in the Excel template, s/he has to click
0
1946
by: shaurya.rastogi | last post by:
I need to open an Excel file that has been stored in the Access Database using the insert Object functionality of MSAccess manually. What i am aware of is that i cant just read the field containing the Excel File into a Byte Array and pass it to the Excel object in C#,as the file is wrapped in the OLE Wrapper used by Access while inserting the file in database. I have tried locating the Header of Excel file from the byte array and...
0
1381
by: shaurya | last post by:
I need to open an Excel file that has been stored in the Access Database using the insert Object functionality of MSAccess manually. What i am aware of is that i cant just read the field containing the Excel File into a Byte Array and pass it to the Excel object in C#,as the file is wrapped in the OLE Wrapper used by Access while inserting the file in database . I have tried locating the Header of Excel file from the byte array and read...
4
19762
by: Scott | last post by:
I'm trying to get access to open, refresh and then close an excel spreadsheet for me. Below is the code I'm using to open and close Excel, I just can't get it to refresh my data linked back to my database, the problem appears to be that if I open Excel myself a window pops up and asks to Enable or Disable Automatic Refresh. Any thoughts on how to get the spreahseet to refresh? Dim appexcel As Object Set appexcel =...
8
9649
by: shenkel55 | last post by:
I'm using Access and Excel 2003. Using either the import wizard or code, I have the same problem. This problem only happens with Excel files automatically generated by Corp IT. If I try to do an import and the Excel file isn't open I get the following error: "The wizard is unable to access information in the file "...path info... "Please check that the file exists and is in the correct format." If the files are opened directly in Excel, it...
22
68736
by: robertgregson | last post by:
Using C#, .NET3.5, Visual Studio 2008 and WCF on Windows VISTA SP1, I have written a service, service host (as a C# console application) and a client. The service uses Microsoft.Office.Interop.Excel.Application to access an Excel file on the local hard drive, opens it (using Application.Workbooks.Open), reads out some data and the provides said data to the client. Everything works perfectly when the Service Host is a C# console...
0
1109
by: jzalar | last post by:
I have Access 2007, Excel 2007, Access 97 and Excel 97 running on my Windows XP computer. When I try to open up a Pivot Form in Access 97, Excel 2007 Opens. I can not figure how to make Excel 97 Open when I open any OLE object in Access 97. Any Ideas.
4
14585
by: mld01s | last post by:
Hi all!! I need help, I have been stuck for a few days on this one. I am trying to open an excel table from a command button in Access. The excel table has an auto_open macro, that is supposed to run everytime I open excel. When I navigate to the excel file, and open it, it autoruns the macro with no problems. When I go from Access hit the command, the excel table opens with no problem, but the macro does not auto run on start. Here is...
0
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8856
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4613
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2321
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.