Trouble running Excel Add In macro from MS Access 2010 procedure - vba

I'm using VBA code in MS Access to export a query as an Excel workbook. I have a separate procedure that should open the workbook from MS Access and run a macro to format the workbook. The macro is stored in a separate Excel Add-In (.xlam) workbook.
My problem: I'm getting a 1004 can't run macro error, because "the macro can't be found or macros have been disabled." I tried enabling all macros in the Excel Trust Center options and I opened the Add-In workbook separately and re-ran the procedure. I'm getting the same error.
I've searched online for an answer, but I haven't found anyone who has successfully ran a macro stored in a separate Add-In workbook from a MS Access procedure. Anyone care to try and help?
Here's my MS Access procedure that's giving me problems:
Private Sub RunExcelTrackerMacro(strFileName As String)
Dim xl As Object
' Create excel app.
Set xl = CreateObject("Excel.Application")
' Open workbook to be formatted via a macro.
xl.Workbooks.Open (strFileName)
xl.Visible = False
' Run the macro, stored in a separate add-in (.xlam) workbook.
' This procedure fails to run the public Call_FormatTracker macro, which
' is stored in an Add-In in a module called "MacroCalls."
xl.Run "MacroCalls.Call_FormatTracker"
' Save and close the workbook.
xl.ActiveWorkbook.Close (True)
' Close excel app.
xl.Quit
Set xl = Nothing
End Sub

What about changing the xl.Run line to something like xl.Run "Sales.xlam!Orders"
Not sure what your xlam is called, but just change to suit.

I had a similar problem running a macro in an add on. Although the addin was available when I opened Excel directly it was not when Excel was called from Access.
After you create the Excel object add the line below.
Call ReloadXLAddins(xl)
Function ReloadXLAddins(oExcel) ' As Boolean
' Dim CurrAddin As Excel.AddIn
For Each CurrAddin In oExcel.AddIns
If CurrAddin.Installed Then
CurrAddin.Installed = False
CurrAddin.Installed = True
End If
Next CurrAddin
End Function

Related

How to suppress "SQL Server Login" window on Excel?

I am using Microsoft Task Scheduler to run Excel models everyday. The models retrieve data from the database and do stuff. The connection strings contain all information(username, password etc.) needed to connect to the database. However, I get this prompt sometimes:
SQL Login Prompt
When I click "OK", everything goes fine, so I need the Excel to just click OK because the username and password are already there. I want to suppress this window by clicking OK. Otherwise the models stop running and wait for me to click OK manually.
I checked for the solutions on the web but did not encounter a working one. I tried to change the connection string in various ways but it did not work.
Does anyone have any idea on how to solve this issue?
Thanks
Edit: Here is the code I am running on VBScript for Task Scheduler.
'Input Excel File's Full Path
ExcelFilePath = "MyExcelFile"
'Input Module/Macro name within the Excel File
MacroPath = "Macro1"
'Create an instance of Excel
Set ExcelApp = CreateObject("Excel.Application")
'Do you want this Excel instance to be visible?
ExcelApp.Visible = True 'or "False"
'Prevent any App Launch Alerts (ie Update External Links)
ExcelApp.DisplayAlerts = False
'Open Excel File
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)
'Execute Macro Code
ExcelApp.Run MacroPath
WScript.Sleep 10000
'Input Module/Macro name within the Excel File
MacroPath = "Macro2"
'Execute Macro Code
ExcelApp.Run MacroPath
WScript.Sleep 10000
'Save Excel File (if applicable)
wb.Save
'Reset Display Alerts Before Closing
ExcelApp.DisplayAlerts = True
'Close Excel File
wb.Close
'End instance of Excel
ExcelApp.Quit
The excel VBA code for Macro1 is just
Sub UpdateModel()
ActiveWorkbook.RefreshAll
End Sub
command for pivot tables which are getting data from the database.

VBA - Workbook Macro Pass Error

I have created an Excel file containing about 5 pages of vba code. All was good until,I choosen to create a macro for the workbook for its protection. Something like.
Dim pass as string/
if pass <> "PAR> or pass<>"par" then activeworkbook.close.
I think that the OR messes the things up. Now I can't access my workbook. Whatever pass I introduced it closes the workbook. I may mention here that pass is an inputbox.
Can someone help me in resolving this problem. I've lost my entire work.
LE: i've made it! Thank you for your support! :)
Try this way:
Sub main()
Dim secAutomation As MsoAutomationSecurity
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Remember the macro security settings and disable macros in target workbook. '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
secAutomation = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Workbooks.Open "<path to your workbook>"
''''''''''''''''''''''''''''''''''''''''''''''
'Reinstate previous macro security settings. '
''''''''''''''''''''''''''''''''''''''''''''''
Application.AutomationSecurity = secAutomation
End Sub
or manually change your Macro settings to 'Disable all macros with notification'
open any other workbook and get to the VBA IDE (ALT+F11)
type in Immediate Window
application.EnableEvents=False
then you can open your "impossible" workbook
as soon as you need things back regular type in the Immediate Window
application.EnableEvents=True
Try to open another workbook, and use statement application.enableEvents = false then before close this workbook, open workbook you wish

