set ActiveX checkbox properties with VBA word 2010 - vba

I have searched high and low for this, but no luck.
To create a checkbox:
Selection.InlineShapes.AddOLEControl ClassType:="Forms.CheckBox.1"
However, there are several properties associated with a checkbox, and I wanted to find out how to set them as well when I am creating the checkbox.
For example, I tried this:
Selection.InlineShapes.AddOLEControl ClassType:="Forms.CheckBox.1", Caption:=""
But the code throws a "Named Argument Not Found" error while highlighting Caption:=""

For anyone with the same issue
'to place it in the table, assuming only 1 table, and in this example the checkbox is placed in the second Row, first Column
ActiveDocument.Tables(1).Cell(2, 1).Select
Set myOB = Selection.InlineShapes.AddOLEControl(ClassType:="Forms.CheckBox.1")
With myOB.OLEFormat
.Activate
Set myObj = .Object
End With
With myObj
'now you can name the field anything you want
.Name = "CB1"
.Value = False
'delete the caption, or have it say what you want
.Caption = ""
.Height = 22
.Width = 22
End With

Related

How can I set OptionButton font using VBA For Loop?

I am trying to manually set every OptionButton's font on a sheet to be a uniform size and type using a For Loop.
I can do them manually by writing out each specific button's information but I have hundreds of buttons.
I can even get VBA to write the correct syntax to a test Worksheet by using this code here:
`Private Sub Thisworkbook_Open()
For i = 1 to Worksheets("Core").OLEObjects.Count
If TypeName(Worksheets("Core").OLEObjects(i).Object) = "OptionButton" Then
Worksheets("testsheet").Range("A" & i).Value = Worksheets("Core").OLEObjects(i).Name
End If
Next i
End Sub`
But what I can't do is put the rest of this below code along with the above code to have ONE clean and concise statement that will manually set all OptionButton values to these settings:
`With Worksheets("Core").OptionButton1
.Font.Size = 11
.Font.Name = "Calibri"
.Font.Bold = False
End With`
Can someone explain to me how I can make this work?
Actually you have your answer in your question, all you have to do is to put your properties to correct location, as follows:
For i = 1 To Worksheets("Core").OLEObjects.Count
If TypeName(Worksheets("Core").OLEObjects(i).Object) = "OptionButton" Then
Worksheets("Core").OLEObjects(i).Object.FontSize = 5
' Remaining code goes here.
End If
Next i

Excel VBA: Listbox Error when assigning Linked Cell

I have some cells with Data Validation. Because the dropdown list is small and hard to read, I have a button which opens a list box and populates it with the cell's Data Validation list.
Dim btnAddToList As OLEObject
Public lboTemp As OLEObject
Set btnAddToList = ws.OLEObjects("btnAddToList")
Set lboTemp = ws.OLEObjects("TempListBoxS")
Set Field = Selection ' This is always cell $D$1, $D$2, or $D$3
btnAddToList.Visible = False
'Create a named range "temp"
ActiveWorkbook.Names.Add Name:="temp", RefersTo:=Field.Validation.Formula1
' open list box
' position list box
' load it with "temp"
With lboTemp
'show the listbox with the list
.Visible = True
.Left = Field.Left
.Top = Field.Top + 50
.ListFillRange = "temp"
.Object.MultiSelect = 0 ' Single select
On Error GoTo errHandler
prev = .LinkedCell
If prev <> "" Then prev = prev & ": " & Range(.LinkedCell).Value ' for debugging
.LinkedCell = Field.Address 'SOMETIMES THIS GIVES Err 440: could not set property value, invalid property value
.Width = Field.Width + 5
.Height = WorksheetFunction.Min(270, .Object.ListCount * 20) 'field.Height + 5
End With
As noted in the comment above, I sometimes, but not always, I get an error when the LinkedCell is supposed to be populated by with the Field.Address.
This code is used by six different cells (D1:D3 on two different worksheets), but the error only appears to occur when one of the D1 cells is the one selected. Those cells have one other thing in common: their data validation lists, respectively, are:
='Category Table'!$F$2:$F$31 and
='Category Table'!$F$32:$F$41
The other four cells -- which don't get the error -- use a complicated dynamic range that references a different table on the "Category Table" sheet. (I don't really think this has anything to do with my problem, but I don't see anything else those cells have in common)
If no one can give me an answer, I'd appreciate some advice on how to track down an intermittent problem.
Thanks!
Maybe the problem is the return of Field.Address. The .Adress property returns, by default, with the absolute value of row and column. You can try Field.Address(RowAabsolute:=false, ColumnAbsolute:=false) . Hope this help.
and sorry my english.
I managed to stop this issue by ensuring that the linked cell was empty before creating the reference.

