i'm making a code for Excel that opens every file in a specific folder. When the file is open I like to add a button to each of these files at the same location. I made this code shown as below. but somehow I get this error:
Run-Time Error '1004': Unable to get the Add property of the
OLEObjects class
The code breaks on the line where it says: set addbutton = etc...
Does anyone know why?
My Code:
Dim AddButton As OLEObject
Set AddButton =
Workbooks(ThisWB).Sheets("Planning").OLEObjects.add(ClassType:="Forms.CommandButton.1", Link:=False,
DisplayAsIcon:=False, Left:=3.52941176470588, Top:=106.764705882353,
Width:=47.6470588235294, Height:=24.7058823529412)
With AddButton
.Name = "SortPlanner"
.OnAction = "SortPersonalPlanner"
With .Object
.Caption = "Sorteren"
.BackColor = &HFFFFFF
End With
End With
You can do something like this, use .Buttons.Add instead of .OLEObjects.add
Set AddButton = Workbooks(ThisWB).Sheets("Planning").Buttons.Add(3.53, 106.76, 47.65, 24.71)
With AddButton
.Characters.Text = "Sorteren"
.Font.Bold = True
.OnAction = "SortPersonalPlanner"
End With
Related
I'm using the code below to add a toggle button to sheet. I will need to dynamically re-create a sequence of buttons, and give them name and caption.
Can anyone help me with a way to change the caption/text of the toggle button button added using VBA? The bName will change so I will need a way to reference them by name.
Set Bttn = ActiveSheet.OLEObjects.Add(ClassType:="Forms.ToggleButton.1", Link:=False, _
DisplayAsIcon:=False, Left:=R.Left, Top:=R.Top, Width:=R.Width, Height:=R.Height)
With Bttn
.Name = bName
End With
where R is the target cell where button will be added
I tried the following, and they all error out:
Bttn.caption = bText
ActiveSheet.Shapes(bName).Text = bText
Or, simply . . .
With Bttn
.Name = bName
.Object.Caption = "MyCaption"
End With
I found this workaround here: https://stackoverflow.com/a/37978572/9852011
Not sure why you need to do this exactly but this code should work for you:
Dim Bttn As OLEObject, aButton As Variant
Set Bttn = ActiveSheet.OLEObjects.Add(ClassType:="Forms.ToggleButton.1", Link:=False, _
DisplayAsIcon:=False, Left:=R.Left, Top:=R.Top, Width:=R.Width, Height:=R.Height)
With Bttn
.Name = bName
End With
Set aButton = Bttn.Object
aButton.Caption = "whateverYouWant"
I am using this code to add an entry to excel right_click menu:
Private Sub Workbook_Open()
Application.ShortcutMenus(xlWorksheetCell).MenuItems.Add "Open document", "OpenDocument", , 1, , ""
End Sub
Sub OpenDocument()
‘vba code here
End Sub
I need to add an icon to this entry (using shell32.dl or any standalone image), as it is now blank.
Your requirement can be solved in more ways, but (at least, this is what I know how to handle) using a different approach (CommandBar):
To place a custom picture, please try the first version. It uses a picture from a specific path:
Sub AddItemContextMenuWithImage_1()
Const butName As String = "Open document"
Const calledProc As String = "testSubX"
deleteCellCustomControl butName
Dim cmBar As CommandBar, ctrlButt As CommandBarButton, picPicture As IPictureDisp
Set cmBar = Application.CommandBars("Cell")
Set ctrlButt = cmBar.Controls.Add(Type:=msoControlButton, Before:=1) 'create it to be the first menu option
Set picPicture = stdole.StdFunctions.LoadPicture(ThisWorkbook.Path & "\test.gif") 'accepted extensions: bmp, jpg, gif
With ctrlButt
.Picture = picPicture
.OnAction = calledProc
.Caption = butName
End With
End Sub
To check it, the demonstative Sub should look as:
Sub testSubX()
MsgBox "It works..."
End Sub
Of course, you may adapt the code to call your own/necessary Sub...
A second version uses/copies a picture already added on a specific sheet of ThisWorkbook:
Sub AddItemContextMenuWithImage_2()
Const butName As String = "Open document"
Const calledProc As String = "testSubX"
deleteCellCustomControl butName
Dim cmBar As CommandBar, ctrlButt As CommandBarButton
Set cmBar = Application.CommandBars("Cell")
Set ctrlButt = cmBar.Controls.Add(Type:=msoControlButton, Before:=1) 'create it to be the first menu option
ActiveSheet.Pictures("Picture 2").Copy 'need to have a "Picture 2" picture on the active sheet
'you can copy it as image of the newly added control button
With ctrlButt
.PasteFace 'paste the above copied picture
.OnAction = calledProc
.Caption = butName
End With
End Sub
The third version uses standard, already defined FaceIDs. There are so many, that it is very probable to find something suitable for your need, so this is the version I prefer:
Sub AddItemContextMenuWithImage_3()
'Here the list of FaceID controls with their images:
'https://bettersolutions.com/vba/ribbon/face-ids-2003.htm
Const butName As String = "Open document"
Const calledProc As String = "testSubX"
deleteCellCustomControl butName
Dim cmBar As CommandBar, ctrlButt As CommandBarButton
Set cmBar = Application.CommandBars("Cell")
Set ctrlButt = cmBar.Controls.Add(Type:=msoControlButton, Before:=1) 'create it to be the first menu option
With ctrlButt
.FaceId = 1661
.OnAction = calledProc
.Caption = butName
End With
End Sub
A lot of such FaceIDs can be found here. I also place the link as a comment inside the Sub, to remain there for people being interested in this approach...
All the above Subs firstly call the next Sub, to preliminarily delete the menu option, if it already exists:
Sub deleteCellCustomControl(strBut As String)
On Error Resume Next 'for the case of not existing button to be deleted...
Application.ShortcutMenus(xlWorksheetCell).MenuItems(strBut).Delete
On Error GoTo 0
End Sub
If there is only such a custom option in the context menu, or if you want deleting all of them (the custom once), you can simple reset the command Bar, using:
Private Sub ResetContextMenuBar()
Application.CommandBars("Cell").Reset
End Sub
I used code provided on Creating form programmatically in the module using vba to create the dynamic form, everything works great for the most part, however, the only way I can close the form is using the red "X" in the corner and I want to be able to close it with a button.
I have tried using the simple Unload UserForm1 and tried to call the UserForm_QueryClose command as well, but nothing seems to work. I have also tried ThisWorkbook.VBProject.VBComponents.Remove ("UserForm1"), but I push the button and nothing happens.
This creates the form:
Set myForm = ThisWorkbook.VBProject.VBComponents.Add(3)
'Create the User Form
With myForm
.Properties("Width") = 270
.Properties("Height") = 376
End With
This creates the command button:
'Create CommandButton Create
Set NewButton = myForm.designer.Controls.Add("Forms.commandbutton.1")
With NewButton
.Name = "cmd1"
.Caption = "Ok"
.Accelerator = "M"
.Top = 100
.Left = 200
.Width = 66
.Height = 20
.Font.Size = 8
.Font.Name = "Tahoma"
.BackStyle = fmBackStyleOpaque
End With
This fills out the code in the form:
'add code for Command Button
myForm.CodeModule.InsertLines 16, "Private Sub cmd1_Click()"
myForm.CodeModule.InsertLines 17, "Unload UserForm1"
myForm.CodeModule.InsertLines 18, "End Sub"
I expect the form to unload once the button is pushed and move on to the next procedure, but nothing happens. I have tested other commands when hitting the button and it seems to work, but I just can't get it unloaded without pushing the red "X".
I am going to create a userform programmatically, I am able to create other controls like commandbuttons, textboxes, optionbuttons...etc., as my wish.
But I can not figure out the way to set the font and font size at the beginning of creating the userform programmatically.
As there are near hundred controls, it would be better for me to set the font at the beginning, otherwise, I may set the font manually afterwards.
I tried the following for setting:
Dim NewForm As Object
Application.VBE.MainWindow.Visible = True
Set NewForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
With NewForm
.Properties("Caption") = ""
.Properties("Width") = 400
.Properties("Height") = 400
------> 'Properties("Font.Name") = "Arial"
'Properties("Font.size") = 9
'Or
------> '.Font = "Arial"
'.font.size = 9
.Name = "frmWebScraping"
End With
'code for creating other controls with no problem is neglected
End Sub
The correct properties are:
.Properties("Font").Value.Item("Name") = "Arial"
.Properties("Font").Value.Item("Size") = 9
on MAC OFFICE 2011 I am trying to get this working: http://www.contextures.com/xlDataVal10.html
There is a sample file that you can download: http://www.contextures.com/DataValCombobox.zip
But when I open the worksheet and click on a cell I get the following error:
"Method or data member not found"
Set cboTemp = ws.OLEObjects("TempCombo")
On Error Resume Next
If cboTemp.Visible = True Then
With cboTemp
.Top = 10
.Left = 10
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = "" <<<- HIGHLIGHTED
End With
End If
The debugger highlights the .value as indicated above.
What is the cause of this and can it be fixed ?
It's because .Value isn't a property of the ComboBox.
Here is the list of properties you can use.
You are probably looking for
.SelectedValue
or Depending on which column you have the Rowsource bound.
.SelectedIndex
You can also use
.Text