I have opened an Excel file via VBA Access and can read and write in the Excel Cells. How can I tick an Excel Ckeck Box?
My code:
Dim Excel_App As Object
Dim strExcel As String
Set Excel_App = CreateObject("Excel.Application")
Excel_App.Visible = True
Excel_App.Workbooks.Open fready
With Excel_App
.CheckBox3.Value = True 'This line is used in VBA Excel and I need in Access
End With
Checkboxes belong to a specific collection on a specific Worksheet. Which collection this is, depends on the type of control you're looking for.
Your code is written as if the checkbox belonged to the Excel.Application object - that can't work.
First you need to keep a reference to that Workbook object you're opening, so instead of this:
Excel_App.Workbooks.Open fready
You need this:
Dim book As Object ' early-bound: As Excel.Workbook
Set book = Excel_App.Workbooks.Open(fready)
If you don't know which worksheet to find the checkbox on, you'll have to iterate the workbook's Worksheets collection:
Dim sheet As Object ' early-bound: As Excel.Worksheet
For Each sheet In book.Worksheets
'todo
Next
Form Controls
So we're looking for a CheckBox form control. We'll find that in the sheet's Shapes collection, and we'll know we're looking at a form control when its Type is msoFormControl; we'll know that it's a checkbox control when its FormControlType property returns xlCheckBox:
Dim sheet As Object ' early-bound: As Excel.Worksheet
For Each sheet In book.Worksheets
Dim shp As Object ' early-bound: As Excel.Shape
For Each shp In sheet.Shapes
If shp.Type = 8 ' early-bound: msoFormControl
If shp.FormControlType = 1 ' early-bound: xlCheckBox
'todo
End If
End If
Next
Next
So now we know shp is a checkbox form control. The Value is accessible through the ControlFormat object/property, so you can set the value of a checkbox named Check Box 1 (that's the default name) like this:
If shp.Name = "Check Box 1" Then
shp.ControlFormat.Value = 1 'checked
End If
Of course if you already know which specific sheet you're looking for, there's no need to iterate them all.
ActiveX Controls
If the control is an ActiveX control, it's a whole different story; you'll find it in the OLEObjects collection instead, which contains OLEObject instances, which have an Object property that returns the MSForms.CheckBox object; you can get the name of the checkbox from the OLEObject.ShapeRange.Name:
Dim ctrl As Object ' early-bound: As Excel.OLEObject
For Each ctrl In sheet.OLEObjects
If TypeName(ctrl.Object) = "CheckBox" Then ' early-bound: If TypeOf ctrl.Object Is MSForms.CheckBox Then
If ctrl.ShapeRange.Name = "CheckBox1" Then
ctrl.Object.Value = True ' checked
End If
End If
Next
Note that the early-bound TypeOf ctrl.Object Is MSForms.CheckBox check is MUCH more robust than the late-bound TypeName check. You need to reference the MSForms type library through Tools > References to use it (it's already referenced if your VBA project has any UserForm component, in which case early-bound code is a no-brainer).
Related
There is an error in the below outlook macro that copies a link to the currently selected email to the selected cell of the open Excel active sheet.
Error displayed is :
Object doesn't support this property or method and points to the following line:
rngSelection.Value = olItem.GetInspector.URL
This is the microsoft Outlook code so far:
`
Sub CopyLinkToSelectedEmail()
Declare all variables
Dim olItem As Object
Dim xlApp As Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlWorksheet As Excel.Worksheet
Dim rngSelection As Excel.Range
' Get the currently selected item(email) in Outlook
Set olItem = Outlook.Application.ActiveExplorer.Selection(1)
' Check the type of the selected item
Select Case TypeName(olItem)
Case "MailItem"
' Get the active Excel workbook and worksheet
Set xlApp = GetObject(, "Excel.Application")
Set xlWorkbook = xlApp.ActiveWorkbook
Set xlWorksheet = xlWorkbook.ActiveSheet
' Get the currently selected cell in the excel worksheet
Set rngSelection = xlApp.Selection
' Check the type of the selected cell
If TypeName(rngSelection) = "Range" Then
' Copy the link to the email to the selected worksheet cell
rngSelection.Value = olItem.GetInspector.URL
Else
' Display a message if the selected cell is not a valid range
MsgBox "A valid cell must be selected in the Excel worksheet to run this code.", vbOKOnly + vbExclamation
End If
Case Else
' Display a message and exit the macro if the selected item is not a mail item
MsgBox "An email mail item must be selected to run this macro.", vbOKOnly + vbExclamation
Exit Sub
End Select`
End Sub`
In the following line of code:
rngSelection.Value = olItem.GetInspector.URL
The Inspector class doesn't provide the URL property. If you need to uniquely identify items in Outlook you may get the Outlook item instance by using the Inspector.CurrentItem property which returns an object representing the current item being displayed in the inspector.
Dim myItem As Object
Set myItem = Application.ActiveInspector.CurrentItem
Then you can use properties and methods to identify items. From the first sight, you may consider using the EntryID property if you deal with a single account and only on your machine. But a better approach is to introduce your own ID which can be stored in a user property, see PropertyAccessor which allows getting and setting item-level properties that are not explicitly exposed in the Outlook object model
I have placed a TextBox control on a sheet in Excel, in a VBA module I want to acces the TextBox and populate with content.
How do I reference the TextBox control?
I've named the TextBox 'tbSQL', in the module I can see Application and can refernece the sheet from the module, the sheet is called 'Database Info.'
I want to write data into the TextBox from the VBA module, but so far I haven't been able to reference it.
I've tried:
Public Const DATABASE_INFO As String = "Database Info."
Dim objDBsheet As Worksheet, objSQL As Range
Dim tbSQL As TextBox
Set objDBsheet = Application.Sheets(DATABASE_INFO)
Set tbSQL = objDBsheet.Shapes("tbSQL")
But this is as far as I get it errors on the Set tbSQL line. The reported error is "Type Mismatch"
I know the control is a TextBox, it was created from the Controlbox toolbar. When looking in the range bar in Excel it displays:
"=EMBED("Forms.TextBox.1","")"
In the Properties box for the TextBox control I have set the (Name) property to tbSQL, however it remains unchanged in the fx text box. It does show as tbSQL in the range box.
Most likely its a bug in Excel. It has nothing to do with Excel-2003.
If you use a Worksheet type variable then Excel fails to discover the control on that sheet in VBA. So if you declare your sheet holding variable as Object / Variant , the code will work fine.
Other alternative is to directly use the Worksheet's CodeName, so if you set the Worksheet's name as wksDBSheet in the VBA IDE's property grid and use that in your code, it will discover the TextBox
Sub test()
Dim objDBsheet As Object 'As Worksheet // Making the 0bjDBSheet type as Object or Variant
'// Allows the discovery of the TextBox on the sheet.
'// Most Likely its a bug.
Dim objSQL As Range
Dim tbSQL As MSForms.TextBox
Set objDBsheet = Application.Worksheets("Database Info.")
Set tbSQL = objDBsheet.tbSQL
tbSQL.Text = "Bug"
'/ Other Alternative is to directly use the CddeName of the sheet.
Set tbSQL = wksDBsheet.tbSQL
tbSQL.Text = "Code Name used"
End Sub
Text Box as shape, or Label as Form Control, or ActiveX control:
Sub f()
Dim tb As Shape, lblControl As Object, lblActiveX As Object
Set tb = Sheet1.Shapes("TextBox 1")
Set lblControl = Sheet1.Shapes("Label 2").OLEFormat.Object
Set lblActiveX = Sheet1.Shapes("Label1").OLEFormat.Object
lblControl.Text = "Form Control"
lblActiveX.Object.Caption = "ActiveX Control"
tb.TextFrame.Characters.Text = "Text Box"
End Sub
You need to use:
Sheets("Sheetname").Shapes("tbSQL").TextFrame.Characters.Text = "Anything you want"
In the end it looks like it is a bug in Excel 2003, I was able to reference the control by using Sheet1.tbSQL instead of going through Application.Sheets.
If you put an ActiveX texbox on a worksheet named "MyTextBox" then you can set it's text in vba like this:
ActiveSheet.MyTextBox.Text = "hi"
I want to programatically add items to an ActiveX ComboBox.
How do I reference the ComboBox to perform a .additem to it.
I tried:
Dim lstSubsB As ComboBox
Set lstSubsB = ws.ListObjects("lstSubs") ' - << Error occurs here
but it gives a 'Subscript out of Range' error. The combobox, is named 'lstSubs'.
In addition to the solutions provided, I found that the below also works.
Set lstSubsB = Me.lstSubs
With lstSubsB
For x = arrLower To arrUpper
lstSubs.AddItem arrSubs(x)
Next x
End With
Dim objX As Object
Set objX = ws.OLEObjects("lstSubs")
I know this is an old topic, but I just figured it out and thought I'd save some time for the next person. The property you want is "Object", which in this case is a ComboBox type. I'm sure Object type works as well, but strong typing helps with code complete.
' This snippet adds sheet names to a combobox
Dim lstSubsB as ComboBox
Dim Sht As Variant
' The Object property in this case returns a typed ComboBox
Set lstSubsB = ws.OLEObjects("lstSubs").Object
lstSubsB.Clear
For Each Sht In Worksheets
lstSubsB.AddItem Sht.Name
Next
I'm attempting to copy the contents of a text box from one workbook to another. I have no problem copying cell values from the first workbook to the 2nd, but I get an object required error when I attempt to copy the text box. This macro is being run from the workbook containing the data I want copied. Using Excel 2007 Code:
Sub UploadData()
Dim xlo As New Excel.Application
Dim xlw As New Excel.Workbook
Set xlw = xlo.Workbooks.Open("c:\myworkbook.xlsx")
xlo.Worksheets(1).Cells(2, 1) = Range("d4").Value 'Copy cell content (this works fine)
xlo.Worksheets(1).Cells(2, 2) = TextBox1.Text 'This gives me the object required error
xlw.Save
xlw.Close
Set xlo = Nothing
Set xlw = Nothing
End Sub
Thanks for any help.
The problem with your macro is that once you have opened your destination Workbook (xlw in your code sample), it is set as the ActiveWorkbook object and you get an error because TextBox1 doesn't exist in that specific Workbook. To resolve this issue, you could define a reference object to your actual Workbook before opening the other one.
Sub UploadData()
Dim xlo As New Excel.Application
Dim xlw As New Excel.Workbook
Dim myWb as Excel.Workbook
Set myWb = ActiveWorkbook
Set xlw = xlo.Workbooks.Open("c:\myworkbook.xlsx")
xlo.Worksheets(1).Cells(2, 1) = myWb.ActiveSheet.Range("d4").Value
xlo.Worksheets(1).Cells(2, 2) = myWb.ActiveSheet.TextBox1.Text
xlw.Save
xlw.Close
Set xlo = Nothing
Set xlw = Nothing
End Sub
If you prefer, you could also use myWb.Activate to put back your main Workbook as active. It will also work if you do it with a Worksheet object. Using one or another mostly depends on what you want to do (if there are multiple sheets, etc.).
I think the reason that this is happening could be because TextBox1 is scoping to the VBA module and its associated sheet, while Range is scoping to the "Active Sheet".
EDIT
It looks like you may be able to use the GetObject function to pull the textbox from the workbook.
The issue is with this line
xlo.Worksheets(1).Cells(2, 2) = TextBox1.Text
You have the textbox defined at some other location which you are not using here. Excel is unable to find the textbox object in the current sheet while this textbox was defined in xlw.
Hence replace this with
xlo.Worksheets(1).Cells(2, 2) = worksheets("xlw").TextBox1.Text
I have a piece of script that’s in module1 that checks if an option button is clicked.
The option button is placed in Sheet1, name “Info”, so I thought the script below would work
Sub checkClicked()
dim Page as worksheet
set Page as worksheets(“Info”)
Debug.print Page.optClicked
End sub
But when I did it like this it says method or data member not found. It would only work if I replace it with
…
Debug.print Sheet1.optClicked
…
Can anyone give me an insight why this happens?
Think of Sheet1 as a "subclass" of "worksheet" - when you add controls to the sheet you're adding new members. A generic worksheet object doesn't have a member which represents your option button, whereas Sheet1 does.
Sub Test()
Dim sht As Worksheet
Dim sht1 As Sheet1
Set sht = ThisWorkbook.Sheets("Sheet1")
Set sht1 = Sheet1
Debug.Print sht.optClicked 'error
Debug.Print sht1.optClicked 'OK
End Sub
Set Page = ActiveWorkbook.Worksheets("Info") should work. I think worksheets is no real property in VBA...
Also, your debug print code looks weird, use debug.print("bla")..
Do you have Option explicit activated?
Try Set Page = Worksheets("Info") and do NOT use these curly “” quotes - just in case (for Excel formulas this DOES matter).
The argument within Worksheets is the name of the worksheet you are interested in, i.e. "Sheet1".
Other approach: the ActiveX controls on sheet are accessible from two collections: Shapes and OLEObjects. You could use the OLEObjects collection to get access to your checkbox.
Sub checkClicked()
Dim Page As Worksheet
Set Page = Worksheets("Info")
' 1/ ActiveX check box in Shapes collection
Dim myShape As Shape
Set myShape = Page.Shapes("optClicked")
' --------------------------------------
' 2/ ActiveX check box in OLEObjects collection
Dim myOLEObject As OLEObject
Set myOLEObject = Page.OLEObjects("optClicked")
' Use Object property to get access to your check box
Dim myCheckBox As Variant
Set myCheckBox = myOLEObject.Object
If (TypeOf myCheckBox Is MSForms.CheckBox) Then
Debug.Print myCheckBox.value
End If
End Sub