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

Excel Automation: I am confused

Hi all,

I have a problem automating an Excel object from Access.

I have the following fucntion that creates a table in Excel from an
Access query:
Function CreateTable2(strSourceName As String, _
strFileName As String) As Variant

Dim xlApp As Excel.Application
Dim xlWrkbk As Excel.Workbook
Dim xlSourceRange1, xlSourceRange2 As Excel.Range
Dim CurCell As Object

On Error GoTo Err_CreateTable2

' Create an Excel workbook file based on the
' object specified in the second argument.
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
strSourceName, strFileName, False
' Create a Microsoft Excel object.
Set xlApp = CreateObject("Excel.Application")
' Open the spreadsheet to which you exported the data.
Set xlWrkbk = xlApp.Workbooks.Open(strFileName)

'Apply formatting depending on type of table
'Detailtabelle
With xlApp
.Worksheets("qryAbtProbKleiner90_PB1_3").Activate
.ActiveSheet.Range("B7:K7").Select
.Columns("A:K").EntireColumn.AutoFit
.ActiveSheet.PageSetup.Orientation = xlLandscape
' Determine the size of the range and store it.
Set xlSourceRange1 = .Selection.Range("a1").CurrentRegion
With xlSourceRange1
.BorderAround (xlContinuous)
.AutoFormat Format:=xlRangeAutoFormatSimple, Number:=True,
Font _
:=True, Alignment:=True, Border:=True, Pattern:=True,
Width:=True
End With
.ActiveSheet.Range("A1:G18").Select
.Selection.HorizontalAlignment = xlCenter
.Columns("C").EntireColumn.HorizontalAlignment = xlLeft
Set xlSourceRange1 = Nothing

'conditional formatting
'********************
'details to follow
' Save and close the workbook
' and quit Microsoft Excel.
With xlWrkbk
.Save
.Close
End With

xlApp.Quit

Exit_CreateTable2:
If Not (xlSourceRange1 Is Nothing) Then
Set xlSourceRange1 = Nothing
End If
If Not (xlSourceRange2 Is Nothing) Then
Set xlSourceRange2 = Nothing
End If
Set xlWrkbk = Nothing
Set xlApp = Nothing
Exit Function

Err_CreateTable2:

MsgBox CStr(Err) & " " & Err.Description
Resume Exit_CreateTable2
End Function

This works fine, but when I add the following to the above function,
where it says "conditional formatting", I get strange results.

'conditional formatting
.ActiveSheet.Range("g2").Select
Set xlSourceRange2 = .Range(Range("g2"),
Range("g2").End(xlDown))
With xlSourceRange2
.FormatConditions.Add Type:=xlCellValue, _
Operator:=xlLess, _
Formula1:="=Now()"
With .FormatConditions(1).Font
.Bold = True
.Italic = False
.ColorIndex = 3
End With
.FormatConditions.Add Type:=xlCellValue, _
Operator:=xlGreater, _
Formula1:="=ReturnBerichtstermin()"
.FormatConditions(2).Interior.ColorIndex = 6
End With
Set xlSourceRange2 = Nothing
End With

The function executes, but the Excel object is still as a Process in
Task Manager. I guess the object is not being managed properly.

Can someone please have a look where I am going wrong?

Regards,
J

Nov 13 '05 #1
2 2716
Jean wrote in message
<11*********************@g44g2000cwa.googlegroups. com> :
Hi all,

I have a problem automating an Excel object from Access.

I have the following fucntion that creates a table in Excel from an
Access query:
Function CreateTable2(strSourceName As String, _
strFileName As String) As Variant

Dim xlApp As Excel.Application
Dim xlWrkbk As Excel.Workbook
Dim xlSourceRange1, xlSourceRange2 As Excel.Range
Dim CurCell As Object

On Error GoTo Err_CreateTable2

' Create an Excel workbook file based on the
' object specified in the second argument.
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
strSourceName, strFileName, False
' Create a Microsoft Excel object.
Set xlApp = CreateObject("Excel.Application")
' Open the spreadsheet to which you exported the data.
Set xlWrkbk = xlApp.Workbooks.Open(strFileName)

