Formatting issues

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Formatting issues

Post by yanlok1345 »

Hi all,

I have encountered some issues regarding word macro. I want the macro to do the following:
(see the attachement for my expected outcomes (with before and after)
Before running the macro.docx
After running the macro.docx
Change words into Italic starting from after the ":" (the colon) to before the "Secretary" in bold type

(Please ignore all indent. I just want to figure out whether Word macro can identify text from after the colon to before the bold typed "Secretary")

How can I achieve that? Here's the current code I drafted:

Code: Select all

Sub ItalicizeWords()

Dim objDoc As Document
Dim objRange As Range

Set objDoc = ActiveDocument
Set objRange = objDoc.Content

With objRange.Find
.Text = ":* Secretary"
.Replacement.Text = ""
.Forward = True
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With

objRange.Italic = True

End Sub
The above code can change all text to italic, but that's not what I wanted.

Many thanks for your help!
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Formatting issues

Post by HansV »

Most of the document occurs between a colon and the word Secretary, so it will be Italicized. You mention that that's not what you wanted. But what did you really want?
Best wishes,
Hans

User avatar
SpeakEasy
4StarLounger
Posts: 563
Joined: 27 Jun 2021, 10:46

Re: Formatting issues

Post by SpeakEasy »

On a side note, if you want dummy text in a document consider the rand statement

https://support.microsoft.com/en-us/off ... e4dd676f87#

User avatar
kdock
5StarLounger
Posts: 720
Joined: 21 Aug 2011, 21:01
Location: The beautiful hills of Western North Carolina

Re: Formatting issues

Post by kdock »

Hi yanlok1345,

You've set the objRange to the entire document. (Set objRange = objDoc.Content)
You've told the macro to find the text you want and replace it with nothing. (.Replacement.Text = "")
At the end, you tell the macro to italicize the entire document. (objRange.Italic = True)

What you want can be done, but it's much more complex than your current code and requires using the Search function differently. It can't be done in a single pass, either. You'll need to find each colon, determine whether it's the colon following "Secretary" (in which case you have to skip it), and if it's the right colon, set the beginning of the range one character to the right, then find, say, "^pSecretary" (Paragraph mark followed by "Secretary") and set the end of the range. Once you've defined the range, you can apply italics.

In reviewing your document, it seems there are a number of colons that wouldn't qualify to be formatted this way. For example, at the end of a paragraph such as "...my reply to your question is a follows:" Unwanted colons could be skipped in your macro by searching for ": " (colon followed by a space) as it seems that's what you intend.

In order to fully automate this process, you'll need to make your search criteria very specific so you don't italicize anything that shouldn't be formatted that way. You'll also need your macro to loop through the document until it reaches the end. It's a lot, but it can be done.

Kim
"Hmm. What does this button do?" Said everyone before being ejected from a car, blown up, or deleting all the data from the mainframe.

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Re: Formatting issues

Post by yanlok1345 »

HansV wrote:
01 Dec 2023, 19:21
Most of the document occurs between a colon and the word Secretary, so it will be Italicized. You mention that that's not what you wanted. But what did you really want?
For example, Here are four paragraphs:

Henry: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.

Secretary: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.

Henry: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.

Secretary: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.

I want the macro to change speakers' words (those underlined words)(they are not underlined in the document, just for demonstration here) into italic form, except the speeches made by "Secretary". After running the macro, the result are as follows:

Henry: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.

Secretary: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.

Henry: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.

Secretary: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.

But I don't know how to make a macro that can automatically select the text from "after the colon" of all speakers to the place before Secretary.

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Formatting issues

Post by HansV »

Try this:

Code: Select all

Sub ItalicizeWords()
    Dim objDoc As Document
    Dim objRange As Range
    Dim lngRngStart As Long
    Dim lngNameStart As Long
    Dim lngStart As Long
    Dim lngEnd As Long
    Set objDoc = ActiveDocument
    Set objRange = objDoc.Content
    With objRange.Find
        .ClearFormatting
        .Text = ": "
        .Forward = True
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        Do While .Execute
            lngRngStart = objRange.Start
            lngNameStart = lngRngStart - 9
            If lngNameStart < 1 Then lngNameStart = 1
            If objDoc.Range(Start:=lngNameStart, End:=lngRngStart).Text = "Secretary" Then
                lngEnd = lngRngStart - 10
                objDoc.Range(Start:=lngStart, End:=lngEnd).Font.Italic = True
            Else
                lngStart = lngRngStart + 2
            End If
        Loop
    End With
End Sub
Best wishes,
Hans

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Re: Formatting issues

Post by yanlok1345 »

HansV wrote:
03 Dec 2023, 14:11
Try this:

Code: Select all

Sub ItalicizeWords()
    Dim objDoc As Document
    Dim objRange As Range
    Dim lngRngStart As Long
    Dim lngNameStart As Long
    Dim lngStart As Long
    Dim lngEnd As Long
    Set objDoc = ActiveDocument
    Set objRange = objDoc.Content
    With objRange.Find
        .ClearFormatting
        .Text = ": "
        .Forward = True
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        Do While .Execute
            lngRngStart = objRange.Start
            lngNameStart = lngRngStart - 9
            If lngNameStart < 1 Then lngNameStart = 1
            If objDoc.Range(Start:=lngNameStart, End:=lngRngStart).Text = "Secretary" Then
                lngEnd = lngRngStart - 10
                objDoc.Range(Start:=lngStart, End:=lngEnd).Font.Italic = True
            Else
                lngStart = lngRngStart + 2
            End If
        Loop
    End With
End Sub
Many thanks for your help! It works well!