Reading Userform Object Values

I created a Userform (manually in the VBA Projectbrowser). I have written VBA code, which fills this Userform with different Objects in runtime (Labels, Optionbuttons etc.). So far everything worked fine
The Userform is filled with data read from my Excel sheets and correctly displayed. However I'm not able to read the inputs from the objects on it (for example Optionbutton - TRUE or FALSE). These objects do not appear anywhere (except on the userform) so that I can link them and use them in another Module.
I guess they are only displayed and not really read into the memory or whatever (initialized !?).
There are two ways to go about it.
WAY 1
Declare your option button object as Public.
Module Code
Public theOpBut As Object
Sub Fill()
If theOpBut.Value = True Then
ActiveSheet.Cells(1, 5) = 1
Else
ActiveSheet.Cells(1, 5) = "NO"
End If
End Sub
Userform Code
Private Sub UserForm_Initialize()
Set theOpBut = UserForm1.Controls.Add("Forms.optionbutton.1", "OptionButton", True)
With theOpBut
.Caption = "Test Button"
'.GroupName = OpButGroupCounter
.Top = 10
.Left = 20
.Height = 16
.Width = 50
.Font.Size = 12
.Font.Name = "Ariel"
End With
End Sub
Private Sub CommandButton1_Click()
Call Fill
End Sub
WAY 2
Declare a Boolean Variable and create a click event of the Option button and then set the value of the Boolean Variable in that click event. To create the click event of the Option button at Run Time, see THIS EXAMPLE
You can then check the value of Boolean Variable in Sub Fill() and act accordingly.

Type 13 mismatch error in VBA excel

Dear friends I'm trying to display photos of the persons as per the name selected in a combo box.
I'm successful in doing that but my problem is that
while continuously choosing different names in combo box suddenly at times it displays ** error 13, type mismatch** and my combo box too disappearing. But after that making the visibility of Mypics(Name defined to the table of person names and pictures) "TRUE" its appearing again after compiling 2 to 3 times.
here is my code
Private Sub Worksheet_Calculate()
Dim Mypics As Picture
Me.Pictures.Visible = False
With Range("B8")
For Each Mypics In Me.Pictures
If (Mypics.Name = .Text) Then
Mypics.Visible = True
Mypics.Top = .Top
Mypics.Left = .Left
Exit For
End If
Next Mypics
End With
End Sub
The cell "B8" is where the name of the picture appears according to the selected person name in combo box with reference to the Index number.
Often, cleaning up your code can produce wonders. I sincerely suggest avoiding using With if you're just aiming to use it once, as in your original code. How about trying the following:
Private Sub Worksheet_Calculate()
Dim Mypics As Picture
Dim TargetName As String
TargetName = Range("B8").Text
Me.Pictures.Visible = False
For Each Mypics In Me.Pictures
If Mypics.Name = TargetName Then
With Mypics
.Visible = True
.Top = .Top
.Left = .Left
End With
Exit For
End If
Next Mypics
End Sub
Let us know if this works. Also, try to Dim everything you can properly Dim. Often, a type mismatch error is thrown due to a variable being declared wrongly at the beginning of a code.

Excel dropdowns in VBA: "Unable to get the DropDowns property of the Worksheet class"

I have this code:
Sub addDropdown(Name)
ActiveSheet.DropDowns.Add(74.25, 60, 188.25, 87.75).Select
Set n = ActiveSheet.DropDowns(Name)
If Not (n Is Nothing) Then
ActiveSheet.DropDowns(Name).Delete
End If
With Selection
.ListFillRange = "$K$15:$M$19"
.LinkedCell = "$K$8:$L$11"
.DropDownLines = 6
.Display3DShading = False
.Name = Name
End With
ActiveSheet.DropDowns(Name).Display3DShading = True
End Sub
Which results in "Runtime error 1004: Unable to get the DropDowns property of the Worksheet class"
I am a VBA noob, so why is it refering to a property? According to the Object Browser DropDowns is a function (although that doesnt rime with the .Add later on).
Also, I can access this exact thing later on after having added something to DropDowns. I just dont get it.
What I want to do, is to delete any pre-existing dropdown with the same name.
You need to handle the error if the named Dropdown does not exist
on error resume next
Set n = ActiveSheet.DropDowns(Name)
on error goto 0