Reputation: 13
I have created a word VBA system that takes information from excel spreadsheets and creates a word document. The document each time is a proposal for a job, so there is typically at least 1, often multiple lines that have a subtotal:
Sub-Total $1,000.00
I want to write a piece of code (to fit in a button) that finds "Sub-Total" in the document, selects the entire line, and makes it bold. It will always be on 1 line and the line will always start with "Sub-Total".
I tried to work with finding text, but the dollar amount changes with any new data import; only the word "Sub-Total" itself is static. Any help is appreciated. Thanks!
Upvotes: 0
Views: 991
Reputation: 1265
Note that its good practice to include a minimum, viable code sample which shows what you've tried when you're asking a stackoverflow question.
The first question is, if you're generating the Word document from Excel, why don't you format the "Sub-Total" line as you create the Word document(?); its probably easier than trying to find and format the text afterwards.
However, assuming you can't for some reason, here is a working example which opens a Word document, loops through all sentences, checks if the sentence starts with "Sub-Total ", and if it does, makes it bold. It assumes that the text (e.g. "Sub-Total $1500.00") is a complete sentence and the whole sentence needs to be bold.
Option Explicit
Public Sub FindAndBold()
Dim WordApp As Word.Application
Dim MyWordDocument As Word.Document
Dim Counter As Long
Set WordApp = New Word.Application
Set MyWordDocument = Word.Application.Documents.Open("C:\test.docx")
For Counter = 1 To MyWordDocument.Sentences.Count
With MyWordDocument.Sentences(Counter)
If Left$(.Text, 11) = "Sub-Total: " Then
.Bold = True
End If
End With
Next
End Sub
Upvotes: 1