Auto_Open macro doesn't work when opening Excel from Access VBA

coxyg

New Member
Joined
Jul 16, 2014
Messages
5
Hi,

I have a simple access database that copies the query results to clipboard, access then opens up an Excel file and should run and auto_open macro. The problem is the auto_open macro doesn't work when access opens the excel file. But opening the excel file through windows explorer it works fine.

My Access VB is:

Private Sub Command4_Click()

'Copies to Clipboard Query Result'
DoCmd.OpenQuery "Project Programme", acViewNormal, acEdit
DoCmd.SelectObject acQuery, "Project Programme"
DoCmd.RunCommand acCmdSelectAllRecords
RunCommand acCmdCopy
DoCmd.Minimize
DoCmd.SetWarnings False
DoCmd.Close acQuery, "Project Programme", acSaveYes

'Open Up Excel File'

Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open "G:\Contracts database 2009\DG Project Programme\Project Programme.xlsm", True, False
Set xlApp = Nothing

'Closes Database'
Application.Quit

End Sub


My Excel Auto_Open is:

Sub Auto_Open()
'
' Auto_Open Macro
'

'
Range("A14").Select
ActiveSheet.Paste
End Sub



Any thoughts?
Regards
Coxyg
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

<CODE>I know if you run excel as a command line you also state the file to open and then something (used to be) "/M macroname" so something like shell "C:\office\excel.exe C:\mysheets\thiswk.xlsm /m Autoopen"

BUT in your case is easier to do
xlApp.RunAutoMacros xlAutoOpen</CODE>
</PRE>

<TBODY>
</TBODY>
 
Upvote 0
Why are you copying the query to the clipboard?

You could easily write code that would open Excel and copy the results of the query to the specified range.

For example.
Code:
Private Sub Command4_Click()
Dim rst As DAO.Recordset
Dim xlApp As Object
Dim xlWB As Object

    Set rst = CurrentDb.OpenRecordset("Project Programme")

    Set xlApp = CreateObject("Excel.Application")
    
    xlApp.Visible = True
    
    Set xlWB = xlApp.Workbooks.Open("G:\Contracts database 2009\DG Project Programme\Project Programme.xlsm", True, False)
    
    xlWB.ActiveSheet.Range("A14").CopyFromRecordset rst
    
    Set xlApp = Nothing

    rst.Close
    
    Set rst = Nothing
    
    'Closes Database'
    Application.Quit

End Sub
 
Upvote 0
Norie,

Thanks for your help.

Unfortunately I am getting the following error:

Run-time error '3061'. Too few parameters. Expected 1.

When I debug the highlighted line is:
Set rst = CurrentDb.OpenRecordset("Project Programme")

Any help would be much appreciated.
Regards
Coxyg
 
Upvote 0
Does the query 'Project Programme' take any parameters?
 
Upvote 0
Here's my query
8
 
Last edited:
Upvote 0
As far as I can see the query takes a parameter from the form 'Main' for the criteria of field 'Project Number'.

When you tried the code I posted was that form open?
 
Upvote 0
Correct. But I need the form open for my combo box selection and to press the button. (Sorry my access skills are quite poor)
 
Upvote 0
If the form was open then the code I posted should work, if the form is closed both the code you originally posted and the code I posted would cause the parameter error.
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,438
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top