PC Review


Reply
Thread Tools Rate Thread

Copying across workbooks

 
 
=?Utf-8?B?anVkaXRo?=
Guest
Posts: n/a
 
      5th Sep 2007
I am working from Access and trying to copy and paste data between two
workbooks.



' Create a new instance of Excel
Set xlApp = New Excel.Application

' Make it visible
xlApp.Visible = True

' Open an associated file
Set xlWorkBook1 = xlApp.Workbooks.Open(strInputFileName)

' download Access Query to excel in same folder as associated file

strInputFilePath = xlWorkBook1.Path
DoCmd.TransferSpreadsheet acExport, 8, "tbl99ReconRepBase",
strInputFilePath & "\sheet15temp.xls"

' Open the downloaded file
Set xlWorkBook2 = xlApp.Workbooks.Open(strInputFilePath &
"\sheet15temp.xls")

' copy some data over

xlWorkBook1.Activate
etc ....

This seems to work fine except if Excel is already open. In which case it
gives an error on the last line
Run time error 1004
Method 'Sheets' of object Global failure

I have declared xlWorkbook1,2 as workbooks. Any suggestions please
 
Reply With Quote
 
 
 
 
papou
Guest
Posts: n/a
 
      5th Sep 2007
Hello Judith
You may consider using the GETOBJECT and CREATEOBJECT functions:

Dim XlApp As Object
On Error Resume Next
Set XlApp = GetObject(,"Excel.Application")
If Err <> 0 Then
Set XlApp = CreateObject("Excel.Application")
On Error Goto 0
End If

XlApp.Visible = True
'etc....

HTH
Cordially
Pascal



"judith" <(E-Mail Removed)> a écrit dans le message de news:
F3D4FC84-87DF-4C9B-8562-(E-Mail Removed)...
>I am working from Access and trying to copy and paste data between two
> workbooks.
>
>
>
> ' Create a new instance of Excel
> Set xlApp = New Excel.Application
>
> ' Make it visible
> xlApp.Visible = True
>
> ' Open an associated file
> Set xlWorkBook1 = xlApp.Workbooks.Open(strInputFileName)
>
> ' download Access Query to excel in same folder as associated file
>
> strInputFilePath = xlWorkBook1.Path
> DoCmd.TransferSpreadsheet acExport, 8, "tbl99ReconRepBase",
> strInputFilePath & "\sheet15temp.xls"
>
> ' Open the downloaded file
> Set xlWorkBook2 = xlApp.Workbooks.Open(strInputFilePath &
> "\sheet15temp.xls")
>
> ' copy some data over
>
> xlWorkBook1.Activate
> etc ....
>
> This seems to work fine except if Excel is already open. In which case it
> gives an error on the last line
> Run time error 1004
> Method 'Sheets' of object Global failure
>
> I have declared xlWorkbook1,2 as workbooks. Any suggestions please



 
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
Copying workbooks Andy Microsoft Excel Programming 5 5th Sep 2008 07:03 PM
Copying between workbooks Brad Microsoft Excel Misc 2 9th Jan 2008 08:22 PM
copying workbooks =?Utf-8?B?TmFkamk=?= Microsoft Excel Worksheet Functions 3 4th Oct 2006 08:43 PM
copying workbooks alistair01 Microsoft Excel Misc 1 2nd Sep 2004 01:32 PM
Copying from several workbooks into one =?Utf-8?B?QW50aG9ueSBHb2JlbA==?= Microsoft Excel Programming 0 6th Apr 2004 09:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:10 PM.