UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Trouble Automating Excel In Access    
 
   
Kamulegeya
post Jan 5 2012, 08:21 AM
Post #1

UtterAccess Ruler
Posts: 1,294
From: Kampala,Uganda The Pearl of Africa



Greetings UA

We have an Excel spread for which i am developing a db . It involves extensive data entry . I thought using a db is better because data entry will be shared .( we have a lan)

The problem i have faced is that one of the reports can not be done in access. I have to export data to excel. It combines three workbooks( linking using formula) . Though i was able to reproduce the three workbooks using crosstab queries, i failed to combine them into the fourth workbook. I am planning to make a sort of template workbook . Export Data , and use formulas to combine that data in the fourth workbook.

I searched and got a got a code to start with. How ever, this code creates hidden workbooks and keeps on incrementing the number...book1, book2, etc the hidden workbooks only show if i am closing the computer and o get the msg " Do want to save Book10?".

Here is the code i have . Looking for advice on how to make it work

CODE
Sub TestExport()
Dim db As Database
Dim rst As Recordset
Dim objApp As Excel.Application
Dim objBook As Excel.Workbook
Dim objSheet As Excel.Worksheet
Dim strTemplatePath As String
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter

Set db = CurrentDb()
Set qdf = db.QueryDefs("qryInstructions_Actual")
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm

' strTemplatePath = CurrentProject.Path & "\DRR Weekly Buy.xlt"
strTemplatePath = CurrentProject.Path & "\" & "wkbTemp.xlsx"

Set objApp = New Excel.Application

'excel spreadsheet file goes here
Set objBook = objApp.Workbooks.Add(strTemplatePath)

'Name of sheet you want to export to
Set objApp = objBook.Parent
Set objSheet = objBook.Worksheets("Try")
objBook.Windows(1).Visible = True


'Opens the recordset and sets the variable

' Set rst = db.OpenRecordset("Send Weekly Buy for Campaign Query", dbOpenSnapshot)
Set rst = qdf.OpenRecordset()

With objSheet

.Activate
.Select
'Clears the current contents in the workbook range
.Range("A2:AP2000").ClearContents
'rst Copies the recordset into the worksheet
.Range("A9").CopyFromRecordset rst
rst.MoveFirst

.Range("H3").Value = rst![RoadName]
.Range("h4").Value = rst![SectionNumber]
.Range("h5").Value = rst![fName]
.Range("B8").Value = rst![ItemDescription]
.Range("C8").Value = rst![Chainage]
.Range("D8").Value = rst![ItemUnitMeasurement]
.Range("e8").Value = rst![StandardRate]
.Range("F8").Value = rst![PlannedQuanties]
.Range("G8").Value = rst![PlannedAmount]
.Range("H8").Value = rst![ActualQuantities]
.Range("I8").Value = rst![ActualAmount]
.Range("J8").Value = rst![Remarks]

End With

rst.Close
objApp.Visible = True
Set rst = Nothing
Set db = Nothing
Set objSheet = Nothing
Set objBook = Nothing
Set objApp = Nothing

End Sub


The Data export works though with out column headings


Ronald

Go to the top of the page
 
+
Bob G
post Jan 5 2012, 08:44 AM
Post #2

UtterAccess VIP
Posts: 8,140
From: CT



i am curious as to why you stated that one of the reports cannot be done in Access. Is it currently done in Excel?
Go to the top of the page
 
+
Kamulegeya
post Jan 5 2012, 09:00 AM
Post #3

UtterAccess Ruler
Posts: 1,294
From: Kampala,Uganda The Pearl of Africa



QUOTE (Bob G @ Jan 5 2012, 04:44 PM) *
i am curious as to why you stated that one of the reports cannot be done in Access. Is it currently done in Excel?



Hello Bob

Currently we using Excel.

And the reporting format can not change.

With the test data i have entered in the DB, i can not make that report

Ronald
Go to the top of the page
 
+
TinaS
post Jan 9 2012, 04:39 PM
Post #4

UtterAccess Addict
Posts: 103



I have done some automation with Excel - if you are looking to name the worksheets - this code is what I have used and works well:


If objwb.ActiveSheet.Name <> strWrkSheet Then
objwb.ActiveSheet.Name = strWrkSheet & "-Pivot"

End If


I would put it in your code like this: (in my example the strWrkSheet was a variable name that I am checking for). This should help with the naming of the sheet- let me know if that helps and if you need to rename the column headings.

'excel spreadsheet file goes here
Set objBook = objApp.Workbooks.Add(strTemplatePath)
If objBook.ActiveSheet.Name <> strWrkSheet Then
objBook.ActiveSheet.Name = "enter sheet name here"
End If


Tina
Go to the top of the page
 
+
Kamulegeya
post Jan 9 2012, 04:54 PM
Post #5

UtterAccess Ruler
Posts: 1,294
From: Kampala,Uganda The Pearl of Africa



QUOTE (TinaS @ Jan 10 2012, 12:39 AM) *
I have done some automation with Excel - if you are looking to name the worksheets - this code is what I have used and works well:


If objwb.ActiveSheet.Name <> strWrkSheet Then
objwb.ActiveSheet.Name = strWrkSheet & "-Pivot"

End If


I would put it in your code like this: (in my example the strWrkSheet was a variable name that I am checking for). This should help with the naming of the sheet- let me know if that helps and if you need to rename the column headings.

'excel spreadsheet file goes here
Set objBook = objApp.Workbooks.Add(strTemplatePath)
If objBook.ActiveSheet.Name <> strWrkSheet Then
objBook.ActiveSheet.Name = "enter sheet name here"
End If


Tina



Thank you Tina

It past midnight here

Will give you Up dates tomorrow


Ronald

Go to the top of the page
 
+
TinaS
post Jan 9 2012, 04:59 PM
Post #6

UtterAccess Addict
Posts: 103



One thing - I just noticed my code renames the worksheet - not the workbook - but I would think the same principle would apply. GOOD LUCK!!
Go to the top of the page
 
+
Kamulegeya
post Jan 10 2012, 02:45 PM
Post #7

UtterAccess Ruler
Posts: 1,294
From: Kampala,Uganda The Pearl of Africa



QUOTE (TinaS @ Jan 10 2012, 12:59 AM) *
One thing - I just noticed my code renames the worksheet - not the workbook - but I would think the same principle would apply. GOOD LUCK!!



Hello Tina S

I failed to make it work

I decided to to do from Excel using ADO


Which is not as complicated as automation

Ronald
Go to the top of the page
 
+
TinaS
post Jan 20 2012, 03:04 PM
Post #8

UtterAccess Addict
Posts: 103



I just wanted to pop in to say i feel your pain. This was incredibly hard to do and figure out, and there wasn't much in terms of resources from automating excel. Probably the best, and most annoying ( (IMG:style_emoticons/default/smile.gif) ) piece of advice I was given, was set the macro start in excel, do what I wanted to see automated, and stop the macro recording, then go in and look at the code. it was NOT full proof by any means, as it simply didn't work to copy the code from excel to access and hope that it would work. Many times it needed some adjustment, but it would at least get me started.

I am sorry I couldn't have been more help. Sometimes actually seeing the issue can be productive, but it seems that with most of my experience, I find I can do a LOT of things, but, I only scrape the surface of these unique processes. In other words, I learned enough to get by and make it work, but am not an expert in automating with Excel. Jack of many (no, not all) trades, master of none... pretty much sums it up.

Good luck,

Tina
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 24th May 2013 - 01:44 PM