Select text w/o the paragraph mark - vba

I have a problem when assigning text in a Word document (usually one line) to a variable. The problem is that the paragraph mark also gets assigned to the variable and that causes the external system to reject the variable.
Since it's just one line, I do:
Selection.WholeStory
var = Selection.Text
In the external system, when assigning this var to a text box, the paragraph mark gets inserted and then processed as #.
How can I assign the text below to a variable w/o the paragraph mark:
This is a sample text with paragraph mark^p

You can just remove the vbCr:
Selection.WholeStory
var = Selection.Text
var = Replace(var, vbCr, "")

Related

How to search only the first line of a multiline textbox in VB.NET

Is there any way to search only the first line of a Multiline Textbox without knowing exactly at what position the text is you're looking for?
If I knew the position of the text I was looking for I could do something like:
Dim myNotes As String = "The book has a lot of text"
Dim myText As String = "text"
If Not myNotes.Substring(0,4) = myText Then
' Do Something
End If
Or if I wanted to search the entire textbox I could do something like:
Dim myNotes As String = "The book has a lot of text"
Dim myText As String = "text"
If Not myNotes.Contains(myText) Then
' Do Something
End If
But I want to search only the first line of the textbox and I'm not sure at what position the text may be. Is there anyway to do a search like that?
This is another example of why you should ALWAYS read the relevant documentation. If you had read the documentation for the TextBox class then you'd know that it has a Lines property. To get the first line of text, you simply get the first element of that array:
Dim firstLine = myTextBox.Lines(0)
If Not filrstLine.Contains(myText) Then
'Do something
End If
Note that this only applies where the user has explicitly added a line break to the text. I assume that that is what you want, given that you have accepted another answer that does the same thing. If you mean the first line based on automatic word-wrap then that requires a bit more effort.
You could take the text and extract the first line.
int pos = text.IndexOfAny('\r', '\n');
if (pos >= 0)
text = text.SubString(0, pos);
// text now contains only the first line
Then you can search the resulting string.

How to insert text into a richtext textbox using VBA code

