PC Review


Reply
Thread Tools Rate Thread

Automate Excel from Access 2007

 
 
Ryan
Guest
Posts: n/a
 
      15th Oct 2008
The following protion of code worked perfect with Access 2003 and after I
updated 2003 to 2007. Now we have new computers with a fresh install of
Office 2007 and now I get Run-time error '-2147417851 (80010105)': Method
'Add' of object 'Workbooks' failed when the compiler hits the last line.

------------------------------------------------------------------------------------------
Dim objXL As Object
Dim objWB As Object
Dim strReportName As String

strReportTitle = "Report Name"

'Create a new excel document
Set objXL = CreateObject("Excel.Application")

'To create new workbook
Set objWB = objXL.Workbooks.Add 'This now creates the error. With or
without parenthesis () at the end.
objWB.Sheets(1).Name = strReportNam
------------------------------------------------------------------------------------------------

I added all the references that were added in 2003 except of course the
Office references are now 12 instead of 11.

If anyone knows what may be causing this I would greatly appreciate some
assistance.

--
Ryan
 
Reply With Quote
 
 
 
 
Tom van Stiphout
Guest
Posts: n/a
 
      15th Oct 2008
On Wed, 15 Oct 2008 05:55:00 -0700, Ryan
<(E-Mail Removed)> wrote:

Why are you using late binding?

This worked for me:
Dim objXL As Excel.Application 'Requires reference to Excel
Dim objWB As Excel.Workbook
Dim strReportName As String

'Note: next line had strReportTitle but that variable is
undefined.
strReportName = "Report Name"

'Create a new excel document
Set objXL = New Excel.Application
objXL.Visible = True 'So we can see what we're doing.

'To create new workbook
Set objWB = objXL.Workbooks.Add
objWB.Sheets(1).Name = strReportName

-Tom.
Microsoft Access MVP


>The following protion of code worked perfect with Access 2003 and after I
>updated 2003 to 2007. Now we have new computers with a fresh install of
>Office 2007 and now I get Run-time error '-2147417851 (80010105)': Method
>'Add' of object 'Workbooks' failed when the compiler hits the last line.
>
>------------------------------------------------------------------------------------------
> Dim objXL As Object
> Dim objWB As Object
> Dim strReportName As String
>
> strReportTitle = "Report Name"
>
> 'Create a new excel document
> Set objXL = CreateObject("Excel.Application")
>
> 'To create new workbook
> Set objWB = objXL.Workbooks.Add 'This now creates the error. With or
>without parenthesis () at the end.
> objWB.Sheets(1).Name = strReportName
>------------------------------------------------------------------------------------------------
>
>I added all the references that were added in 2003 except of course the
>Office references are now 12 instead of 11.
>
>If anyone knows what may be causing this I would greatly appreciate some
>assistance.

 
Reply With Quote
 
JP
Guest
Posts: n/a
 
      15th Oct 2008
Since you're using automation, you might have to fully qualify the
application object reference.

objXL.objWB.Sheets(1).Name = strReportName

Otherwise I'd go with what Tom said, if you set a reference to the
Excel object library, you should declare the application object as
Excel.Application, not Object.

--JP

On Oct 15, 8:55*am, Ryan <R...@discussions.microsoft.com> wrote:
> The following protion of code worked perfect with Access 2003 and after I
> updated 2003 to 2007. Now we have new computers with a fresh install of
> Office 2007 and now I get Run-time error '-2147417851 (80010105)': Method
> 'Add' of object 'Workbooks' failed when the compiler hits the last line.
>
> ---------------------------------------------------------------------------*---------------
> * * Dim objXL As Object
> * * Dim objWB As Object
> * * Dim strReportName As String
>
> * * strReportTitle = "Report Name"
>
> * * 'Create a new excel document
> * * Set objXL = CreateObject("Excel.Application")
>
> * * 'To create new workbook
> * * Set objWB = objXL.Workbooks.Add 'This now creates the error. With or
> without parenthesis () at the end.
> * * objWB.Sheets(1).Name = strReportName
> ---------------------------------------------------------------------------*---------------------
>

 
Reply With Quote
 
Ryan
Guest
Posts: n/a
 
      16th Oct 2008
Thanks but I still get the error.
--
Ryan


"JP" wrote:

> Since you're using automation, you might have to fully qualify the
> application object reference.
>
> objXL.objWB.Sheets(1).Name = strReportName
>
> Otherwise I'd go with what Tom said, if you set a reference to the
> Excel object library, you should declare the application object as
> Excel.Application, not Object.
>
> --JP
>
> On Oct 15, 8:55 am, Ryan <R...@discussions.microsoft.com> wrote:
> > The following protion of code worked perfect with Access 2003 and after I
> > updated 2003 to 2007. Now we have new computers with a fresh install of
> > Office 2007 and now I get Run-time error '-2147417851 (80010105)': Method
> > 'Add' of object 'Workbooks' failed when the compiler hits the last line.
> >
> > ---------------------------------------------------------------------------Â*---------------
> > Dim objXL As Object
> > Dim objWB As Object
> > Dim strReportName As String
> >
> > strReportTitle = "Report Name"
> >
> > 'Create a new excel document
> > Set objXL = CreateObject("Excel.Application")
> >
> > 'To create new workbook
> > Set objWB = objXL.Workbooks.Add 'This now creates the error. With or
> > without parenthesis () at the end.
> > objWB.Sheets(1).Name = strReportName
> > ---------------------------------------------------------------------------Â*---------------------
> >

