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.
Related
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)
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
So I have this textbox and a string I want to set a label to the string and the value combined like this:
Lebel1.Caption = "Hello" Textbox1.Text
I don't know what the proper code is.
I guess you are having a Typo issue. Not sure if your control is Lebel or Label.
In this example you can combine a predefined Text and a Control text or Variable. Don't forget to add an space after your words.
Label1.Caption = "Hello " & Textbox1.Text & "."
Label1.Caption = "Hello " & Your variable & "."
Also you can add a line break and also use a Message Box as follows:
Msgbox = "Welcome Message." & Chr(10) & "Hello " & Textbox1.Text & "."
Completely new to VBA and need help with detailed instructions (dummy version for me).
I have a table with various columns and the following columns, specifically:
ReviewerComments
NewComment
I created a form with both of these fields and need to create an Append Comment button that moves the text from NewComment field and appends it to ReviewerComment field and time/date stamps the comments as they are added. I named this button cmdAppendComment.
I had seen someone else post something and I tried, but as I am completely new to this I know I messed it up. Any help is greatly appreciated.
This is what the VBA code looks like right now:
Private Sub cmdAppendComment_Click()
If (IsNull(NewComment.Value)) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
If (IsNull(ReviewerComments.Value)) Then
ReviewerComments.Value = NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
Else
ReviewerComments.Value = ReviewerComments.Value & _
vbNewLine & vbNewLine & _
NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
End If
NewComment.Value = ""
End Sub
I have some suggestions for your code:
1. Do not check if a textbox is null but how many characters your textbox has. You should always do it that way because otherwise you tend to get errors.
If (len(Me.NewComment.Value & "") > 0) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
Here you check the length of the string in your textbox. You need to append "" because otherwise you tend to get null-errors or something similar.
2. You forgot to reference the objects in your form correctly. You have your form and in that form you put your textboxes and also your VBA-code. Your can reference all your objects with "Me.[FormObjects]".
The compiler complains that "NewComment.Value" or "ReviewerComment.Value" is not initialized or in other words not dimensioned. With correct reference this should stop.
Private Sub cmdAppendComment_Click()
If (len(Me.NewComment.Value & "") > 0) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
If (IsNull(Me.ReviewerComments.Value)) Then
Me.ReviewerComments.Value = Me.NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
Else
Me.ReviewerComments.Value = Me.ReviewerComments.Value & _
vbNewLine & vbNewLine & _
Me.NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
End If
Me.NewComment.Value = ""
End Sub
I am trying to add the contents of my five text boxes in the form to a multi line text box named "TextBox1". However, everytime I change the content of my textboxes, instead of adding another line with the contents it replaces the pre-existing line. How do I make the next line print.
Dim Newline As String
Newline = System.Environment.NewLine
TextBox1.Text = "Plane Truss"
TextBox1.Text = TextBox1.Text & Newline & "JointCoordinates"
TextBox1.AppendText(Newline & Joint# & " " & coordinates1 & " " & bc1 & " " & jointloads1 & " " & settlements1 & " " & jointrotation1)
Is there a way I can convert the whole multiline text box into a .txt
file?
Sure...use the Lines() property of the TextBox and File.WriteAllLines():
System.IO.File.WriteAllLines("c:\some path\folder\file.txt", Result1.Lines)