In MS Access VBA, I have been trying to programmatically insert code into a form's richtext textbox control when the user presses a button. The idea is to put a mark where the user's cursor is at the time the user presses the button--the mark will signify the start of text where the user enters a comment about the text.
However, presumably because the richtext textbox has hidden formatting codes embedded (e.g., <div>, etc.), using .SelStart and .SelLength does not seem to get me to the correct position in the textbox when I am trying to insert the new text. It is consistently inserting the text earlier in the textbox than where the cursor was when the button is clicked, but not a consistent number of characters earlier.
Although I've done a search and found some wonderful functions for inserting text into a standard textbox (e.g., Lebans' InsertAtCursor function), I cannot get those functions to work for richtext textboxes either--that is, they have the same problem as code that I wrote myself; it inserts the new text too early in the existing textbox text.
Anyone have a solution for programmatically inserting new text into a richtext textbox at the cursor position?
Here is code (obviously, I could make the code more efficient, but I was just trying to get something working first) from one of my attempts. It inserts text, but, not at the correct location, presumably due to the richtext formatting that does not visibly appear in the textbox but apparently influences .SelStart position values:
Dim intSelStart As Integer 'this is the starting location of the selection in the note at the time the comment was initially added
Dim strAddComment as String 'this is the string comment that I want to add--it is not the comment itself, it is a flag that will indicate the comment number
strAddComment = "|1`17|" 'the | characters delimit the comment flag; the first number is the comment number so 1 is the first comment, 2 is the second, etc.; the value after the ` is the length of the text selected in the textbox to which the comment applies, e.g., `17 means the comment applies to 17 selected characters
Forms!frmAppt_individual.SetFocus 'set the focus to the main form
Forms!frmAppt_individual.sub_C.SetFocus 'set the focus to the subform so we can get the .Sel property values of the text selected in the textbox on the subform
Forms!frmAppt_individual.sub_C.Form.Controls("Note").SetFocus 'set focus on the control which is required to get the .Sel property values
intSelStart = Forms!frmAppt_individual.sub_C.Form.Controls("Note").SelStart
'now try to insert the comment
Forms!frmAppt_individual.sub_C.Form.Controls("Note") = Left(Forms!frmAppt_individual.sub_C.Form.Controls("Note"), intSelStart) & strAddComment & Mid(Forms!frmAppt_individual.sub_C.Form.Controls("Note"), intSelStart + 1)
So, I am posting code below that worked for me to insert text at the current insertion point into a richtext textbox using VBA code. The use of the PlainText function seemed helpful. Hopefully the code below will help someone else trying to do the same thing.
An explanation of the code:
The function fAddComment is contained in a standard module. fAddComment is called when the user clicks a button btnAddComment on the parent form frmAppt. A subform subNoteForm contains the richtext textbox named tbxNote. The user is expected to have the insertion point at the position in the richtext textbox where the comment reference is inserted; the user may also have a number of characters selected that indicate the range of text to which the comment reference applies. The function fAddComment will determine what comment number the new comment reference will be by counting any existing comment references already in the note. The function will also count the number of characters selected when the comment reference is inserted so that a different function can later locate the comment reference and select the relevant characters. The function adds, at the insertion point, a comment emoji (💬), followed by the comment reference number, followed by a ` character, followed by the number of characters selected when the comment reference was added, ending with a terminating comment emoji. The function returns the string of the comment reference block that was added.
Public Function fAddComment() As String
'this is used to add a new comment reference to the currently showing note
'it returns "" if there is no note
'it returns the string of the comment reference added to the note if a comment reference is successfully added
Dim strBubble As String 'the comment bubble emoji string
strBubble = ChrW(55357) & ChrW(56492) 'the comment bubble emoji
Dim strProgNote As String
Dim intMaxComment As Integer 'this is the maximum comment number found
Dim intSelStart As Integer 'this is the starting location of the selection in the note at the time the comment was initially added
Dim intSelLength As Integer 'this is the selection length in the note at the time the comment was initially added
Dim whr As Integer 'this locates the next comment emoji and the emoji after it
Dim intFoundValue As Integer 'this is the value of the found comment number
Dim strNew As String 'the new string that holds the comment number and length
Dim strSelectedText As String 'the text that is selected
Dim intPosBeforeInsert As Integer 'the position of the insertion point prior to inserting the new comment reference
'first, get the text of the progress note
strProgNote = PlainText(Forms!frmAppt.subNoteForm.Form.Controls("tbxNote"))
If Len(strProgNote) = 0 Then 'there's no note, so don't try to find comments
fAddComment = ""
Exit Function
End If
'get the selection length at the time the comment was initially added
Forms!frmAppt.SetFocus 'set the focus to the main form
Forms!frmAppt.subNoteForm.SetFocus 'set the focus to the subform so we can get the length of the text selected in the textbox on the subform
Forms!frmAppt.subNoteForm.Form.Controls("tbxNote").SetFocus 'set focus on the control which is required to get the SelLength property
intSelStart = Forms!frmAppt.subNoteForm.Form.Controls("tbxNote").SelStart
intSelLength = Forms!frmAppt.subNoteForm.Form.Controls("tbxNote").SelLength
strSelectedText = PlainText(Forms!frmAppt.subNoteForm.Form.Controls("tbxNote").SelText)
'now, find each comment emoji string and get the value
intMaxComment = 0 'default to no comments
whr = 1
Do Until whr = 0
whr = InStr(whr, strProgNote, strBubble)
If whr > 0 Then 'found a comment, check the number
intFoundValue = Val(Mid(strProgNote, whr + 2)) 'the comment emoji consists of 2 characters, not just 1 character
If intFoundValue > intMaxComment Then intMaxComment = intFoundValue 'the new value is greater so make it the highest value now
whr = InStr(whr + 2, strProgNote, strBubble) + 2
End If
Loop
'return the next comment number and the length of the selected string in the note
strNew = strBubble & Trim(str(intMaxComment + 1)) & "`" & Trim(str(intSelLength)) & strBubble
'insert the new comment reference into the note
intPosBeforeInsert = Forms!frmAppt.subNoteForm.Form.Controls("tbxNote").SelStart
Forms!frmAppt.subNoteForm.Form.Controls("tbxNote").SelLength = 0 'collapse selection
Forms!frmAppt.subNoteForm.Form.Controls("tbxNote").SelText = strNew 'insert the new comment text
'move the insertion point back to the original location and after the comment reference we just added
Forms!frmAppt.subNoteForm.Form.Controls("tbxNote").SelStart = intPosBeforeInsert + Len(strNew)
fAddComment = strNew 'return the comment string we just added to the note
End Function

VBA Word - eliciting properties of a range-object in a text containing revision marks

