Excel Add-in to call subroutine from ActiveWorkbook - vba

I created an Add-In that adds a sheet to a Workbook and imports multiple modules. I would then like for the Add-in to run a Subroutine that is now in Active Workbook. This is what I have so far, and I'm getting Run-time error '438': Object doesn't support this property or method.
What is the correct syntax (if it can even be done). Thanks.
' Class name is EventClassModule
Public WithEvents App As Application
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
If Wb.Name = "Just To Test.xls" Then
Wb.Sheets.Add Type:="C:\TestGLPage.xls"
fname = Dir("C:\Users\Me\Desktop\BAS\*.*", vbNormal)
While fname <> ""
If Right(fname, 3) = "frm" Or Right(fname, 3) = "bas" Or Right(fname, 3) = "cls" Then
ActiveWorkbook.VBProject.VBComponents.Import "C:\Users\Me\Desktop\BAS\" & fname
End If
fname = Dir() 'get the next file
Wend
Call Application.Workbooks("Just To Test.xls").starter
End If
End Sub

VBA is not a dynamic language. When you alter the code or the names of code objects at runtime VBA has to recompile those modules before the changes can be accessed. You may find breakpoints don't work properly after you've made such changes as well.
This recompilation happens automatically and immediately, but is not accessible from code that is already executing. You need to get Excel to re-enter the VBA code.
You may be able to get away with using Application.Run, but I would probably use Application.OnTime to be safer if you don't need starter to be a blocking call (which appears to be the case in your example code).
Just for clarity, when calling a subroutine using Application.Run or Application.OnTime you cannot qualify it using the module name. You can however qualify it using the workbook's name using bang syntax. Eg. Application.Run "Book1.xlsx!SubNameToBeCalled"
Example
I created a blank workbook
I created two modules
Imported
Public Sub RunMe()
MsgBox "Test!"
End Sub
Main
Public Sub Run()
ActiveWorkbook.VBProject.VBComponents.Import "C:\Temp\Imported.bas"
' Showing how to do it with Run
Application.Run ThisWorkbook.Name & "!RunMe"
' Showing the safer way with OnTime
Application.OnTime Now, ThisWorkbook.Name & "!RunMe"
End Sub
I then exported the "Imported" module to "C:\Temp\Imported.bas" and removed it from the project.

Related

VBA - Close "ThisWorkbook", and re-open after Delay

