473,403 Members | 2,071 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,403 software developers and data experts.

Export to Excel works - but manipulate the worksheet

Hi,

I am using:

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,
lcQueryName, strFilePathNamE, True

....to export my query data to an existing excel workbook. Then what i
need to do is sort the data and then copy and paste it to another
sheet in the workbook. I have managed to do similar things bofore but
only when i use the line:

DoCmd.OutputTo acExport, lcQueryName, , strFilePathNamE, True, ""

....this if i am not mistaken creates a new workbook - the problem
there being that i need to use an existing workbook.

In the past i have recorded a macro in Excel and then ripped the code
and put it into Access VBA then placed a snipit of code before each
line to make it work. But it isn't working this time.

Any ideas?
Cheers,
Nov 13 '05 #1
1 2538
If you can instantiate and Excel workbook object like so : Dim
objXLApp As Object, objXLBook As Object
then set the values to the files you want like so: Set objXLApp =
CreateObject("Excel.Application")
and Set objXLBook =
objXLApp.Workbooks.Open("\\server\existingfile.xls ")
Then go crazy and add sheets or edit existing sheets like so:
objXLBook.Sheets(0).Select
With objXLBook.worksheets(0)
End With
That might get you going in the right direction.
I have an export procedure that pastes data into a newly created
workbook and modifies formatting and the like.

Public Sub outToNSIR(strNSIRID As String)

Dim rsNSIR As Recordset, intA As Integer, objXLApp As Object, objXLBook
As Object, intb As Integer, _
strA As String, varOutVal
Set rsNSIR = CurrentDb.OpenRecordset("Select * from tblNSIR where
NSIR_ID = '" & strNSIRID & "'")

rsNSIR.MoveFirst
Set objXLApp = CreateObject("Excel.Application")

Dim blFndLstFil As Boolean, strLstFil As String, intLstDat As Integer

intLstDat = 0

While Not blFndLstFil
strLstFil = Dir("\\netltnm02\bridb\- Q-SCORE 2003\03.ExPRESS
Tools\NSIR template\NSIR_Form_" & Format(Date - intLstDat, "mmddyy") &
".xls")

If strLstFil <> "" Then
blFndLstFil = True
Else
intLstDat = intLstDat + 1

End If

Wend
Set objXLBook = objXLApp.Workbooks.Open("\\netltnm02\bridb\- Q-SCORE
2003\03.ExPRESS Tools\NSIR template\" & strLstFil)

If Dir("C:\NSIR", vbDirectory) = "" Then
MkDir ("C:\NSIR")
End If
If Dir("C:\NSIR\output", vbDirectory) = "" Then
MkDir ("C:\NSIR\output")
End If

objXLBook.SaveAs ("C:\NSIR\Output\NSIR_Form_" & strNSIRID & ".xls")

objXLBook.Close

Set objXLBook = objXLApp.Workbooks.Open("C:\NSIR\Output\NSIR_Form_ " &
strNSIRID & ".xls")

objXLApp.Visible = True
For intb = 2 To objXLBook.worksheets.Count - 1

objXLBook.Sheets(intb).Select

With objXLBook.worksheets(intb)
If intb = 2 Then

.Range("A7:A7").EntireRow.Insert

.Cells(7, 1).Interior.ColorIndex = 33
.Cells(7, 2).Interior.ColorIndex = 33
.Cells(7, 3).Interior.ColorIndex = 33

.Cells(7, 1) = "NSIR Tracking Number:"
.Cells(7, 2) = "x"
.Cells(7, 2).Font.ColorIndex = 33
.Cells(7, 3) = strNSIRID
.Cells(7, 3).Font.Bold = True
.Cells(7, 3).Font.Size = 10

Else

.Range("A4:A4").EntireRow.Insert

.Cells(4, 1).Interior.ColorIndex = 33
.Cells(4, 2).Interior.ColorIndex = 33
.Cells(4, 3).Interior.ColorIndex = 33

.Cells(4, 1) = "NSIR Tracking Number:"
.Cells(4, 3) = strNSIRID
.Cells(4, 3).Font.Bold = True
.Cells(4, 3).Font.Size = 10

End If

End With

Next

For intA = 0 To rsNSIR.Fields.Count - 1

For intb = 1 To objXLBook.worksheets.Count

With objXLBook.worksheets(intb)

strA = ""

If Not IsNull(rsNSIR(intA)) And Not IsNull(DLookup("Field",
"tblFieldNamesXRef", "[DB_Field] = '" & rsNSIR.Fields(intA).Name &
"'")) Then

On Error GoTo FindErr

strA = .Cells.Find(What:=DLookup("Field",
"tblFieldNamesXRef", "[DB_Field] = '" & rsNSIR.Fields(intA).Name & "'")
_
, SearchOrder:=1, LookAt:=1,
SearchDirection:=xlPrevious).Address

FindErr: Resume Next
If strA <> "" Then

objXLBook.Sheets(intb).Select

strA = "C" & Right(strA, Len(strA) - 3)
If IsDate(rsNSIR(intA)) Then

If InStr(1, rsNSIR(intA).Name, "Date") Then

varOutVal = Format(rsNSIR(intA),
"mm/dd/yy")
Else

varOutVal = "'" & rsNSIR(intA)
End If

ElseIf rsNSIR(intA) = True And rsNSIR(intA).Type =
1 Then

varOutVal = "yes"

Else
varOutVal = rsNSIR(intA)

End If

.Cells(Int(Right(strA, Len(strA) - 1)), 3) =
varOutVal

End If

End If
End With

Next

Next

If Dir("C:\NSIR\Output\", vbDirectory) = "" Then
MkDir ("C:\NSIR\Output\")
End If

objXLBook.Save
MsgBox "Output procedure complete."

End Sub

Nov 13 '05 #2

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

Similar topics

1
by: Matt | last post by:
I have an ASP page that calls ASP routines that I created that execute a database query and return the results to a recordset. I then iterate through the recordset and display the data in a table....
5
by: Maria L. | last post by:
Hi, I need to export the content of a DataGrid (in Windows application in C#), into an Excel spreadsheet. Anyone knows how to do this? Any code snippets would help! thanks a lot, Maria
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...
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...
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...
1
by: DennisBetten | last post by:
First of all, I need to give some credit to Mahesh Chand for providing me with an excellent basis to export data to excel. What does this code do: As the title says, this code is capable of...
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
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
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...
0
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,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.