In the following code I am trying to look for a text box in an open word document. Once that text box has been found I would also like to check to make sure the correct text is inside. This is the code that does not work and I could not figure out why.
Dim i as Integer
for i = 1 to Application.ActiveDocument.Shapes.Count
if Application.ActiveDocument.Shapes(i).Name = "Text Box 2" and _
instr(Application.ActiveDocument.Shapes(i).TextFrame.TextRange.Text, "[Grab your reader") then
` Execute
end if
next i
Now if I replace the and with another if statement it works.
Dim i as Integer
for i = 1 to Application.ActiveDocument.Shapes.Count
if Application.ActiveDocument.Shapes(i).Name = "Text Box 2" then
if instr(Application.ActiveDocument.Shapes(i).TextFrame.TextRange.Text, "[Grab your reader") then
` Execute
end if
end if
next i
From what I understand the and should work exactly the same as adding a new if statement. If anyone could tell me why this happens that would be great.
Related
I'm so close to getting this code working, I just need a little push please. I would like to
take the name of a combo box and then add a string to the end, But then get the value of a textbox with that string. This is to create a dynamic function instead of pasting the same code over and over.
Here's what I have so far, after you select something in the dropdown, the data is then pulled to populate the boxes next to it. I have about 8 drop downs so far so that's why I need this to work.
'Combobox after update
Call GrabData(Me, Me.ActiveControl)
Then
Private Sub GrabData(ctl As Control)
'name of ctl/combobox is "Kitchen"
data1 = (ctl.Name & "Size") '"KitchenSize"
'Here is where it all goes wrong
data1.Value = size.value
'size.value is just a textbox for example
End Sub
I can debug this with:
msgbox(data1)
'outputs "KitchenSize"
But I cannot get the value of kitchensize's textbox with data1.value
Error:
Object Required
I have also added Dim As String / Dim As Control.
I will be assigning the variable to some other stuff in this 50 line code I wrote so please don't take the above example as exactly what I intend to do, I just need help appending the ctl.name to a string, then use that to reference another control and so on.
EDIT
For anyone who wants to know, I figured it out.
Dim Ctrl As Control
Dim CtrlName As String
CtrlName = ctl.Name & "Size"
Set Ctrl = Me.Controls(CtrlName)
Ctrl.Value = 'Wherever you want to send the values to
See the edit.
You need to dim it as a string, then use Set Ctrl
I simply want to underline some text in a worksheet textbox using VBA, from a specific character to another. It should be extremely simple, and I can do it without problem with Bold and Italic.
I have the following sub
Sub ew()
Dim txt1 As Shape
Set txt1 = Sheet1.Shapes("txt_1")
txt1.TextFrame.Characters.Text = "Bold and Underline this"
txt1.TextFrame.Characters.Font.Bold = True
txt1.TextFrame.Characters.Font.Italic = True
txt1.TextFrame.Characters.Font.Underline = True
End Sub
The code fails on the last line, which is extremely strange because it worked for the 2 previous lines. the error (1004) says something like "Impossible to define the Underline function of the Font property".
To recreate the problem, take my sub to a new Excel document and create a textbox named "txt_1", that's all you need to run it.
If anyone has any idea why it fails, please help!
You need to define the Underline style. Taking your last line
txt1.TextFrame.Characters.Font.Underline = xlUnderlineStyleSingle
Use TextFrame2 for underline
txt1.TextFrame2.TextRange.Font.UnderlineStyle = msoUnderlineSingleLine
I am creating a form in Access which will be used as an order sheet for classroom materials. I have the available resources listed and a text box next to the resource where the user inputs the quantity they desire.
My VBA code checks to see if any entries have been made by using the following. (I am using Nz() to allow for Null results):
QuantCheck = Nz(Box1.Value, 0) + Nz(Box2.Value, 0) + Nz(Box3.Value, 0)
Where "QuantCheck" is the variable I am using in the IF statement which begins the workflow:
If QuantCheck > 0 Then
I would like to clean this up by using some kind of loop statement, however I am not able to extract the .value from a string. I would love something like the following which I could incorporate into a loop:
"Box"&VariableNumber.Value
From what I can tell, I am not able to use a string (concatenated or otherwise) as the base for the .value call.
It is interesting that there is a way to accomplish this when using a SQL statement. I have this elsewhere in the code which works nicely:
SQLStr = "INSERT INTO OrderRequests VALUES (cbSchool, txtName, Title" & x & ".caption, Box" & x & ")"
Here I have a variable "x" which increases with each loop to change the Title line, and the Box line.
Any help is appreciated.
I suggest you use the Tag property of the controls. Put "QuantCheck" in the Tag property of any control you want to include. Then
Function QuantitiesExist(frm As Form) As Boolean
Dim Ctrl As Control
Const sQUANTCHK As String = "QuantCheck"
For Each Ctrl In frm.Controls
If Ctrl.Tag = sQUANTCHK Then
If Nz(Ctrl.Value) > 0 Then
QuantitiesExist = True
Exit For
End If
End If
Next Ctrl
End Function
Now you get self documenting code
If QuantitiesExist(Me) Then
And when you add/delete/change controls, you don't have to edit your code. Just set up new controls with the proper tags.
You could loop through the control on the for checking the names and then if it is the one you wanted take an action on it, is this what you was thinking of?
Dim Ctrl As Control
For Each Ctrl In Me.Controls
If Ctrl.Name = "TxtPath" Then ' "Box" & VariableNumber Then
MsgBox Ctrl.Value
End If
Next
Im fairly new at visual basic and im having trouble using function. ive tried many ways but failed, so ive deleted the funtion to start again. i want this function to run once someone has clicked a button, however the button is on a different form.
When button(btnAdd) is clicked on form 2 i want this to run on form one...
Using writer As System.IO.StreamWriter = New System.IO.StreamWriter(filepath", True)
Dim recipient As String = tbRecipient.Text
If (tbRecipient.Lines.Count > 1) Then
recipient = ""
For Each line As String In tbRecipient.Lines
recipient = recipient & " " & line
Next
recipient = recipient.Trim()
End If
writer.WriteLine(recipient)
End Using
Im not sure if this is the right code to achieve what i want it to do. What the code should do is when the user clicks the button add, it reads the checked options in a checklistbox and adds them to a file. that file is the outputted to a different textbox, which is on a different form. I have the funtion working correctly for the adding of the checkboxlist but need to then display it in a text box on another form. if anyone can help or point me in the right direction that would be great.
Double click on the button(btnAdd) in design window then write:
myFunction()
then in the second form code window:
public function myFunction()
'your code
end function
just call the function name where you want to execute it, or using the reserved keyword Call as Below:
First Way:
'Call by taping the function name
myFunctionName(Argumets)
Second Way:
'Use the Call Keyword:
Call myFunctionName(Argumets)
Self taught(in progress) Visual Basic guy here.
I've searched for a clear answer on this, but so far have come up empty handed.
The problem...
I have two comboboxes. The first combobox has 10 options, second combobox has 2 options
I have 10 textboxes, with a name that includes one of the 10 options.
ex 1st textbox name - "txb_Option1Type"
2nd textbox name - "txb_Option2Type" and so on.
I have 2 tabs, with the first 5 text boxes on the 1st tab and last 5 on the 2nd tab.
I thought the following bit of code, upon a button click, would transfer the text of the chosen option in the 2nd combobox to the corresponding textbox...
`
Public Sub TransferTruckToDoorText()
Dim str_ErrorButton As String = cbx_DoorNumber.Text
Dim str_ReplaceSpacesButton As String = str_ErrorButton.Replace(" ", "")
Dim str_Button As String = str_ReplaceSpacesButton
' Null reference error on below line of code
Me.Controls("txb_" & str_Button & "Type").Text = cbx_TruckType.Text
End Sub
`
As noted in the above code, I'm getting a null reference and for the life of me cannot figure out why. I've stepped through the code, and I'm not able to find a NULL or Nothing value that could be making this catch.
Any and all help would be appreciated.
edited for clarity
The Me.Controls collection does not automatically search the child panels.
Try using the Controls.Find method for that, which includes a parameter to search the child control's control collection, too. It returns an array:
Dim c As Control() = Me.Controls.Find("txb_" & str_Button & "Type", True)
If c.Length = 1 Then
c(0).Text = cbx_TruckType.Text
End If
Me.Controls.Item("txb_" & str_Button & "Type")