Hello there
Seems like the original thread I opened was deleted when the database broke down a couple of days ago.
So, my problem is that I have 2 databases, and 1 excel file. The databases are identical in their function and structure - of course, macro names, references, variables have a different name, but the function they do is the same.
I have to copy information from both database to the common excel file. For this, I use the following macro which:
Runs and export results of a query to excel, opens up the report, insert a module and a macro which copies the information with a VLOOKUP.
My problem is that anytime I run the macro from the second database, I got runtime error 1004 message. It does not matter, which database do I pick first, the second will always run to this error.
It seems like, for some reason I'm unable to insert a second Module to the common excel file.
This is the code I'm using
Running it twice it always fails at this line:
any ideas what should I update ?
Seems like the original thread I opened was deleted when the database broke down a couple of days ago.
So, my problem is that I have 2 databases, and 1 excel file. The databases are identical in their function and structure - of course, macro names, references, variables have a different name, but the function they do is the same.
I have to copy information from both database to the common excel file. For this, I use the following macro which:
Runs and export results of a query to excel, opens up the report, insert a module and a macro which copies the information with a VLOOKUP.
My problem is that anytime I run the macro from the second database, I got runtime error 1004 message. It does not matter, which database do I pick first, the second will always run to this error.
It seems like, for some reason I'm unable to insert a second Module to the common excel file.
This is the code I'm using
Code:
Function Comment1()
' Start Excel
Dim xlapp As Object 'Excel.Application
Set xlapp = CreateObject("Excel.Application")
' Make it visible...
xlapp.Visible = True
' Add a new workbook
Dim destination2 As String
destination2 = "C:\****\test.xls"
Dim xlbook As Object 'Excel.Workbook
Set xlbook = xlapp.Workbooks.Open(destination2)
' Add a module
Dim xlmodule As Object 'VBComponent
Set xlmodule = xlbook.VBProject.VBComponents.Add(1) 'vbext_ct_StdModule
'define code and add a macro to the module...
Dim Code3 As String
Code3 = "" (text of inserted macro comes here)
xlmodule.CodeModule.AddFromString Code3
' Run the new macro!
xlapp.Run "embeddedmacro" (name is defined in Code3 string)
' Remember to release module
Set xlmodule = Nothing
' Clean up
xlbook.Saved = True
'xlapp.Quit
End Function
Running it twice it always fails at this line:
Code:
Set xlmodule = xlbook.VBProject.VBComponents.Add(1)
any ideas what should I update ?