I have a workbook that contains a Macro that checks the workbook out from our SharePoint server, updates it, and then should check it back in. Afterwards, it should remain open for the user review and possibly execute other updates. The main problem I have is that the ".checkin" method closes out the Workbook, terminating the macro before it can re-open the workbook. I had found a proposed solution here: https://stackoverflow.com/a/22380886/8858822 , however I have encountered a problem with how Application.OnTime operates.
If I attempt to use the Application.Workbook.Open procedure as an argument for the Application.OnTime method, as suggested by the link above, the .Open procedure gets called immediately, without waiting the desired delay. In this scenario, the workbook has not been checked-in and closed yet, causing an error:
Sub TestSub()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim FPath As String
FPath = wb.FullName
Workbooks.CheckOut (FPath)
'''''''''''''''''''''''''
'execute workbook updates
'''''''''''''''''''''''''
Application.DisplayAlerts = False
On Error Resume Next
Application.OnTime Now + TimeValue("00:00:10"), Application.Workbooks.Open(FPath)
wb.CheckIn
Application.DisplayAlerts = True
End Sub
Is there a reason that Application.Workbooks.Open(FPath) executes immediately, instead of waiting the specified 10 seconds? If I can delay that execution by 10 seconds, the code would execute correctly.
I noticed that if instead of directly opening the file, I call one the macros in the file, it does wait the desired 10 seconds. This allows for the file to first be checked in and closed, then re-opens it and executes the specified macro.
Sub TestSub()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim FPath As String
FPath = wb.FullName
Workbooks.CheckOut (FPath)
'''''''''''''''''''''''''
'execute workbook updates
'''''''''''''''''''''''''
Application.DisplayAlerts = False
On Error Resume Next
Application.OnTime Now + TimeValue("00:00:10"), " ' file address & name '!OtherMacroName "
wb.CheckIn
Application.DisplayAlerts = True
End Sub
With this code, the file is checked in, closes, and then after 10 seconds reopens the file and runs the macro. Unfortunately, due to trust center settings I believe, I cannot directly call macros from workbooks saved on our SharePoint server, and this code generates an permissions error. If I relocate the file to my personal or our network drive, it executes without error. I have not been able to resolve the SharePoint permissions issue, even after following: https://stackoverflow.com/a/21175812/8858822 .
If I can understand why the Application.Workbooks.Open procedure operates immediately, while the macro call procedure waits the desired 10 seconds, when each is an argument used by Application.OnTime, I believe the code would work as desired. If there is a way to call the macro within the closed workbook after 10 seconds, I presume there must be some way of just opening the workbook after the 10 seconds? Thank you for your time and assistance.
OnTime takes the name of a subroutine as its argument. You need to create a new routine for OnTime to call (it doesn't actually have to do anything if all you need is the workbook reopened).
Is there a reason that Application.Workbooks.Open(FPath) executes
immediately, instead of waiting the specified 10 seconds? If I can
delay that execution by 10 seconds, the code would execute correctly.
Yes. The answer you linked is incorrect.
Application.OnTime Now + TimeValue("00:00:10"), SecondArgument
The SecondArgument is the name of a subroutine to call.
What you're doing when you put Application.Workbooks.Open(FPath) in SecondArgument is opening the workbook, then returning a reference to the workbook, in the same way you would go:
Set wb = Application.Workbooks.Open(FPath)
Put differently, when you do this...
Application.OnTime Now + TimeValue("00:00:10"), Application.Workbooks.Open(FPath)
... you're actually doing this:
Set wb = Application.Workbooks.Open(FPath)
Application.OnTime Now + TimeValue("00:00:10"), wb
So that answers your question, but to solve your problem...
From this link, there's some sort of dirty workaround where the key is apparently to call the Application.OnTime method a moment before you close the workbook (check in the workbook in your case):
Sub CloseMe()
Application.OnTime Now + TimeValue("00:00:02"), "OpenMe"
ThisWorkbook.Close ' check in
End Sub
Sub OpenMe()
' literally does nothing
End Sub
So Excel knows to run the OpenMe method in 2 seconds' time, and it is that act of attempting to run the OpenMe method that actually re-opens the workbook.
You can test this by having two workbooks:
E:\Documents\Book3.xlsm has the following code:
Sub TestMe()
MsgBox "test"
End Sub
E:\Documents\Book2.xlsm has the following code:
Sub OpenAnotherWorkbook()
Application.Run "'E:\Documents\Book3.xlsm'!TestMe"
End Sub
Making sure that E:\Documents\Book3.xlsm is currently closed, we run OpenAnotherWorkbook from E:\Documents\Book2.xlsm.
It will open E:\Documents\Book3.xlsm and run TestMe.

Excel VBA - How to use worksheet event in add-in module?

I am new to Excel Add-ins and I am not sure how to write mi programm.
I would like to put in an add-in a code so that, when the workbook that uses the add-in is opened, it creates a sheet named "mainSheet".
I can use the event handler in the Workbook, but is it possible to put the code in the module of the add-in and still be able to run it?
I found this on the "Automate Excel" web site. Hope this helps
The following code works opening a workbook. It automatically adds a new sheet and labels it with the name. It also checks to see that the sheet doesn’t already exist – to allow for the possibility of it being opened more than once a day.
This code makes use of the Workbook Open Event and must be placed in the workbook module under the “Open work Book” event. The function Sheet_Exists must be placed in a module and this checks whether or not the sheet exists:
Private Sub Workbook_Open()
Dim New_Sheet_Name As String
New_Sheet_Name = "mainSheet"
If Sheet_Exists(New_Sheet_Name) = False Then
With Workbook
Worksheets.Add().Name = New_Sheet_Name
End With
End If
End Sub
==
Function Sheet_Exists(WorkSheet_Name As String) As Boolean
Dim Work_sheet As Worksheet
Sheet_Exists = False
For Each Work_sheet In ThisWorkbook.Worksheets
If Work_sheet.Name = WorkSheet_Name Then
Sheet_Exists = True
End If
Next
End Function

Running excel macros only on one sheet

Hi Iam new to writing VBA code and need assistance. My VBA code and Macro works fine as long as I am on the active sheet.
My Problem:
My VBA code and macro, stops running automatically, when i change from the active sheet to another within the same workbook and
My VBA code and macro, stops running automatically, when i open a new excel workbook
Solution required:
Run the VBA code and macro only on desired worksheet and prevent it from running on other worksheets and workbooks.
Background:
I have an excel file, named "Net Weight" with two sheets. sheet 1 is named : "weight", sheet 2: is named "base data".
sheet 1 is used as a user input form.
In sheet 1- cell B1 : user will type in a product code , in cell E1: a look up formula will place the description of the product code using the data from sheet 2
I have setup a VBA code and macro that does the following:
As soon as a user inputs a product code into cell B1, in sheet 1, sheet 1 is saved as PDF file into a predefined folder location data from cell B1 and E1
A macro saves and overwrties the PDF file every 10 seconds.
This process is repeated every time a new product code is entered
There are no buttons on sheet 1, everything is done automatically.
Here is my current code:
Sheet 1: set as Worksheet
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$1" Then
Call Macro1
End If
End Sub
Module 1 macro : set as general
Sub Macro1()
Application.DisplayAlerts = False
If ThisWorkbook.Name = "Nett Weight.xlsm" And ActiveSheet.Name = "Weight" Then
Sheets("Weight").ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\Nett weight\" & Range("B1 ").Text & Range(" E1").Text
Application.OnTime Now + TimeValue("00:00:10"), "Macro1"
Else
Exit Sub
End If
End Sub
First, you need to create a public variable to hold your timer, otherwise you'll never be able to cancel it so it will continue trying to fire even when your workbook is closed. You should also create a public variable to store when the timer is running, so you can check before creating a new timer.
At the top of a code module put:
Public nextTime As Date
Then in your Workbook_BeforeClose() event method (within ThisWorkbook), disable the existing timer so it doesn't keep trying to fire after the workbook is closed.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next ' Continue with next line of code if we encounter an error
Application.OnTime Earliesttime:=nextTime, Procedure:="Macro1", Schedule:=False
On Error GoTo 0 ' Resume error-trapping
End Sub
In Macro1() you should explicitly and directly reference your workbook components - ThisWorkbook always refers to the workbook the code is running from, so you don't need your If statement. Then you set the nextTime and activate the timer using that variable if it is not already running.
Sub Macro1()
Dim sht As Worksheet ' Creates a variable to hold your Weight worksheet
Set sht = ThisWorkbook.Sheets("Weight") ' Sets the reference
Application.DisplayAlerts = False
sht.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Nett weight\" & sht.Range("B1").Text & sht.Range("E1").Text ' Remember to preceed Range with sht. to explicitly reference the range of your Weight worksheet
On Error Resume Next ' Continue with next line of code if we encounter an error
Application.OnTime Earliesttime:=nextTime, Procedure:="Macro1", Schedule:=False
On Error GoTo 0 ' Resume error-trapping
nextTime = Now + TimeSerial(0, 0, 10) ' Adds 10 seconds to Now
Application.OnTime Earliesttime:=nextTime, Procedure:="Macro1", Schedule:=True
timerIsRunning = True
Application.DisplayAlerts = True ' Remember to enable alerts at the end of code
End Sub
Your Worksheet_Change() event method can stay as is. Now if there is a change in B1 it will call Macro1(). Macro1() will save the Weight worksheet as a PDF regardless of whether the workbook or worksheet is active, and create a new timer to re-run Macro1() every 10 seconds after deactivating an existing timer. When you're finished with the workbook, you close it and the existing timer is also deactivated.
EDIT:
Fortunately (as it fixes a spreadsheet of my own) I have figured out why the solution I originally provided wasn't working. Placing the Public variables under ThisWorkbook meant they no longer held their values after code execution. The remedy was to place them in a module instead. Once that was sorted out, I also realised that when the timer fires to call Macro1() it will throw an error when trying to unschedule the existing timer (as none exists unless Macro1() was called ad hoc by the Worksheet_Change() event).
Long story short: Public variables have been moved to a code module, and the timerIsRunning flag has been removed entirely and errors when attempting to unschedule the timer are simply ignored in the event that no timer exists.

Running code from VBA to VBScript back to VBA

I'm trying to figure out a way to call a VBScript function using vba in Excel, and then pass a value back to excel-vba. See below
VBA within Excel
Sub RunTest()
Dim objString as String
'Begin Pseudocode
objString = Call VBScript Function Test()
'End Pseudocode
MsgBox objString `from VBS
End Sub
VBScript
Function Test
Test = "Hello World"
End Function
I know this may seem strange because I could just write the function in VBA, but we had an office patch pushed out and it completely killed the functionality of one of my macros for some reason. Strange thing is, I can run the exact same code within any other office program, just not excel. As a work around, I moved the function that crashes excel to word and I pull it using application.run, but I prefer to not have to do that, as opening a the word application to run my macro slows my process way down.
Any help is appreciated, thank You
Ok, I feel a litte dirty :)
This code has two key parts:
the vbs Uses GetObject and the full host workbook path to re-attach to the file containing the VBA that called the VBS
the VBS adds a value to a specific worksheet in the host VBA file to trigger the Worksheet_Change event to fire, running VBA with the string passed from the VBS.
Step 1: Regular Excel code module
Sub VBA_to_VBS_to_VBA()
Shell "wscript c:\temp\myvbs.vbs", vbNormalFocus
End Sub
Step 2: myvbs
Dim xlApp
Dim xlSht
On Error Resume Next
Set xlApp = GetObject("c:\temp\mybook.xlsx").Application
Set xlSht = xlApp.Sheets("vbs sheet")
On Error GoTo 0
If Not xlSht Is Nothing Then
xlSht.Range("A1").Value = "hello world"
Else
wscript.echo "sheet not found"
End If
Step 3: Sheet code for vbs sheet in your Excel File
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox [a1].Value, vbCritical, "VBS insertion"
End Sub
Try syntax like:
Sub Test()
Shell "cscript c:\TestFolder\sample.vbs", vbNormalFocus
End Sub

Refreshing Data Connections in Excel 2010 VBA

So I have all of my VBA code down and it seems to be right, but it just is not refreshing the workbooks. The only thing that I can think of is when I open up the sheets I have to enable editting, so would I need to add something to my code?
Edit: I am running this through a VBScript
Public wb As Workbook
Sub executeUpdate()
`some code
openBook path & testArray(i) & ext, True
saveBookAs path & testArray(i)
End Sub
Sub openBook(ByVal fileName As String, ByVal refresh As Boolean)
Set wb = Workbooks.Open(fileName, 0, False)
If refresh = True Then
wb.RefreshAll
End If
End Sub
Sub saveBookAs(ByVal fName As String)
wb.SaveAs fileName:=fName & "_posReport.xlsx"
End Sub
I found this code over here:
If Application.ProtectedViewWindows.Count > 0 Then
Application.ActiveProtectedViewWindow.Edit
End If
Have not tested it, but maybe it works for you.
--
Quote:
code will bypass the need for the user to select the "Enable Editing" button on the message bar below the ribbon. One additional note [...] the code will error if the workbook isn't in "ProtectedView". So to avoid this the following code will only enable editing if the workbook has "ProtectedView" enabled.