Link to home
Start Free TrialLog in
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?
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of et73
et73

ASKER

I am getting an runtime error.  This method or property is not available because the object refers to the end of a table row.
Ah tables. Always seem to need separate coding. I'll see it I can the produce the error and fix it.
Avatar of et73

ASKER

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
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

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

 
Avatar of et73

ASKER

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.
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.
Avatar of et73

ASKER

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.
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

Avatar of et73

ASKER

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!!

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
Avatar of et73

ASKER

Got compile error, method or data member not found at  "If Para.sentences.count > 1 then"
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of et73

ASKER

Thank you that is exactly what I needed!!!
(Eventually :) )Thanks