Programatically adding and modifying command buttons (active X control) - Excel VBA - vba

Hello this have been giving me a headache for a while.
I want to simply be able to remove existing buttons and add new ones. I already have ready made functions xbutton_click() and ybutton_click(). I tried adding buttons like this:
Set btn= ActiveSheet.Buttons.Add(Range("B3").Left, Range("B3").Top, Range("B3").Width, Range("B3").Height)
With openForm
.OnAction = "new_Click"
.Caption = "new"
.Name = "newButton"
End With
but those seem to only work with macros which is not what I want. I realized that when I manually add a button, I add the active X control button. I want that. It has a name property, which can be changed, and its function is basically name_click(), perfect!
Now when it comes to adding them its a headache. I have surfed the internet but it is full of outdated solutions (I am using 2013). I could only add new command buttons, but couldn't change their caption, name, nothing.
this is the code for adding a "command button" (not "button"):
Set objBtn = ws.OLEObjects.Add(ClassType:="Forms.CommandButton.1", link:=False, _
displayasicon:=False, Left:=celLeft, Top:=celTop, Width:=celWidth, Height:=celHeight)
Adding the ActiveX Command Button works, but I cannot change its properties like "Caption". Can someone please guide me through this maze?
Edit
objBtn.Name = "newName" works. But objBtn.Caption = "newCaption" gives an error 438.

For OLEObjects, you can't do it just a caption this should solve it
Dim objBtn As OLEObject
Set objBtn = ws.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, _
DisplayAsIcon:=False)
objBtn.Object.Caption = "Example"
Notice you were missing .Object.

Related

How do you show a panel in VB dot net, if all the panels are created dynamically and sit in the exact same spot?

I am using Visual Studio 2022 and my language of choice is VB. When the program starts it creates Multiple Panels that are located in the same position created from data from a database.
No matter what I do, I can not get any of the panels to show. I can search for the panels and verify the panels exist, I can search the panels and verify that there are buttons in the panels. However I can not get each individual panel to show.
I am going to list all the panels that are created in the order they are created and they are all in the exact same position and size.
panel2
pnlDeptItems01
pnlDeptItems11
pnlDeptItems22
pnlDeptItems23
pnlDeptItems24
pnlDeptItems25
pnlDeptItems26
pnlDeptItems27
pnlDeptItems28
pnlDeptItems29
pnlDeptItems32
pnlDeptItems54
pnlDeptItems82
pnlDeptItems83
pnlDeptItems84
pnlDeptItems85
pnlDeptItems96
pnlDeptItems97
pnlDeptItems98
pnlDeptItems99
Visible and Enabled are set to true for all panels on creation.
Below is the code that I use to search for the panel by name and if I find it then try to make it visible which it already is and then I try to bring it to front. And it does absolutely nothing but keeps the 1st panel visible.
For Each control In Me.Controls.OfType(Of Panel)
If control.Name = btnPanelName Then
control.Controls.Add(oButton)
control.Visible = True
control.BringToFront()
Console.WriteLine(control.Name & " " & oButton.Name)
End If
Next
CODE TO CREATE PANELS
Private Sub CreateNewPanel(ByRef pnlName As String, ByRef pnltopx As Integer, ByRef pnlLefty As Integer, ByRef pnlSizex As Integer, ByRef pnlSizey As Integer)
Dim iPanel As Panel
iPanel = New Panel
iPanel.Name = pnlName
iPanel.Enabled = True
iPanel.Visible = False
iPanel.Top = pnltopx
iPanel.Left = pnlLefty
iPanel.Size = New Size(pnlSizex, pnlSizey)
iPanel.AutoScroll = True
Me.Controls.Add(iPanel)
If iPanel.Name = "pnlDepartments" Or iPanel.Name = "pnlDeptItems26" Then
iPanel.Visible = True
iPanel.BringToFront()
End If
End Sub
I then tried doing this and this does not work either:
For Each control In Me.Controls.OfType(Of Panel)
If control.Name = btnPanelName Then
control.Controls.Add(oButton)
End If
If control.Name = "pnlDeptItems26" Then
control.Visible = True
End If
Next
Code that is called to create the panels
CreateNewPanel("pnlDeptItems" & dataDeptId, 608, 5, 429, 479)
dataDeptId is pulled from a database in a loop.
Can someone explain to me why this is not working or what the work around is? I know it has something to do with all the panels that are created being created in the exact same position.
Thanks,
Shawn Mulligan
I found the solution. After reading my post I took notice that for some reason that my paneltopx and my panellefty were reversed and too large for some reason as well as my panelsizey and panelsizex.
I made the adjustments and made sure that all panels visibility was set to false, and now when I set visibility to true, the correct panel displays with the buttons that I created.
Thanks,
Shawn Mulligan

