Insert formatted text in word document with VBA Userform - vba

I have a userform which contains some textboxes and a button.
when button is pressed it insert text at the end of the document. however I need to insert the text as following format:
First line bold - textbox1
Second line is a web link so it should be converted into hyperlink i.e blue, underline - textbox2
Thrid line is simple text not bold. - textbox3 & 4
the following code does insert the text but not formatted as required. how do I insert the text with least code possible?
Private Sub CommandButton2_Click()
Dim CR As Range
Selection.EndKey wdStory
Set CR = Selection.Range
CR.Text = vbCr & TextBox1.Text _
& vbCr & TextBox2.Text _
& vbCr & TextBox3.Text & " at " & TextBox4.Text
End Sub
Possible formatting:

For example:
With ActiveDocument.Range
With .Characters.Last
.Style = wdStyleStrong
.Text = TextBox1.Text & vbCr
End With
.Characters.Last.Font.Reset
.Hyperlinks.Add Anchor:=.Characters.Last, Address:=TextBox2.Text
.Characters.Last.Text = vbCr & TextBox3.Text & " at " & TextBox4.Text
End With

FINAL EDIT - two lines of code to add
If you truly want to make the new code as short as possible
CR.Paragraphs(CR.Paragraphs.Count - 2).Range.Sentences(1).Font.Bold = True
CR.Paragraphs(CR.Paragraphs.Count - 1).Range.Hyperlinks.Add _
CR.Paragraphs(CR.Paragraphs.Count - 1).Range, _
CR.Paragraphs(CR.Paragraphs.Count - 1).Range.Text

Related

Inserting text from VBA UserForm textbox into part of a string

I'm new to VBA and hoping someone could help, if this might even be possible.
A date will be manually added by the user into UserForm TEXTBOX1 which will be placed at a bookmark in the document.
.Bookmarks("BOOKMARK1").Range _
.InsertBefore TEXTBOX1
I have option buttons for the user to select, which will place specific text (depending on the button selected) into the document as follows:
Private Sub OptionButton2_Click()
If Me.OptionButton2.Value = True Then
Set oRng = ActiveDocument.Bookmarks("BOOKMARK2").Range
oRng.Text = "EXAMPLE SENTENCE 1" & Chr(11) & Chr(9) & _
"EXAMPLE SENTENCE 2" & Chr(11) & _
"EXAMPLE SENTENCE 3" & vbNewLine & " "
ActiveDocument.Bookmarks.Add "BOOKMARK2", oRng
End If
End Sub
I am trying to get the date that was entered in TEXTBOX1 to appear at the end of the sentence of EXAMPLE SENTENCE 2 before the & CHR(11) &. Can anybody please help with this? Thank you!
I've tried numerous online searches to find the answer for my problem but haven't come across anything so far unfortunately.
"EXAMPLE SENTENCE 2" & TEXTBOX1.Text & Chr(11)

Insert Word Headings in ListBox including numbers with VBA

Currently I use VBA to create headlines at a certain level. For this I use the following code:
Private Sub InsertButton_Click()
'Insert Title
With Word.Selection
.TypeText Text:=FeatureBox.Text & " " & "-" & " " & ComponentBox.Text & " " & "-" & " " & TitleBox.Text
.Font.size = 12
.Font name = "Calibri"
.Font.Bold = True
.style = wdStyleHeading3
.type paragraph
End With
End Sub
I would like to automatically insert each heading on level 3 into a Multicolumn ListBox. Copying the text is less of a problem, but I would like to insert the automatically generated heading number into the ListBox as well. As an additional function I would like to jump to the element selected in the ListBox by double clicking.
Is this even possible? I would be very grateful for your help.
Many thanks and best regards
Ben

Determining the Number of Lines in a Textbox in VBA