Run Macro from Desktop Shortcut

I wrote a macro that does some calculations based on a particular type of excel sheet. Im trying to distribute this to my coworkers but the addition of a macro to a workbook and then running the macro is something foreign to them. I'd like to have a "shortcut" or some VBS program to open a specific workbook (specified by the user), run the macro, and display the results.
Your help is appreciated!
--Edit--
I wrote a macro in VBA. I exported the file to my desktop. Its simply called "Macro1". We have a standard form of excel sheet our company uses. Its literally the same sheet with different numbers. The macro I designed works on these kinds of sheets and does calculations. My coworkers aren't good with macros, so I want some sort of "code" that will prompt one of my coworkers for an excel file, then execute the macro on the file. Hopefully this clarifies any questions.
You need to make it a excel add-in.
Then in the add-in make it run on workbook open with Sub App_SheetActivate(ByVal Sh As Object) in thisworkbook.
In the macro you can then have it only activate on certain workbook name or workbook type by:
If range("A1").value = "something" ' something that makes the workbook type special.
' Maybe you need B1 value and so on too.
Do you need a way to self-install the add in just let me know and I have a code for that too.
Self install:
' if add-in is not installed and the workbook is a add-in (workbookcount =0)
' Also take note that this code will only run if the add-in is not installed
If Dir(Application.UserLibraryPath & "YourWorkbookName.xlam") = "" And Workbooks.Count = 0 Then
'optional ask user if he wants to install or not. Code not included.
' copy file from current position to add-ins folder.
Result = apiCopyFile(ThisWorkbook.FullName, Application.UserLibraryPath & "YourWorkbookName.xlam", False)
' activate the add-in
AddIns("YourAdd-inName").Installed = True
msgbox("add-in installed")
' Close Excel since add-ins does not work without restart of Excel
On Error Resume Next
Application.Interactive = False
AppActivate "Microsoft Excel"
Application.Quit
Exit Sub
End If
Note that the file must be saved as a add-in. (xlam) this means there is no sheets, the workbook is VBA code only.
Normally, that does not mean the code needs to be written in a special way.
Usually Range("XX").value works, but some commands may need to point towards the correct workbook. (you have two workbooks open with add-ins, the add-in with the code and the workbook with the sheets and numbers)
Hope this helps

Turn On Automatic Calculations

I am using Excel 2013 and we are coming across random Excel files set to manual calculations and they do not seem to go away after resetting to automatic.
Those files seem to stay as automatic, but on a random day a different Excel file or the same Excel reverts back to manual. I want to auto execute a macro when loading any Excel file or just the program to set Excel to automatic calculations.
I tried the following macro:
Private Sub Auto_Open()
Application.Calculation = xlCalculationAutomatic
I received the following error message upon loading Excel:
"Run-time error '1004': Method 'Calculation' of object'_Application'failed
Troubleshoot:
An Auto_Open macro runs before any other workbooks open. Therefore, if you record actions that you want Excel to perform on the default Book1 workbook or on a workbook that is loaded from the XLStart folder, the Auto_Open macro will fail when you restart Excel, because the macro runs before the default and startup workbooks open.
If you encounter these limitations, instead of recording an Auto_Open macro, you must create a VBA procedure for the Open event as described in the next section of this article.
Question: is there a way to create a macro to reset any Excel file back to Automatic? I stored the macro in my personal workbook as I am hoping that the macro will execute on any Excel file I load.
I tried what you wrote and it works.
Just in case, here is my code :)
Private Sub auto_open()
Application.Calculation = xlAutomatic
End Sub

Run Excel macro automatically upon excel file opening

I have a personal workbook macro written in excel vba... I want that macro run automatically as soon as a particular named excel file is opened by any user. And that it should not run if it is already been run on that excel file earlier..
Open excel vba editor: Shift+F11. In MsExcelObjects doubleclick on "This Workbook".On the right side write this code and adapt it to your needs:
Private Sub Workbook_Open()
If SheetX.cells(x,y) = 0 then 'adapt SheetX and cell(x,y) to your needs.
'execute your code here
SheetX.cells(x,y)=1 'placeing an indicator if program runs, so it wont run another time it starts.
end if
end sub