DataGridView: programmatically select/highlight entire column on button click

I want to programmatically select/highlight entire column on button click to let users know what they searched.
This is what currently happens on button click ("GO" button)
but this is what I need to happen.
I have tried these so far to no avail:
DataGridView2.SelectionMode = DataGridViewSelectionMode.FullColumnSelect
DataGridView2.Columns(2).Selected = True
Doing so gave me this error: DataGridView control's SelectionMode cannot be set to FullColumnSelect while it has a column with SortMode set to DataGridViewColumnSortMode.Automatic.
I also tried to simply select the whole column. No error, but it didn't work.
DataGridView2.Columns(2).Selected = True
Just set the Selected property of each cell in the column.
Found this thread. I modified the code like so:
Dim row As DataGridViewRow
For Each row In DataGridView2.Rows
row.Cells(1).Selected = True
Next
and placed it inside Private Sub Button1_Click
I noticed that the first cell is automatically selected. This solved that problem.

Word CheckBox ContentContol OnChange Event

I try to make a word document with two checkboxes where each checkbox will show/hide a part of the document with a custom style.
I plan to set value Style.Font.Hidden = True/False depending from checkbox values, but...
I found that there are 3 types of controls:
Legacy Controls - This seems old the ugly.
ActiveX Controls - I can easyly attach to checkbox onChange events, but these are also ugly and I think it's not that secure, also this is probably now working on mac.
ContentControls - This seems like the right way to do this, but I just can't attach to the right event. (Also there is some XML attachment described, but I'm not using this, this seem too complicated, I don't know.)
Can you tell me how to atach to onChange event of CheckBox ContentContol? I need the same behaviour like it's ActiveX CheckBox.
Content Controls do not have "onChange" events, so you can't get a content control to behave like the ActiveX checkbox in a simple manner. Similarly to form fields, the code for ContentControls fires when the control is entered/exited.
The only way to emulate "onChange" for a Content Control is to link the content control to a node in a CustomXMLPart in the document then work with the Document_ContentControlBeforeStoreUpdate event that triggers when the content of the node in the CustomXMLPart is going to be changed.
If, as your question indicates, this is too complex for your purposes you could use a MacroButton field that displays a font character (Symbol) that looks like a checkbox. Clicking the field would exchange that character for a different one, that looks checked. And the reverse again for the next click. Here's some sample code to get you started. If you don't like the checkboxes I chose, you can pick something else from Insert/Symbols/Symbol. Just change the character numbers and the font name.
By default a MacroButton field triggers on double click. You can change this to a single click when the document is opened in an AutoOpen macro.
Sub AutoOpen()
Application.Options.ButtonFieldClicks = 1
End Sub
Sub ToggleCheckBox()
Dim iNotChecked As Integer, iChecked As Integer
Dim rngCheck As word.Range
Dim sBkmName As String, sFontName as String
iNotChecked = 111
iChecked = 253
sBkmName = "bkmCheck"
sFontName = "Wingdings"
Set rngCheck = ActiveDocument.Bookmarks(sBkmName).Range
If Asc(rngCheck.Text) = iNotChecked Then
rngCheck.Text = Chr(iChecked)
ActiveDocument.Bookmarks.Add sBkmName, rngCheck
rngCheck.Font.Name = sFontName
ElseIf Asc(rngCheck.Text) = iChecked Then
rngCheck.Text = Chr(iNotChecked)
rngCheck.Font.Name = sFontName
ActiveDocument.Bookmarks.Add sBkmName, rngCheck
End If
End Sub

