Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 13th, 2005, 11:41 AM
info@lowerbill.co.uk
Guest
 
Posts: n/a
Default VBA Excel paste from access function fails every second time it runs!

I am pasting in a recordset from access to an excel sheet via VBA
successfully. Then my routine copies a range of cells and pastes them
into another range, this works fine every other time it runs. Every
other run of the code it fails at the line Selection.Copy with a
Runtime error 91:. I then run it again and it works fine. This is
really puzzling me a snippet of my code is below. Any ideas?
[color=blue][color=green][color=darkred]
>>>>>>>>>>>>>>>>>>>>>>[/color][/color][/color]


ExcelApp.Workbooks.Open ("C:\Test.xls")

Set ExcelSheet = ExcelApp.Worksheets("Sheet1")
ExcelSheet.Range("B13").CopyFromRecordset rs
ExcelSheet.Range("O13:X13").Select
Selection.Copy ' Code fails here every second time it runs with Runtime
error 91:
ExcelSheet.Range(Cells(14, 15), Cells(rec + 12, 24)).Select
ActiveSheet.Paste

ExcelApp.ActiveWorkbook.Save
ExcelApp.Quit

  #2  
Old November 13th, 2005, 11:41 AM
MacDermott
Guest
 
Posts: n/a
Default Re: VBA Excel paste from access function fails every second time it runs!

Try using
ExcelSheet.Selection.Copy

Also be sure to close your ActiveWorkbook before you do your Quit,
and set ExcelApp=Nothing

Once you've run this code, it's a good idea to check in your Task Manager on
the Processes tab to make sure you're not leaving a "zombie" Excel instance.

HTH

<info@lowerbill.co.uk> wrote in message
news:1118271293.775023.54950@g47g2000cwa.googlegro ups.com...[color=blue]
> I am pasting in a recordset from access to an excel sheet via VBA
> successfully. Then my routine copies a range of cells and pastes them
> into another range, this works fine every other time it runs. Every
> other run of the code it fails at the line Selection.Copy with a
> Runtime error 91:. I then run it again and it works fine. This is
> really puzzling me a snippet of my code is below. Any ideas?
>[color=green][color=darkred]
> >>>>>>>>>>>>>>>>>>>>>>[/color][/color]
>
>
> ExcelApp.Workbooks.Open ("C:\Test.xls")
>
> Set ExcelSheet = ExcelApp.Worksheets("Sheet1")
> ExcelSheet.Range("B13").CopyFromRecordset rs
> ExcelSheet.Range("O13:X13").Select
> Selection.Copy ' Code fails here every second time it runs with Runtime
> error 91:
> ExcelSheet.Range(Cells(14, 15), Cells(rec + 12, 24)).Select
> ActiveSheet.Paste
>
> ExcelApp.ActiveWorkbook.Save
> ExcelApp.Quit
>[/color]


  #3  
Old November 13th, 2005, 11:41 AM
info@lowerbill.co.uk
Guest
 
Posts: n/a
Default Re: VBA Excel paste from access function fails every second time it runs!

Thanks for the tips. Yes a "zombie" excel instance was being left.
Sorted by ensuring the ActiveWorkbook was properly closed. Also
hooking up to an already running instance when initiating. Solutions
to these problems found on this message board.

 

Bookmarks

Thread Tools

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles