473,395 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Export to Excel and open the worksheet

Jim
This should really be simple, but I can't figure it out. I have some
VB that exports a table in an Excel format ("C:\NewReport.xls"). After
the export is done, I simply want to have some code that launches
NewReport.xls in Excel.

How?

Nov 13 '05 #1
1 2864

Jim wrote:
This should really be simple, but I can't figure it out. I have some
VB that exports a table in an Excel format ("C:\NewReport.xls"). After
the export is done, I simply want to have some code that launches
NewReport.xls in Excel.

How?

This is more than what U asked for, but it does include code on how to
open a worksheet - and past data into it.
Private Sub btnGenerateData_Click()
'-------------------------------------------------------------
' Purpose : Inserts query data into existing Excel worksheet.
' Author :
' Phone:
' E-Mail:
' Notes : Must not use a 'WITH' statement when using Excel automation
object. MSKB 199219.
' Must have Reference to Microsoft Excel Library selected.

' Excel File:
'-------------------------------------------------------------
' Revision History
'-------------------------------------------------------------
' Tuesday, October 21, 2003 RGO:
'================================================= ============
' End Code Header block
Dim MyApp As New Excel.Application
Dim dbe As DAO.DBEngine
Dim db As DAO.Database
Dim rec As DAO.Recordset
Dim sql$
Dim strSQL As String
Dim rst As Recordset
Dim DQ As String
On Error GoTo HandleErr
DQ = """"

DoCmd.Hourglass True
Set MyApp = New Excel.Application
Set dbe = CreateObject("DAO.DBEngine.36")
Set db = CurrentDb()

strSQL = "SELECT Rpt10.Maname, Count(Rpt10.Maname) AS [Total
Enroll], Count(Rpt10.Hosp20) AS Hosp20, Count(Rpt10.PCM10) AS PCM10,
Count(Rpt10.PCM20) AS PCM20, Count(Rpt10.Spec20) AS Spec20,
Count(Rpt10.Spec40) AS Spec40 " _
& "FROM Rpt10 " _
& "GROUP BY Rpt10.Maname " _
& "HAVING (((Rpt10.Maname) Is Not Null));"

Set rst = CurrentDb.OpenRecordset(strSQL) '
Pointer to Recordset
MyApp.Visible = True ' Make
Excel visible

MyApp.Workbooks.Open ("C:\Documents and Settings\A072411\My
Documents\Databases\Network Sizing\template.xls") ' Open an existing
workbook, Local testing
' MyApp.Workbooks.Open ("\\smb\labshare\databases\GenLab_3100.xls")
' Open an existing workbook
MyApp.Worksheets("Data").Activate ' Activate
the proper Worksheet
MyApp.Range("A1").Select '
Activate the proper cell
MyApp.ActiveCell.CopyFromRecordset rst ' Use
Excel's CopyFromRecordset Method

Set rst = Nothing
Set MyApp = Nothing
DoCmd.Hourglass False
DoCmd.Beep
ExitHere:
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "Form_frm3100SampShts.btnGenerateData_Click"
Set MyApp = Nothing
MyApp.Quit
DoCmd.Hourglass False
End Select

End Sub

Nov 13 '05 #2

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

Similar topics

4
by: Anthony Cuttitta Jr. | last post by:
I'm working on some procedures where Access queries are exported to Excel, and then later on, those same workbooks are openned, and I need to target a specific original sheet. Sometimes there will...
4
by: Hans [DiaGraphIT] | last post by:
Hi! I want to export a dataset to an excel file. I found following code on the net... ( http://www.codeproject.com/csharp/Export.asp ) Excel.ApplicationClass excel = new ApplicationClass();...
8
by: DC Gringo | last post by:
I have a simple button that should open another window and export a datagrid to an Excel file. I'm getting: "Name 'window' is not declared." What do I need to declare or import? <INPUT...
3
by: Agnes | last post by:
Dim dsExcelExport As New System.Data.DataSet Dim daExcelExport As New System.Data.SqlClient.SqlDataAdapter Dim Excel As New Excel.Application Dim strExcelFile As String Dim strFileName As...
6
by: Elena | last post by:
I'm trying to export data to an Excel worksheet. I can export the data in the cell values perfectly. I need the code to change a header and footer for the worksheet, not for the columns. Is...
0
by: devolper | last post by:
I am devolping a web application which which .net reports to a excel work book .each report in one excel sheet.i am using the following code.......... Dim objDestinationExcel As New...
1
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
3
by: jmarcrum | last post by:
I want to export a report (that contains two separate queries, 1. Current year data, and 2. split-year data) from access into excel, but everytime I run my code and export the data to excel, it looks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.