>

 
Reply With Quote
 
Ryan
Guest
Posts: n/a
 
      16th Oct 2008
Thanks for the excellent feedback.

I'm using late binding because it's the first way I was able to create an
Excel workbook and work with it. I'm open to a better way if you have one.

The variable declaration isn't causing the problem. I made a mistake
trimming down the code to just show the error.

I tried your suggestions but I still get the same error message on the same
line of code. Here it is with you suggestions and without being trimmed down:

Dim objXL As Excel.Application
Dim objWB As Excel.Workbook
Dim sheet As Object
Dim SheetName As String
Dim intColCount As Integer
Dim rstCount As Long
Dim varRange As String
Dim strCol As String
Dim Counter As Integer
Dim strReportName As String
Dim strReportTitle As String
Dim strInteriorPattern As String
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset

strReportTitle = "Stock Record Account"
strReportName = strReportTitle & " Report"
strInteriorPattern = "xlSolid"

'Create a new excel document
Set objXL = New Excel.Application

objXL.Visible = True

'To create new workbook
Set objWB = objXL.Workbooks.Add
objWB.Sheets(1).Name = strReportName

All code that follows is formating and populating the worksheet with data....

Thanks for your help,

Ryan
--
Ryan


"Tom van Stiphout" wrote:

> On Wed, 15 Oct 2008 05:55:00 -0700, Ryan
> <(E-Mail Removed)> wrote:
>
> Why are you using late binding?
>
> This worked for me:
> Dim objXL As Excel.Application 'Requires reference to Excel
> Dim objWB As Excel.Workbook
> Dim strReportName As String
>
> 'Note: next line had strReportTitle but that variable is
> undefined.
> strReportName = "Report Name"
>
> 'Create a new excel document
> Set objXL = New Excel.Application
> objXL.Visible = True 'So we can see what we're doing.
>
> 'To create new workbook
> Set objWB = objXL.Workbooks.Add
> objWB.Sheets(1).Name = strReportName
>
> -Tom.
> Microsoft Access MVP
>
>
> >The following protion of code worked perfect with Access 2003 and after I
> >updated 2003 to 2007. Now we have new computers with a fresh install of
> >Office 2007 and now I get Run-time error '-2147417851 (80010105)': Method
> >'Add' of object 'Workbooks' failed when the compiler hits the last line.
> >
> >------------------------------------------------------------------------------------------
> > Dim objXL As Object
> > Dim objWB As Object
> > Dim strReportName As String
> >
> > strReportTitle = "Report Name"
> >
> > 'Create a new excel document
> > Set objXL = CreateObject("Excel.Application")
> >
> > 'To create new workbook
> > Set objWB = objXL.Workbooks.Add 'This now creates the error. With or
> >without parenthesis () at the end.
> > objWB.Sheets(1).Name = strReportName
> >------------------------------------------------------------------------------------------------
> >
> >I added all the references that were added in 2003 except of course the
> >Office references are now 12 instead of 11.
> >
> >If anyone knows what may be causing this I would greatly appreciate some
> >assistance.

>

 
Reply With Quote
 
New Member
Join Date: Nov 2008
Posts: 1
 
      19th Nov 2008
try rearranging your code in the following order:

Set objXL = New Excel.Application
'To create new workbook
objXL.Workbooks.Add
Set objWB = activeworkbook

objXL.Visible = True

objWB.Sheets(1).Name = strReportName

and the unreasons why:

#1: when using automation, Excel isn't happy doing anything until you add a new workbook. why? i don't know

#2: sometimes set x = workbooks.add fails, but .add and set x = activeworkbook works. why? don't know
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
automate 2dline with marker chart generation - Excel 2007 kdilly Microsoft Excel Misc 2 4th Feb 2010 10:32 PM
How do I automate the conversion of Access 2007 to 2003 Tracy Microsoft Access VBA Modules 2 13th May 2009 04:06 AM
How do I automate the conversion of Access 2007 to 2003 Tracy Microsoft Access VBA Modules 0 11th May 2009 05:09 PM
Automate saving filtered report as PDF in Access 2007 Amp Microsoft Access VBA Modules 5 19th Nov 2008 09:00 PM
Automate Access from Excel =?Utf-8?B?RXJuc3QgR3Vja2Vs?= Microsoft Excel Programming 1 16th Apr 2005 09:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:57 AM.