Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-14
3
Medium Priority
?
3,178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 1000 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

609 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