Check for an empty Text Box - vba

I have a text box in a Word 2016 document. This is not a TextBox form object but a normal Text Box that you insert from the Insert tab here:
I am trying to check if it is empty when the document is opened. For the life of me I cannot figure out how to do this. I have tried all of the following:
If (Len(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text & vbNullString) = 0) Then
If (IsNull(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text)) Then
If (LTrim(RTrim(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text)) = "") Then
If (ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text = "") Then
None of these return true when the text box is empty? This is the text box:
It seems that the text box always contains a paragraph marker (which I can't delete). This is what the VBA watch shows:
Watch : : ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text : "
" : String : ThisDocument.Document_Open
Note that the watch is two separate lines which makes me think that there's CRLF or something in there?

There is always a paragraph break in an otherwise empty textbox. Accordingly, you could use something along the lines of:
Private Sub Document_Open()
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2")
If Not .TextFrame Is Nothing Then
If Trim(.TextFrame.TextRange.Text) = vbCr Then
MsgBox "Empty Text Range"
End If
Else
MsgBox "No Text Range"
End If
End With
End Sub

Related

VBA - Enter array into multiline textbox on userform

I want to enter each element of an array onto a new line in multiline textbox on a userform. I have the Multiline and EnterKeyBehaviour of the textbox set to True.
The code below will enter the array elements but after entered they are then overwritten by the next element.
For i = LBound(clipArray) To UBound(clipArray)
frmMyForm.txtBox.Text = clipArray(i)
Next
I have tried variations like below (with vbCr, vbCrLf etc) but still does not work.
frmMyForm.txtBox.Text = clipArray(i, vbNewLine)
How can I fix?
Please, use:
frmMyForm.txtBox.Text = Join(clipArray, vbLf)

Want to add the line from the text box to a list box if it contains a word from words

I Want to add the line from the text box to a list box if it contains a word from words.
This code introduces my result into the textbox, I want it in a textbox line or in a listbox line.
So, TempTextBox will have to be a textbox line or a listbox line so I will not create too many textboxes. How can I improve the code?
If TextBox1.Lines(i).Contains(word) Then
found = True
Dim tempTextBox As TextBox = CType(Me.Controls("CheckVar" & i.ToString), TextBox)
On Error Resume Next
If TextBox2.Lines(i).Contains(word) Then
If tempTextBox.Text.Contains(word) Then
Else
tempTextBox.Text = tempTextBox.Text + " " + TxtbValAfterCompar.Text()
End If
Else
End If
End If

How to get Selection.Text to read displaytext of Macro field code

I'm having some trouble figuring this out, and would really appreciate some help. I'm trying to write a macro that uses the selection.text property as a Case text-expression. When the macro is clicked in Microsoft Word, the selected text is automatically set to the DisplayText. This method worked great for the formatting via Selection.Font.Color for a quick and dirty formatting toggling macro, but it doesn't work for the actual text.
When debugging with MsgBox, it is showing a box (Eg: □ ) as the value.
For example,
Word Field Code:
{ MACROBUTTON Macro_name DisplayText }
VBA Code run when highlighting "DisplayText" in Word:
Sub Macro_name()
Dim Str As String
Str = Selection.Text
MsgBox Str
Select Case Str
Case "DisplayText"
MsgBox "A was selected"
Case "B"
MsgBox "B was selected"
End Select
End Sub
What is output is a Message Box that only shows □
When I run this macro with some regular text selected, it works just fine.
My question is this: Is there a way to have the macro read the displaytext part of the field code for use in the macro?
You can read the field code, directly, instead of the selection (or the Field.Result which also doesn't give the text).
It's not quite clear how this macro is to be used throughout the document, so the code sample below provides two variations.
Both check whether the selection contains fields and if so, whether the (first) field is a MacroButton field. The field code is then tested.
In the variation that's commented out (the simpler one) the code then simply checks whether the MacroButton display text is present in the field code. If it is, that text is assigned to the string variable being tested by the Select statement.
If this is insufficient because the display text is "unknown" (more than one MacroButton field, perhaps) then it's necessary to locate the part of the field code that contains the display text. In this case, the function InstrRev locates the end point of the combined field name and macro name, plus the intervening spaces, in the entire field code, searching from the end of the string. After that, the Mid function extracts the display text and assigns it to the string variable tested by the Select statement.
In both variations, if the selection does not contain a MacroButton field then the selected test is assigned to the string variable for the Select statement.
(Note that for my tests I needed to use Case Else in the Select statement. You probably want to change that back to Case "B"...)
Sub Display_Field_DisplayText()
Dim Str As String, strDisplayText As String
Dim textLoc As Long
Dim strFieldText As String, strMacroName As String
Dim strFieldName As String, strFieldCode As String
strDisplayText = "text to display"
If Selection.Fields.Count > 0 Then
If Selection.Fields(1).Type = wdFieldMacroButton Then
strFieldName = "MacroButton "
strMacroName = "Display_Field_DisplayText "
strFieldCode = strFieldName & strMacroName
Str = Selection.Fields(1).code.text
textLoc = InStrRev(Str, strFieldCode)
strFieldText = Mid(Str, textLoc + Len(strFieldCode))
MsgBox strFieldText
Str = strFieldText
'If InStr(Selection.Fields(1).code.text, strDisplayText) > 0 Then
' Str = strDisplayText
'End If
End If
Else
Str = Selection.text
End If
Select Case Str
Case strDisplayText
MsgBox "A was selected"
Case Else
MsgBox "B was selected"
End Select
End Sub

Select combobox if wrong item selected

I have an MS-Word 2013 document with several (legacy) formfields; some text boxes, some comboboxes. The first list item on all of the comboboxes is "(select one)" to let the end user know they need to make a selection (I did not draft or design this document, I've just been asked to write the VBA code for it). So I coded each to give a simple VBA message box, ran on exit, if that first selection was not changed, for example:
Public factor1 As Integer
Sub MyFormFieldFactor1()
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
Exit Sub
End If
If ActiveDocument.FormFields("cbofactor1").Result = "Yes" Then
factor1 = 1
Else
factor1 = 0
End If
End Sub
The word document automatically goes to the next formfield when you click ok on the message box. Through VBA, I want it to stay on the current formfield when "(select one)" is chosen. Bonus points if it stays on the current field and pulls up the list selection automatically.
Will this work:
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
ActiveDocument.FormFields("cbofactor1").SetFocus()
Exit Sub
End If
You can auto drop the list with something like:
SendKeys "%{down}", True
DoEvents
Full code:
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
ActiveDocument.FormFields("cbofactor1").SetFocus()
SendKeys "%{down}", True
DoEvents
Exit Sub
End If

Delete Selected Text in a Text Box

Hey fellow Stackoverflow users,
i'm trying to create a text editor with HTML tag functions (for export) and can't get a solution working. Therefor i created a textbox where the user should be able to insert the text. For this insert function i need the in the title described function to delete the selected text (only the selected text, the text around it stays) inside the textbox. Even SendKeys won't function right.
Please let me know if anyone got an idea, thanks upfront!
EDIT: Here's the corrected code for the bold button with maintextbox as textbox for the user's text:
Private Sub BoldButton_Click()
If maintextbox.SelLength = 0 Then
MsgBox ("Please highlight the text you want to edit!")
Else
SelectionText = maintextbox.SelText MsgBox ("SelText: " & SelectionText)
maintextbox.SelText = "<b>" & _
SelectionText & _
"</b>"
End If
End Sub
Simply:
TextBox.SelText = ""
The .Sel* methods all relate to the text currently selected within the control. SelText = "" replaces the current selection with nothing, thereby deleting it whilst preserving the surrounding unselected text.