I'm wondering if there's a simple way for a Word macro to determine which button was just pressed? I have a document template with several button which should all fire a macro.
The thing is, I want to create ONE macro which is called in each button. I don't want tons of macros for each button.
Now, this macro, when the button is pressed, it inserts a picture and the size of this picture is selected based on the buttons size. Meaning, this ends up as a picture placeholder. But, I want to write the macro dynamically so that the same code will work on each button without doing more than just calling the macro.
The complete macro is already done, I just need to know this one last thing, if anyone has any info on how to accomplish this? :) Thanx in advance!
UPDATE: This is the code at the moment
Private Sub ImageButton1_Click()
PicturePlaceholder ImageButton1
End Sub
Private Sub ImageButton2_Click()
PicturePlaceholder ImageButton2
End Sub
Public Sub PicturePlaceholder(ByVal oButton As CommandButton)
Dim oShape As Word.Shape
Dim Dlg As Office.FileDialog
Dim strFilePath As String
Dim oDoc As Document
Dim rgePlace As Range
Dim buttonHeight As String
Dim buttonWidth As String
Set Dlg = Application.FileDialog(msoFileDialogFilePicker)
Set oDoc = ActiveDocument
Set rgePlace = Selection.Range.Fields(1) _
.Result.Paragraphs(1).Range
Response = MsgBox("Do you want to delete the button/Picture?", vbYesNoCancel, "Do you want an image here?")
If Response = vbYes Then rgePlace.Fields(1).Delete
If Response = vbCancel Then Exit Sub
If Response = vbNo Then
With Dlg
.AllowMultiSelect = False
If .Show() <> 0 Then
strFilePath = .SelectedItems(1)
End If
End With
If strFilePath = "" Then Exit Sub
Set oShape = oDoc.Shapes.AddPicture(FileName:=strFilePath, _
LinkToFile:=False, SaveWithDocument:=True, _
Anchor:=rgePlace)
With oShape
.Height = oButton.Height
.Width = oButton.Width
End With
rgePlace.Fields(1).Delete
End If
End Sub
OK, so they're CommandButtons in the document.
In that case, there's nothing you can do - you need to have handlers called Button1_Click, Button2_Click, etc. (or whatever the button names are).
However, you can do something like this:
Private Sub Button1_Click(...)
DoStuff Button1
End Sub
Private Sub Button2_Click(...)
DoStuff Button2
End Sub
Private Sub DoStuff(ByVal oButton As CommandButton)
' All your shared code goes here
MsgBox oButton.Caption
End Sub
See also this tech note for how to create your buttons in code.
EDIT: updated to pass CommandButton reference so that the shared function can access the button properties.
EDIT 2: updated to show complete code using InlineShapes. Note that this no longer passes in the Button object, since the width/height of the button can be obtained directly from the field.
Private Sub CommandButton1_Click()
PicturePlaceholder
End Sub
Private Sub CommandButton2_Click()
PicturePlaceholder
End Sub
Public Sub PicturePlaceholder()
' Get the selected field, which must be a button field
Dim oField As Field
Set oField = Selection.Fields(1)
Debug.Assert oField.Type = wdFieldOCX
' Ask the user what he wants to do
Select Case MsgBox("Do you want to delete the button/Picture?", vbYesNoCancel, "Do you want an image here?")
Case vbCancel
Exit Sub
Case vbYes
oField.Delete
Exit Sub
End Select
' Get the filename of the picture to be inserted
Dim strFilePath As String
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
If .Show() <> 0 Then
strFilePath = .SelectedItems(1)
End If
End With
If strFilePath = "" Then
Exit Sub
End If
' Figure out where to insert the picture, and what size to make it
Dim oRange As Range
Set oRange = oField.Result
Dim sglWidth As Single
sglWidth = oField.InlineShape.Width ' oButton.Width
Dim sglHeight As Single
sglHeight = oField.InlineShape.Height ' oButton.Height
' Delete the button field
oField.Delete
' Insert and resize the picture
Dim oInlineShape As Word.InlineShape
Set oInlineShape = oRange.InlineShapes.AddPicture(FileName:=strFilePath, LinkToFile:=False, SaveWithDocument:=True, Range:=oRange)
With oInlineShape
.Width = sglWidth
.Height = sglHeight
End With
End Sub
EDIT 3: Updated as requested to use Shapes rather than InlineShapes. (Both the CommandButton and the inserted Picture are now Shapes).
Private Sub CommandButton1_Click()
PicturePlaceholder
End Sub
Private Sub CommandButton2_Click()
PicturePlaceholder
End Sub
Public Sub PicturePlaceholder()
' Get the selected shape, which must be a button shape
Debug.Assert Selection.Type = wdSelectionShape
Dim oButtonShape As Shape
Set oButtonShape = Selection.ShapeRange(1)
' Ask the user what he wants to do
Select Case MsgBox("Do you want to delete the button/Picture?", vbYesNoCancel, "Do you want an image here?")
Case vbCancel
Exit Sub
Case vbYes
oButtonShape.Delete
Exit Sub
End Select
' Get the filename of the picture to be inserted
Dim strFilePath As String
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
If .Show() <> 0 Then
strFilePath = .SelectedItems(1)
End If
End With
If strFilePath = "" Then
Exit Sub
End If
' Insert the picture at the same size/position
Dim oPictureShape As Shape
Set oPictureShape = ActiveDocument.Shapes.AddPicture _
( _
FileName:=strFilePath, _
LinkToFile:=False, _
SaveWithDocument:=True, _
Left:=oButtonShape.Left, _
Top:=oButtonShape.Top, _
Width:=oButtonShape.Width, _
Height:=oButtonShape.Height, _
Anchor:=oButtonShape.Anchor _
)
' Copy across the button shape formatting
oButtonShape.PickUp
oPictureShape.Apply
' Copy across other layout details
oPictureShape.LayoutInCell = oButtonShape.LayoutInCell
oPictureShape.LockAnchor = oButtonShape.LockAnchor
oPictureShape.RelativeHorizontalPosition = oButtonShape.RelativeHorizontalPosition
oPictureShape.RelativeVerticalPosition = oButtonShape.RelativeVerticalPosition
oPictureShape.WrapFormat.Type = oButtonShape.WrapFormat.Type
oPictureShape.WrapFormat.Side = oButtonShape.WrapFormat.Side
oPictureShape.WrapFormat.DistanceTop = oButtonShape.WrapFormat.DistanceTop
oPictureShape.WrapFormat.DistanceLeft = oButtonShape.WrapFormat.DistanceLeft
oPictureShape.WrapFormat.DistanceBottom = oButtonShape.WrapFormat.DistanceBottom
oPictureShape.WrapFormat.DistanceRight = oButtonShape.WrapFormat.DistanceRight
oPictureShape.WrapFormat.AllowOverlap = oButtonShape.WrapFormat.AllowOverlap
' Delete the button shape
oButtonShape.Delete
End Sub
I assume you mean that the button is a Command Bar button (aka toolbar button).
If so, you can use Application.CommandBars.ActionControl to get a reference to the button that was clicked. From there you can examine the caption, tag, or whatever.
You could put your base macro into a separate sub and then just call the macro from each button's click event, passing as a parameter the desired size. Then the only code you would have in the buttons is the call to the base sub.
You can have a button in Vba to pass arguments when it calls the macro which takes arguments.
For eg: if you have a function called as function test(x as string)
then the button which calls the macro will have a syntax as onclick("sheetx!test", "whatever"). So that way you can either have a generic macro to be called. Hope this helps you.
Placing the following in the various click/got_focus/change events of ActiveX buttons and textboxes works for me:
MsgBox ThisDocument.ActiveWindow.Selection.Fields.Item(1).OLEFormat.Object.Name
Related
I was on MS Word 2007 where the macros worked fine.
After upgrading to MS Word 2016 the method Document_ContentControlOnExit does not fire.
Since I write very few MS Word macros I don’t keep up on fundamental change to the architecture. Has something changed in MS Word 2016 that would cause the code to stop working?
The set of methods will detect a Content Control exit. Then update all Content Controls with the same tag name with the new value.
I have put a breakpoint in Document_ContentControlOnExit. Then made some changes to a Content Control. Nothing.
Sub SetUp()
Set eventHandler.doc = ActiveDocument
End Sub
Sub Document_Close()
Call UpdateAllFields
End Sub
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
ccTag = ContentControl.Tag
ccValue = ContentControl.Range.Text
Call updateValueInAllInstances(ccTag, ccValue)
End Sub
Sub updateValueInAllInstances(ccTag, ccValue)
Dim doc As Document
Dim ccs As ContentControls
Dim cc As ContentControl
Set doc = ActiveDocument
For Each cc In doc.SelectContentControlsByTag(ccTag)
' If the author of the document did not lock the contents, then perform a auto update.
If cc.LockContents Then
Respose = MsgBox("The author of this cookbook has deliberately designed this specific content control to not be editable.", 0, "Content Control is not editable", "", 1000)
Else
cc.Range.Text = ccValue
End If
Next cc
End Sub
Sub UpdateAllFields()
Dim objStory As Range
Dim objTOC As TableOfContents
Dim objTOA As TableOfAuthorities
Dim objTOF As TableOfFigures
Dim objIndex As Index
Application.ScreenUpdating = False
Application.DisplayAlerts = wdAlertsNone
For Each objStory In ActiveDocument.StoryRanges
UpdateFieldsInStory objStory
While Not (objStory.NextStoryRange Is Nothing)
Set objStory = objStory.NextStoryRange
UpdateFieldsInStory objStory
Wend
Next
For Each objTOC In ActiveDocument.TablesOfContents
objTOC.Update
Next
For Each objTOA In ActiveDocument.TablesOfAuthorities
objTOA.Update
Next
For Each objTOF In ActiveDocument.TablesOfFigures
objTOF.Update
Next
For Each objIndex In ActiveDocument.Indexes
objIndex.Update
Next
Application.DisplayAlerts = wdAlertsAll
Application.ScreenUpdating = True
End Sub
Private Sub UpdateFieldsInStory(iobjStory As Range)
Dim objShape As Shape
With iobjStory
.Fields.Update
Select Case .StoryType
Case wdMainTextStory, wdPrimaryHeaderStory, _
wdPrimaryFooterStory, wdEvenPagesHeaderStory, _
wdEvenPagesFooterStory, wdFirstPageHeaderStory, _
wdFirstPageFooterStory
For Each objShape In .ShapeRange
With objShape.TextFrame
If .HasText Then .TextRange.Fields.Update
End With
Next
End Select
End With
End Sub
I was hoping the code was not so out of date that it would keep working.
This mistake was what Cindy Meister pointed out. When I copied the macro code from the old to the new template I failed to notice the location in the Visual Basic Editor. Once the code was in the correct place it worked.
I have an ActiveX Command Button that when pressed opens a userform to allow data entry into the word document. This button needs to remain visible when working on the document but not visible when printing.
How can I hide/make invisible only when printing?
Unlike in Excel VBA where the properties include an option to 'PrintObject', word VBA does not have this functionality. The best I have been able to do is delete the button after being clicked but this is not really what I want.
'Needs to hide button only on printing, not delete it
UserForm2.Show
CommandButton1.Select
Selection.Delete
I assume you are having ActiveX Command Button in word and using user form entered data gets feed in corresponding fields and you are closing user form and then trying to print document and printed file should not have ActiveX Command Button in it
Paste the following code into CommandButton_Click event
Private Sub CommandButton1_Click()
With ActiveDocument
.Shapes(1).Visible = msoFalse
.PrintOut Background:=False
.Shapes(1).Visible = msoTrue
End With
End Sub
#ille P. - You should post a new question, perhaps with a link to this one.
Try the Shapes collection as well as inlineshape collection.
The following is suggested by Ibby on the Microsoft Word MVP VBA FAQ pages:
Private Sub CommandButton1_Click()
With ActiveDocument
.Shapes(1).Visible = msoFalse
.PrintOut Background:=False
.Shapes(1).Visible = msoTrue
End With
End Sub
So translating to the collection:
Dim oShape as Shape
For Each oShape in ActiveDocument.Shapes
oShape.Visible = False
Next oShape
That would, though hide all shapes, not just your buttons.
You could add bookmarks to your buttons and use them as a filtering Range.
After investigating what happens with the CommandButtons, I detected that in the document they are included (if it has not been edited to be a Shape) in the InLineShape collection by being encapsulated in an InLineShape object.
It would be great if we could directly typecast from CommandButton to InShapeLine but I think Microsoft doesn't allow it, too bad.
The idea is to create two class modules:
One will be an event manager to capture the 'before printing' event and another.
The other will be an encapsulation of an InLineShape object that will have the methods to make the object visible, taking advantage of the Brightness property of the PictureFormat object.
CLASS clsButton
Option Explicit
Private M_ishpButton As InlineShape
Public Property Get button() As InlineShape
Set button = M_ishpButton
End Property
Public Property Set button(oObj As InlineShape)
Set M_ishpButton = oObj
End Property
Public Property Get Visible() As Boolean
Visible = Not bIsHidden
End Property
Public Property Let Visible(bValue As Boolean)
Dim oPictureFormat As PictureFormat
Set oPictureFormat = M_ishpButton.PictureFormat
If bValue Then
Call show
Else
Call hide
End If
End Property
Private Function bIsHidden() As Boolean
Dim oPictureFormat As PictureFormat
Set oPictureFormat = M_ishpButton.PictureFormat
If oPictureFormat.Brightness = 1 Then bIsHidden = True: Exit Function
bIsHidden = False
End Function
Private Sub hide()
Dim oPictureFormat As PictureFormat
Set oPictureFormat = M_ishpButton.PictureFormat
oPictureFormat.Brightness = 1
End Sub
Private Sub show()
Dim oPictureFormat As PictureFormat
Set oPictureFormat = M_ishpButton.PictureFormat
With oPictureFormat
.Brightness = 0.5
.Contrast = 0.5
End With
End Sub
CLASS clsEvents
Option Explicit
Public WithEvents appWord As Word.Application
Public WithEvents docWord As Word.Document
Private m_button As New clsButton
Private Sub appWord_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
'If process is not cancel and button is not visible
With m_button
If Cancel = False And .Visible = True Then
.Visible = False
End If
End With
End Sub
Private Sub appWord_WindowDeactivate(ByVal Doc As Document, ByVal Wn As Window)
'If button is not visible then set to true
With m_button
If .Visible = False Then
.Visible = True
End If
End With
End Sub
Public Property Set button(oObj As clsButton)
Set m_button = oObj
End Property
CLASS ThisDocument
Now, in ThisDocument class should create objects and linking
Dim oEventsManager As New clsEvents
Dim oEditedButton As New clsButton
Const BUTTON_LINKS As String = "cmdUpdateLinks" 'For example
Dim oInShpDoc As Word.InlineShape, oOleDoc As Word.OLEFormat, oInShapesDoc As Word.InlineShapes
Public Sub Set_Manager_Events()
Set oEventsManager.appWord = Word.Application 'ThisDocument.Application
Set oEventsManager.docWord = ThisDocument
Set oInShpDoc = FNC_oGet_Button_Variable(BUTTON_LINKS)
If Not oInShpDoc Is Nothing Then
Set oEditedButton.button = oInShpDoc
Set oEventsManager.button = oEditedButton
End If
End Sub
'###### EVENTOS OF BUTTON
Private Sub cmdUpdateLinks_Click()
If oEventsManager.appWord Is Nothing Then Call Set_Manager_Events
Call UpdateLinks ' Is one example
End Sub
Public Function FNC_oGet_Button_Variable(sCodeName As String) As InlineShape
Dim oForm As InlineShape, oFormsInLine As InlineShapes, oOLEFormat As OLEFormat
Set oFormsInLine = ThisDocument.InlineShapes
If oFormsInLine .Count < 1 Then GoTo bye
i = 0
For Each oForm In oFormsInLine
With oForm
Set oOLEFormat = .OLEFormat
If Not oOLEFormat Is Nothing Then
If InStr(1, oOLEFormat.ClassType, "CommandButton") > 0 Then
If .OLEFormat.Object.Name = sCodeName Then
Set FNC_oGet_Button_Variable= oForm
Exit Function
End If
End If
End If
End With
Next
bye:
Set FNC_oGet_Button_Variable = Nothing
End Function
With this you will can hide button for printing.
I am maintaining a Word related vb6 project. When we download a word document from our server and make changes on it. Then click Close.
Then
the objWord_DocumentBeforeClose event will occur.
If the downloaded file has not uploaded back to server, then we prompt a yes/no message box Do you want to check-in the document?
If clicked on 'Yes' and if click again on ctrl+w before the check-in process completed, document will close without reaching the mobjWord_DocumentBeforeCloseevent.
I have added some code in the DocumentBeforeClose event to prevent closing the document if the check-in process is running using document variables.
Could anybody please explain me why the mobjWord_DocumentBeforeClose is not reaching at the second close click?
Below is my code.
Private Sub mobjWord_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
Dim objDoc As Project.Document
Dim objApp As Project.Application
Dim strProcess As String
Set objApp = New Project.Application
If objApp.Settings.RespondToWordEvents Then
Set objDoc = objApp.GetDocument(Doc)
'Check the document variable to seen if any process is running with the document
If objDoc.IsBusy = False Then
if objDoc.NotCheckedIn
If objDoc.DownloadProperties.WasCheckedOut Then
Select Case MsgBox("Do you want to check-in the document?", vbYesNoCancel + vbQuestion)
Case vbYes
If objApp.CheckInDocument(WordDocument:=Doc) Is Nothing Then
Cancel = True
End If
fDisebleCheckIn = True
Case vbNo
fDisebleCheckIn = True
Case vbCancel
Cancel = True
End Select
End If
Else
//some code
End If
Else
Cancel = True
strProcess = ProcessInProgress(objDoc, objApp)
MsgBox objApp.GetUIString("Unable to close the document " + strProcess + "process is running"), vbOKOnly + vbInformation
End If
End If
ErrorHandler:
objApp.Quit
Set objDoc = Nothing
Set objApp = Nothing
End Sub
I am not sure I fully understand you, but in VBA, so you can adapt for your VB6, I would have the following in a normal module
Public wdCustomWordApplication As clsCustomWordApplication
Sub Setup()
Dim w As Word.Application
Set w = New Word.Application
w.Visible = True
Set wdCustomWordApplication = New clsCustomWordApplication
wdCustomWordApplication.InitialiseCustomWordApplication w
End Sub
and then a class module called clsCustomWordApplication, like so
Private WithEvents wdWordApplication As Word.Application
Public Sub InitialiseCustomWordApplication(objWord As Word.Application)
Set wdWordApplication = objWord
End Sub
Private Sub wdWordApplication_DocumentBeforeClose( _
ByVal Doc As Word.Document, Cancel As Boolean)
MsgBox "Some question"
End Sub
Hope this helps.
I have a button in a workdbook (wbShared), clicking on that button a second workbook (wbNewUnshared) opens. I want to add a button to wbNewUnshared with code programmatically.
I already found how to add the button, but I didn't find how to add code to this button.
'create button
'--------------------------------------------------------
Dim objBtn As Object
Dim ws As Worksheet
Dim celLeft As Integer
Dim celTop As Integer
Dim celWidth As Integer
Dim celHeight As Integer
Set ws = wbNewUnshared.Sheets("Sheet1")
celLeft = ws.Range("S3").left
celTop = ws.Range("T2").top
celWidth = ws.Range("S2:T2").width
celHeight = ws.Range("S2:S3").height
Set objBtn = ws.OLEObjects.add(classType:="Forms.CommandButton.1", link:=False, _
displayasicon:=False, left:=celLeft, top:=celTop, width:=celWidth, height:=celHeight)
objBtn.name = "Save"
'buttonn text
ws.OLEObjects(1).Object.Caption = "Save"
I found this online:
'macro text
' Code = "Sub ButtonTest_Click()" & vbCrLf
' Code = Code & "Call Tester" & vbCrLf
' Code = Code & "End Sub"
' 'add macro at the end of the sheet module
' With wbNewUnshared.VBProject.VBComponents(ActiveSheet.name).codeModule
' .InsertLines .CountOfLines + 1, Code
' End With
But this gives an error in the last line. Anybody has a clue?
tx
EDIT:
SOLVED
Ok, the code given works, I had an error 'Programmatic Access To Visual Basic Project Is Not Trusted'. Thanks to the help of S Meaden I solved that via https://support.winshuttle.com/s/article/Error-Programmatic-Access-To-Visual-Basic-Project-Is-Not-Trusted.
after that my code worked. So thanks again.
The first code I provided assumes 1 workbook. The code I'm presenting now does not. The limitation of this is that if the arrBttns is lost, the project is reset, the link between the code and the button is lost and the procedure addCodeToButtons has to be run again.
In the wbNewUnshared, create a class module with the following code
Option Explicit
Public WithEvents cmdButtonSave As MSForms.CommandButton
Public WithEvents cmdButtonDoStuff As MSForms.CommandButton
Private Sub cmdButtonDoStuff_Click()
'Your code to execut on "Do Stuff" button click goes here
MsgBox "You've just clicked the Do Stuff button"
End Sub
Private Sub cmdButtonSave_Click()
'Your code to execut on "Save" button click goes here
MsgBox "You've just clicked the Save button"
End Sub
In the wbNewUnshared add a standard module with the following code
Option Explicit
Dim arrBttns() As New Class1
Public Sub addCodeToButtons()
Dim bttn As OLEObject
Dim ws As Worksheet
Dim i As Long
ReDim arrBttns(0)
'Iterate through worksheets
For Each ws In ThisWorkbook.Worksheets
'Iterate through buttons on worksheet
For Each bttn In ws.OLEObjects
'Expand arrBttns for valid buttons.
If bttn.Name = "Save" Or bttn.Name = "DoStuff" Then
If UBound(arrBttns) = 0 Then
ReDim arrBttns(1 To 1)
Else
ReDim Preserve arrBttns(1 To UBound(arrBttns) + 1)
End If
End If
'Link button to correct code
Select Case bttn.Name
Case "Save"
Set arrBttns(UBound(arrBttns)).cmdButtonSave = bttn.Object
Case "DoStuff"
Set arrBttns(UBound(arrBttns)).cmdButtonDoStuff = bttn.Object
End Select
Next bttn
Next ws
End Sub
In the wbNewUnshared add the following code in the ThisWorkbook module, this is to add the code to the buttons on workbook open.
Option Explicit
Private Sub Workbook_Open()
Call addCodeToButtons
End Sub
In the wbShared add the following line after you're done adding buttons
Application.Run "wbNewUnshared.xlsm!addCodeToButtons"
Original Answer
Add a class module to your project to which you add.
Option Explicit
Public WithEvents cmdButton As MSForms.CommandButton 'cmdButton can be an name you like, if changed be sure to also change the Private Sub below
Private Sub cmdButton_Click()
'Your code on button click goes here
MsgBox "You just clicked me!"
End Sub
To a module you add the code below
Option Explicit
Dim arrBttns() As New Class1 'Change Class1 to the actual name of your classmodule
'The sub which adds a button
Sub addButton()
Dim bttn As OLEObject
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set bttn = ws.OLEObjects.Add(ClassType:="Forms.CommandButton.1")
ReDim arrBttns(0)
If UBound(arrBttns) = 0 Then
ReDim arrBttns(1 To 1)
Else
ReDim Preserve arrBttns(1 To UBound(arrBttns))
End If
Set arrBttns(UBound(arrBttns)).cmdBttn = bttn.Object
End Sub
I'm trying to make an Excel Add-In to create a simple button when any workbook is opened, but I'm getting
Object variable or With Block variable not set
I think this is happening because technically there is no 'ActiveWorkbook' yet.
First thing I want to do is delete any buttons currently on the sheet. Then I want to place a button.
Anyone know how to make that happen?
Code
Private Sub Workbook_Open()
ActiveWorkbook.ActiveSheet.Buttons.Delete
Dim CommandButton As Button
Set CommandButton = ActiveWorkbook.ActiveSheet.Buttons.Add(1200, 100, 200, 75)
With CommandButton
.OnAction = "Test_Press"
.Caption = "Press for Test"
.Name = "Test"
End With
End Sub
I then have a Private Sub Test_Press() to display a MsgBox. The button is not being created though.
Credit goes to http://www.jkp-ads.com/Articles/FixLinks2UDF.asp
Note: I have another module I didn't post below, which houses the macro Project_Count I tied to the button I place on the workbook only if the workbook name is TT_GO_ExceptionReport
I also have a VBScript that downloads the Add-In, places it in the users addin folder, and installs it. If you want to know how to do that, leave a comment.
Code of Add-In that solved the problem:
ThisWorkbook
Option Explicit
Private Sub Workbook_Open()
' Purpose : Code run at opening of workbook
'-------------------------------------------------------------------------
'Initialise the application
InitApp
modProcessWBOpen.TimesLooped = 0
Application.OnTime Now + TimeValue("00:00:03"), "CheckIfBookOpened"
End Sub
Module 1 named modInit
Option Explicit
'Create a module level object variable that will keep the instance of the
'event listener in memory (and hence alive)
Dim moAppEventHandler As cAppEvents
Sub InitApp()
'Create a new instance of cAppEvents class
Set moAppEventHandler = New cAppEvents
With moAppEventHandler
'Tell it to listen to Excel's events
Set .App = Application
End With
End Sub
Module 2 named modProcessWBOpen
Option Explicit
'Counter to keep track of how many workbooks are open
Dim mlBookCount As Long
'Counter to check how many times we've looped
Private mlTimesLooped As Long
' Purpose : When a new workbook is opened, this sub will be run.
' Called from: clsAppEvents.App_Workbook_Open and ThisWorkbook.Workbook_Open
'-------------------------------------------------------------------------
Sub ProcessNewBookOpened(oBk As Workbook)
If oBk Is Nothing Then Exit Sub
If oBk Is ThisWorkbook Then Exit Sub
If oBk.IsInplace Then Exit Sub
CountBooks
'This checks to make sure the name of the new book matches what I
'want to place the button on
If oBk.Name = "TT_GO_ExceptionReport.xlsm" Then
Dim CommandButton As Button
Set CommandButton = Workbooks("TT_GO_ExceptionReport.xlsm").Sheets(1).Buttons.Add(1200, 100, 200, 75)
With CommandButton
.OnAction = "Project_Count"
.Caption = "Press for Simplified Overview"
.Name = "Simplified Overview"
End With
End If
End Sub
Sub CountBooks()
mlBookCount = Workbooks.Count
End Sub
Function BookAdded() As Boolean
If mlBookCount <> Workbooks.Count Then
BookAdded = True
CountBooks
End If
End Function
' Purpose : Checks if a new workbook has been opened
' (repeatedly until activeworkbook is not nothing)
'-------------------------------------------------------------------------
Sub CheckIfBookOpened()
If BookAdded Then
If ActiveWorkbook Is Nothing Then
mlBookCount = 0
TimesLooped = TimesLooped + 1
'May be needed if Excel is opened from Internet explorer
Application.Visible = True
If TimesLooped < 20 Then
Application.OnTime Now + TimeValue("00:00:01"), "CheckIfBookOpened"
Else
TimesLooped = 0
End If
Else
ProcessNewBookOpened ActiveWorkbook
End If
End If
End Sub
Public Property Get TimesLooped() As Long
TimesLooped = mlTimesLooped
End Property
Public Property Let TimesLooped(ByVal lTimesLooped As Long)
mlTimesLooped = lTimesLooped
End Property
Class Module named cAppEvents
' Purpose : Handles Excel Application events
'-------------------------------------------------------------------------
Option Explicit
'This object variable will hold the object who's events we want to respond to
Public WithEvents App As Application
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
'Make sure newly opened book is valid
ProcessNewBookOpened Wb
End Sub
Private Sub Class_Terminate()
Set App = Nothing
End Sub
something like this?
Option Explicit
Sub Button()
Dim cButton As Button
Dim rng As Range
Dim i As Long
ActiveSheet.Buttons.Delete
For i = 2 To 3 Step 2
Set rng = ActiveSheet.Range(Cells(i, 2), Cells(i, 2))
Set cButton = ActiveSheet.Buttons.Add(rng.Left, rng.Top, rng.Width, rng.Height)
With cButton
.OnAction = "Test_Press"
.Caption = "Press for Test " & i
.Name = "Test" & i
End With
Next i
End Sub
See Example here