Avatar of et73
et73 asked on

Macro for Word

I want to add a symbol at the end of the first sentance of every paragraph throughout the document.  How can I create a marco to do that?
Microsoft ApplicationsMicrosoft WordOffice Productivity

Avatar of undefined
Last Comment
GrahamSkan

8/22/2022 - Mon
GrahamSkan

This inserts a dropper symbol

Sub InsertDropper()
    Dim para As Paragraph
    Dim rng As Range
   
    For Each para In ActiveDocument.Paragraphs
        Set rng = para.Range.Sentences(1)
        rng.Collapse wdCollapseEnd
        rng.Move wdCharacter, -1
        rng.InsertSymbol -4063, "Wingdings", True
    Next para
End Sub
ASKER
et73

I am getting an runtime error.  This method or property is not available because the object refers to the end of a table row.
GrahamSkan

Ah tables. Always seem to need separate coding. I'll see it I can the produce the error and fix it.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
et73

Graham:

Thank you.  I actually need to insert a Paragraph Separator at the end of each sentance.  The insert command is "Selection.InsertStyleSeparator".  I cannot figure out how to incorporate it into your code.  Please help.

thank you.
ET
GrahamSkan

This code detects the end of row, and steps back a character before inserting the symbol.

Sub InsertDropper()
    Dim para As Paragraph
    Dim rng As Range
   
    For Each para In ActiveDocument.Paragraphs
        Set para = ActiveDocument.Paragraphs(p)
        Set rng = para.Range.Sentences(1)
        rng.Collapse wdCollapseEnd
        rng.Move wdCharacter, -1
        If rng.Tables.Count > 0 Then
            If rng.Rows(1).Range.End - rng.End = 1 Then
                rng.Move wdCharacter, -1
            End If
         End If
        rng.InsertSymbol -4063, "Wingdings", True
    Next para
End Sub

GrahamSkan

Firstly, it seems the method is not available to the Range object, so we must first select the range. and use the Selection Object.
Secondly what it does is to join the paragraph to the next one, supressing the line break, regardless of style. It works on the whole paragraph, so it can't just be set on the first sentence.
This code tries.

Sub InsertStyleSep()
    Dim para As Paragraph
    Dim rng As Range
   
    For Each para In ActiveDocument.Paragraphs
        Set rng = para.Range.Sentences(1)
        rng.Collapse wdCollapseStart 'not really necessary, just moves to the range to the start of the sentence
        rng.Select
        Selection.InsertStyleSeparator
    Next para
End Sub

 
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
et73

It's getting close.  But, it does not insert the StyleSeparator at the end of the first sentance in the paragraph.  It replaces the existing paragraph mark with the Style Separator.

Thank you so much for your help.
GrahamSkan

Yes. That was my point. The style separator applies to the whole paragraph. It is meaningless to apply it to the first sentence because that is already a part of the same paragraph.
ASKER
et73

There are some documents we need to insert the StyleSeparator at the end of the first sentance in order to apply two styles in a paragraph.  Would it be possible to detect the first "." and insert the StyleSeparator?  Then, jump to the next paragraph and do the same thing.

Again.  Thank you so much for your help.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
GrahamSkan

Duh!. Sorry, sometimes you really have to rub my nose in it before I get the point.

You want the first sentence separated out as a paragraph in its own right, then rejoined to the rest of the old paragraph with a style separator.

Sub InsertStyleSep()
    Dim para As Paragraph
    Dim rng As Range
   
    For Each para In ActiveDocument.Paragraphs
        Set rng = para.Range.Sentences(1)
        rng.Collapse wdCollapseEnd
        rng.InsertParagraphAfter
        rng.Expand wdSentence
        rng.Select
        Selection.InsertStyleSeparator
    Next para
End Sub

ASKER
et73

The code works for paragraphs that has more than 1 sentances.  But, it will go to infinite loop when a paragraph only has one sentance.  Can we skip the paragraph only has 1 sentance.

Thank you so much for you help...   You are awesome!!

GrahamSkan

Try this:

Sub InsertStyleSep()
    Dim para As Paragraph
    Dim rng As Range
   
    For Each para In ActiveDocument.Paragraphs
        If Para.sentences.count > 1 then
        Set rng = para.Range.Sentences(1)
        rng.Collapse wdCollapseEnd
        rng.InsertParagraphAfter
        rng.Expand wdSentence
        rng.Select
        Selection.InsertStyleSeparator
        endif
    Next para
End Sub
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
et73

Got compile error, method or data member not found at  "If Para.sentences.count > 1 then"
ASKER CERTIFIED SOLUTION
GrahamSkan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
et73

Thank you that is exactly what I needed!!!
GrahamSkan

(Eventually :) )Thanks
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes