TransferSpreadsheet or OutputTo question

  • Thread starter Thread starter GD
  • Start date Start date
G

GD

I'm trying to transfer query results to an existing Excel spreadsheet.
Here's my problem:

I want to use the spreadsheet as a template, with formulas active in rows
1-9 and headers in row 10. Can I set up a macro in Access to open the
spreadsheet, transfer the query results (no header) to it, but starting at
row 11 in Excel???

Thanks for the knowledge!!
 
It can be done use VBA and Automation. It is fairly involved and takes quite
a bit of VBA code. If you are experienced with VBA, let me know and perhaps
we can work through this together.
 
Thanks for the offer, Klatuu!! I'm experienced with basic code, but would
like to learn more. If you have time to walk me through it, I'd be ever so
grateful.

P.S. Was Klatuu the robot from "The Day the Earth Stood Still"?
 
Okay, here is a fairly high level overview, because there is more code than I
can post.

This first part is code you can paste into a standard module. It is useful
for opening an existing workbook or creating a new workbook.

Option Compare Database
Option Explicit

' Declare necessary API routines:
Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As Long) As Long

Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Sub GetExcel()
Dim MyXL As Object ' Variable to hold reference
' to Microsoft Excel.
Dim ExcelWasNotRunning As Boolean ' Flag for final release.

' Test to see if there is a copy of Microsoft Excel already running.
On Error Resume Next ' Defer error trapping.
' Getobject function called without the first argument returns a
' reference to an instance of the application. If the application isn't
' running, an error occurs.
Set MyXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear ' Clear Err object in case error occurred.

' Check for Microsoft Excel. If Microsoft Excel is running,
' enter it into the Running Object table.
DetectExcel

' Set the object variable to reference the file you want to see.
Set MyXL = GetObject("c:\vb4\MYTEST.XLS")

' Show Microsoft Excel through its Application property. Then
' show the actual window containing the file using the Windows
' collection of the MyXL object reference.
MyXL.Application.Visible = True
MyXL.Parent.Windows(1).Visible = True
' Do manipulations of your file here.
' ...
' If this copy of Microsoft Excel was not running when you
' started, close it using the Application property's Quit method.
' Note that when you try to quit Microsoft Excel, the
' title bar blinks and a message is displayed asking if you
' want to save any loaded files.
If ExcelWasNotRunning = True Then
MyXL.Application.Quit
End If

Set MyXL = Nothing ' Release reference to the
' application and spreadsheet.
End Sub

Sub DetectExcel()
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024
Dim hWnd As Long
' If Excel is running this API call returns its handle.
hWnd = FindWindow("XLMAIN", 0)
If hWnd = 0 Then ' 0 means Excel not running.
Exit Sub
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage hWnd, WM_USER + 18, 0, 0
End If
End Sub


This is how you copy data to a specific location. The cell reference you
give it tells you were it starts. It will populate the sheet with all the
data in the recordset:

'Load Header Data
xlSheet.Cells(2, 1).CopyFromRecordset rstItms
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top