Solved

What is the correct VBA PasteSpecial Format -  copy from Access to Excel?

Posted on 2011-02-14
3
2,911 Views
Last Modified: 2012-05-11
I have an database that was developed in Access 2003.

I export query results to a temp Excel file and copy the contents of the temp file to a pre-formatted Excel workbooks using VBA.

This code worked perfectly for several years using Access 2003.
...
xlObj.ActiveWorkbook.sheets("On Hold").select
xlObj.activesheet.range("A1").select
xlObj.activesheet.PasteSpecial Format:=2

I then upgraded to Office 2010 - however the PasteSpecial stopped working properly and did a straight paste and overwrote my formatting.
I then edited the PasteSpecial lines to:

xlObj.activesheet.PasteSpecial Paste:=xlPasteValues   (which i took from a recorded Excel macro)

However, the code now fails with a Run time 1004 error





0
Comment
Question by:radiantripack
3 Comments
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 250 total points
ID: 34887428
try defining
Const xlPasteValues = -4163


or add to your references the Microsoft excel 14 object library
0
 

Author Comment

by:radiantripack
ID: 34969018
Hi Capricorn1

I add the references and now have a line as follows:
xlObj.activesheet.PasteSpecial Paste:=xlPasteValues

I'm still getting the Run Time error 1004
0
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 250 total points
ID: 35496003
In a global sense, most developers do not use copy/paste to transfer data between Access and Excel.

This is typically done by using variables and specific Access/Excel automation.

Roughly like this from Access:

Dim YourVariable as SomeDatatype
YourVariable =Forms!YourForm!YourContol
'Code to instansiate Excel
xlObj.ActiveWorkbook.sheets("On Hold").select
xlObj.activesheet.range("A1").value=YourVariable

Then simply apply the formatting via Excel automation.

Also note that some formatting in Access 2010 is not compatible with Excel, so this may be part of your issue.

JeffCoachman
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question