A text
containins a string of hidden text
contains revision marks while deleted revisions are formatted as hidden.
i.e. deleted (hidden) textvisible text
The visible text has been selected (not knowing that it is preceded by hidden text) and the selection is copied to a range.
Dim R_Visible as range
set R_Visible = selection.range
If I want to determine some property, i.e. the character style, VBA will elicit the style property of the whole string "deleted (hidden) textvisible text", not only of "visible text". Thus, if the character style of the deleted part differs, from the character style of the visible part, VBA will return the value of selection.ParagraphFormat.style.
To make things more complicated I want to elicit the character style of only the 1st character of the selected text. Here are some possible approaches and their results:
a)
Print R_Visible.characters(1).CharacterStyle ==> Runtime error 91
b)
Print R_Visible.characters(1).Style ==> paragraph style
As a work-around I have written the following function:
Function ExactRange(Optional R_Range) As Range
Dim R_Selected As Range
If IsMissing(R_Range) Then
Set ExactRange = Selection.Range
Else
'set ExactRange = R_Range.duplicate '(I haven't tested thoroughly whether that works under all circumstances)
Set R_Selected = Selection.Range
R_Range.Select
Set ExactRange = Selection.Range
R_Selected.Select 're-establishing the original selection-object
End If
ExactRange.SetRange Start:=ExactRange.Characters(1).End - 1, _
End:=ExactRange.Characters(Len(ExactRange)).End
End Function
which produces some more results:
c)
print Mtf_ExactRange(R_Visible).style==> character style
d)
print MTF_ExactRange(R_Visible).Characters(1).Style ==> paragraph style
e)
print MTF_ExactRange(R_Visible.characters(1)).Style== character style
It seems to me that the behaviour is quite erratic. Can someone explain to me what is happening here and maybe point out an intended VBA-method to elicit a character style under the given circumstances without the need of a work-around function?
Selection.Style will only return one style, even if more than one has been applied. A selection can have up to 4 styles.
To find the character style of selected text, use:
Selection.Range.CharacterStyle
To get the paragraph style:
Selection.Range.ParagraphStyle
To get the table style:
Selection.Range.TableStyle
To get the list (bullets or numbering) style:
Selection.Range.ListStyle

I printed text that contains multiple "\"'s but "\S" turned into a "1"

So I have a C1TrueDBGrid on my form (which is a ComponentOne control), and I give the user the option to print the contents of the grid.
When printed, I include a header with some text. This is my code for printing:
Dim dlgPrint As New PrintDialog
dlgPrint.ShowDialog()
dgvList.PrintInfo.PrintEmptyGrid = False
dgvList.PrintInfo.PageHeader = txtDirectory.Text & Environment.NewLine & "Search Term: " & txtSearch.Text & Environment.NewLine
dgvList.PrintInfo.PageSettings.Landscape = True
dgvList.PrintInfo.WrapText = C1.Win.C1TrueDBGrid.PrintInfo.WrapTextEnum.Wrap
dgvList.PrintInfo.RepeatColumnHeaders = True
dgvList.PrintInfo.Print(dlgPrint.PrinterSettings)
dlgPrint.Dispose()
txtDirectory.Text as I'm sure you can imagine contains the path for a directory, which includes back-slashes \ . What actually got printed turned the instances of \S into 1.
For example: txtDirectory.Text = \\Server02\Users\Me\J\Star
page that got printed = \1erver02\Users\Me\J1tar
Is "\S" a printer command for "1" or something? Is there a list somewhere of what all such commands are, if that's the case? Either way, how do I get it to print the actual text?
Thank you!
You are setting that text to a PageHeader, and according to ComponentOne, \S is a special character that returns the total number of sub-pages, or "1" in your example. You will need to double-escape any of the characters in the list on that page.
Updates have been posted to this ComponentOne forum thread.
So what I did was to simply assign the string I want to print to a variable printText and then replace those special characters accordingly:
printText.Replace("\t", "\\t")
printText.Replace("\p", "\\p")
printText.Replace("\P", "\\P")
printText.Replace("\g", "\\g")
printText.Replace("\G", "\\G")
printText.Replace("\s", "\\s")
printText.Replace("\S", "\\S")
Just note that the "\\t" is not yet working like the others...they are looking into it.
Thanks #DonBoitnott for the original link!

How to Find Bullet List and Format Bullets (VBA _ Word Documnet)

I'm trying to find instances of a bullet list in a document then change the bullet type and bullet size.
I was trying to change the following style code so that it would use a bullet type that is formatted to the size and type of bullet i want but I can't seem to figure out the correct object to call or the correct properties to assign.
I have added a custom bullet type and size to the bullet list it is in position nine(9) of the list, if that helps.
I was tried to change the "list Paragraph" to a bullet type but kept keeping an error.
Sub BulletStyle ()
Dim oBull As Word.Paragraph
For Each oBull In ActiveDocument.Paragraphs
If oBull.Range.ListFormat.ListType = _
WdListType.wdListBullet Then
oBull.Style = "List Paragraph"
End If
Next
End Sub
Create a Custom Style that is set to the bullet you want to change it to and make sure format is set to "Numbering" in the style definition.
Then simply change the following line of code
oBull.Style = "List Paragraph"
To the name of your custom style:
oBull.Style = "MyCustomStyle"
When you create your Custom Style make sure to set Format as Numbering.