customize shorcut (right-click) menu powerpoint

I want to make appear a new element on the shortcut menu, known also as right-click menu. I want this element to execute a macro that I made. I check on the web but I could find a solution that really work. I put here which one I tried:
Public Sub customizeRightClick()
Dim pic As IPictureDisp
Set pic = LoadPicture("C:\path\pic.jpg")
For Each oCmdBar In Application.CommandBars
If oCmdBar.Type = msoBarTypePopup Then
If oCmdBar.Name = "Shapes" Then
Set cmdButton = oCmdBar.Controls.Add(Type:=msoControlButton)
With cmdButton
.Caption = "Edit Element"
.Tag = "Edit"
.Picture = pic 'Object of type IPictureDisp
.OnAction = "editMag"
End With
End If
End If
Next
End Sub
I check in the Watches and it add the control but when I use right-click on a shape the options doesn't appear. Maybe I am not putting where it have to be but I cannot find anywhere an explanation about where is the correct place to set the new element.
David's correct re 2007, but it seems they've added the ability to customize context menus back in PPT 2010:
http://social.msdn.microsoft.com/forums/office/en-US/c1eb22ba-6ca8-4c21-8100-62185355aa53/customize-rightclick-context-menu-in-powerpoint-2010
OK I have confirmed that there is:
Limited support for shortcut menu customizations in PPT 2007+
specifically. ....You cannot add any item to the shapes shortcut menu
(unless it is an activex control) in PPT 2007+.
http://answers.microsoft.com/en-us/office/forum/office_2007-customize/customizing-right-click-menu/76aff9b3-9253-40cf-bd21-e1f832144ad8
You may be able to do this with Ribbon/XML interface, but again it may be subject to the annoying limitation that they put on certain context menus. It might simply not be possible to do what you want to do in this version of PowerPoint.
http://msdn.microsoft.com/en-us/library/gg469862.aspx#odc_xl_ta_CustomExcelContextMenus_AddDynamicMenu

how to get the object associated with a context menu in an office add-in

I have a simple COM add-in for office that I am developing (for access specifically).
I have added a custom commandbarbutton item to the context menu that pops up when you right click on an object in the navigation pane.
This works fine. The debug code I added runs (currently just a msgbox command). The one thing I cannot figure out how to do though is get an object for the object bound to the context menu.
I would like this to happen; I right click on a module in the navigation pane, select my new menu option, and then a message box appears with the name of the module that is currently highlighted. How would I go about this?
This is how I am currently handling the event:
Public Sub myEventHandler(ByVal ctrl As CommandBarButton, ByRef CancelDefault As Boolean) Handles contextMenu_navPaneObject.Click, contextMenu_navPaneList.Click
MsgBox(Microsoft.VisualBasic.Information.TypeName(ctrl) & vbCrLf & _
Microsoft.VisualBasic.Information.TypeName(ctrl.Parent) & vbCrLf & _
Microsoft.VisualBasic.Information.TypeName(ctrl.Parent.Parent))
End Sub
contextMenu_navPaneObject and contextMenu_navPaneList are private objects declared using "withevents" and having an object type of commandbarbutton.
Is this the correct way to do what I want, or is there an alternative method I should be using?
Turns out to do what I wanted, I needed to run the following method:
Access.Application.CurrentObjectName()
This returned the name of the item I currently had highlighted.
I hope this helps others!