'Apply formatting depending on type of table
'Detailtabelle
With xlApp
.Worksheets("qryAbtProbKleiner90_PB1_3").Activate
.ActiveSheet.Range("B7:K7").Select
.Columns("A:K").EntireColumn.AutoFit
.ActiveSheet.PageSetup.Orientation = xlLandscape
' Determine the size of the range and store it.
Set xlSourceRange1 = .Selection.Range("a1").CurrentRegion
With xlSourceRange1
.BorderAround (xlContinuous)
.AutoFormat Format:=xlRangeAutoFormatSimple, Number:=True,
Font _
:=True, Alignment:=True, Border:=True, Pattern:=True,
Width:=True
End With
.ActiveSheet.Range("A1:G18").Select
.Selection.HorizontalAlignment = xlCenter
.Columns("C").EntireColumn.HorizontalAlignment = xlLeft
Set xlSourceRange1 = Nothing

'conditional formatting
'********************
'details to follow
' Save and close the workbook
' and quit Microsoft Excel.
With xlWrkbk
.Save
.Close
End With

xlApp.Quit

Exit_CreateTable2:
If Not (xlSourceRange1 Is Nothing) Then
Set xlSourceRange1 = Nothing
End If
If Not (xlSourceRange2 Is Nothing) Then
Set xlSourceRange2 = Nothing
End If
Set xlWrkbk = Nothing
Set xlApp = Nothing
Exit Function

Err_CreateTable2:

MsgBox CStr(Err) & " " & Err.Description
Resume Exit_CreateTable2
End Function

This works fine, but when I add the following to the above function,
where it says "conditional formatting", I get strange results.

'conditional formatting
.ActiveSheet.Range("g2").Select
Set xlSourceRange2 = .Range(Range("g2"),
Range("g2").End(xlDown))
With xlSourceRange2
.FormatConditions.Add Type:=xlCellValue, _
Operator:=xlLess, _
Formula1:="=Now()"
With .FormatConditions(1).Font
.Bold = True
.Italic = False
.ColorIndex = 3
End With
.FormatConditions.Add Type:=xlCellValue, _
Operator:=xlGreater, _
Formula1:="=ReturnBerichtstermin()"
.FormatConditions(2).Interior.ColorIndex = 6
End With
Set xlSourceRange2 = Nothing
End With

The function executes, but the Excel object is still as a Process in
Task Manager. I guess the object is not being managed properly.

Can someone please have a look where I am going wrong?

Regards,
J


If this is still a problem, ...
In this line

Set xlSourceRange2 = .Range(Range("g2"),
Range("g2").End(xlDown))

you have two unqualified references to the Excel Range object,
which I think might be the probable culprit - two small "dot's" (.)
might make the difference:

Set xlSourceRange2 = .Range(.Range("g2"),
..Range("g2").End(xlDown))

Or if the With block object isn't the correct one, insert the correct
reference.

--
Roy-Vidar

Nov 13 '05 #2
Hey Roy,

Thanks, that was the right solution!

Rgds

J

Nov 13 '05 #3

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

Similar topics

7
by: taylor.bryant | last post by:
I am running: Win XP SP2 Excel 2002, Access 2002 (Office XP SP3) Using Visual Basic (not VB.NET) At one point (prior to XP SP2?!? - I can't pin it down), this did not happen and I was easily...
17
by: Mansi | last post by:
I need to do some research on how to use excel automation from c#. Does anyone know of any good books related to this subject? Thanks. Mansi
12
by: D. Shane Fowlkes | last post by:
This most likely belongs in another forum but I thought I'd start here. I have a COM Object written in VB6. The DLL will access MS Excel and use it's Object Library to write a customized report...
3
by: Carlos Magalhaes | last post by:
Hey All, I am doing some excel automation using the excel COM. I can do most of the functions and its working well until I come across a formula. I can run a formula and insert the formula...
1
by: Robin Tucker | last post by:
Heres and interesting problem: I have a VB.NET program that creates reports via. Word Automation. This all works fine. What I want to do as part of this report generation process is to embed a...
6
by: a.theil | last post by:
Please help! I need a simple excel automation, just 2 write some files into excel. I do: Dim oXL As Excel.Application Dim oWB As Excel.Workbook Dim oSheet As Excel.Worksheet Dim oRng As...
2
by: jereviscious | last post by:
Hi all - Last resort time. I'm importing data from a spreadsheet that I receive from one of my vendor using interop.excel. The date field in excel is entered as 4/7/2006, but when I retrieve...
13
by: chuckie_9497 | last post by:
hello all you gurus. I am struggling with releasing com objects. I have isolated the problem to the code below. Objects are released and the process ends until I use "int k = sheet.Count;" Then...
6
by: Dan Fergus | last post by:
Let's try both ASp and Excel groups and see if I can get some help... My web site loads an Excel template, populates it and redirects the user to the new file. This works great on my dev box. ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.