I have a textbox set up in a GUI where the user can enter information. This string is then spit out in a textbox within a PPT slide. Depending on the number of lines used in the textbox within the PPT slide, I need to enter the next set of information so many new lines below the text from the textbox. Here is what I have so far:
This is the code that takes the text the user enters in the textbox within the GUI and places it in the textbox within the PPT slide:
Private Sub Location()
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
'Make sure there is text in the call to action textbox. If not, display an error message.
If C2AText = "" Then
MsgBox "Woah there! You need to enter text in the location/call to action box."
'Otherwise, if text is inserted, place that text in the WarningData box found on the PPT slide.
Else
.TextRange = C2AText
.TextRange.Paragraphs.Font.Size = 21
.TextRange.Paragraphs.Font.Name = "Calibri"
.TextRange.Paragraphs.Font.Shadow.Visible = True
.TextRange.Paragraphs.Font.Bold = msoTrue
End If
End With
End Sub
This text determines whether or not anything is selected in the HailInfo drop down. If it is, I need to place this text so many lines below the C2AText that was inserted in the previous Sub:
Private Sub HailInfo()
Call Dictionary.HailInfo
ComboBoxList = Array(CStr(HailDropDown))
For Each Ky In ComboBoxList
'On Error Resume Next
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
'If nothing is selected in HailDropDown, do nothing and exit this sub.
If HailDropDown = "" Then
Exit Sub
'If a hail option is selected, execute the following code.
ElseIf HailDropDown <> "" And C2AText.LineCount = 2 Then
.TextRange = .TextRange & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & dict2.Item(Ky)(0)
ElseIf HailDropDown <> "" And C2AText.LineCount = 3 Then
.TextRange = .TextRange & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & dict2.Item(Ky)(0)
End If
End With
Next
Set dict2 = Nothing
End Sub
Using the C2AText.LineCount within the HailInfo sub does not appear to do anything. It will not insert the hail text anywhere, so I am not sure what I am doing wrong. Any help would be greatly appreciated...thanks!!
You should try the following ...
Private Sub HailInfo()
Call Dictionary.HailInfo
ComboBoxList = Array(CStr(HailDropDown))
For Each Ky In ComboBoxList
'On Error Resume Next
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
'If nothing is selected in HailDropDown, do nothing and exit this sub.
If HailDropDown = "" Then
Exit Sub
'If a hail option is selected, execute the following code.
Else
.TextRange.Text = .TextRange.Text & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & dict2.Item(Ky)(0)
End If
End With
Next
Set dict2 = Nothing
End Sub
You were only referencing .TextRange, rather than .TextRange.Text.
Also, because you need to add the text at the end, you only need an Else condition, rather than two ElseIfs that both do the same thing! ;0)
More example code ... https://msdn.microsoft.com/en-us/library/office/ff822136.aspx

vb.net find and replace text inside a textbox in Word

I have this line of code which does a search and replace in a word document:
oDoc.Content.Find.Execute(FindText:="Invoice", ReplaceWith:="Invoice - Paid: " + paid_date, Replace:=word.WdReplace.wdReplaceAll)
Is there any way, i can do the search and replace inside a specific textbox in word rather than the whole document?
Here is one way to go about it.
For Each oCtl As Shape In doc.Shapes
If oCtl.Type = Microsoft.Office.Core.MsoShapeType.msoTextBox Then
oCtl.TextFrame.TextRange.Text.Replace("Invoice", "Invoice - Paid: " + paid_date)
End If
Next
This essentially searches all the text boxes in your document and replaces "Invoice"
As an attempt to find the name of the textbox try this...
For Each oCtl As Shape In doc.Shapes
If oCtl.Type = Microsoft.Office.Core.MsoShapeType.msoTextBox Then
MsgBox("Name: " & oCtl.Name & vbNewLine & "ID: " & oCtl.ID & vbNewLine & "Title: " & oCtl.Title & vbNewLine & "Text: " & oCtl.TextFrame.TextRange.Text)
End If
Next
This should give you unique or identifiable items of every textbox.
then you could just go like this -
If oCtl.Name = "1234" then oCtl.Text.replace(whatever)
or ID or Title or whatever you decide to choose.

VBA - How do I send new line command (\n) or tab command (\t) to a textbox.textrange.text of a PowerPointS Shape

SlideNumber = 1
Set oPPTSlide = oPPTFile.Slides(SlideNumber)
For y = 1 To oPPTSlide.Shapes.Count
MsgBox oPPTSlide.Shapes(y).Name
Next
With oPPTSlide.Shapes("Title 1")
.TextFrame.TextRange.Text = _
"Operations Monthly Report\n" & _
"April " & _
"2014"
End With
This is the code I have now. The "\n" does cause the text-box I am editing to start a new line. Is it possible? The code, in its context, is working perfectly. The exact text is sent to the text-box though, not two lines of text.
There is no "\n" in Vba instead you should use VbNewLine or VbCrLf or Vblf
Replace this
SlideNumber = 1
Set oPPTSlide = oPPTFile.Slides(SlideNumber)
For y = 1 To oPPTSlide.Shapes.Count
MsgBox oPPTSlide.Shapes(y).Name
Next
With oPPTSlide.Shapes("Title 1")
.TextFrame.TextRange.Text = _
"Operations Monthly Report" & VbCrLf & _
"April " & _
"2014"
End With
I had the problem where the vbNewLine didn't work in the UserForm, but I fixed it by checking the textBox properties and making sure multi-line is true. Give that a try.