Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    2 Star Lounger
    Join Date
    Nov 2001
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How can I find the first period (.) in a sentence? (Word 2002)

    Hi,

    I'm sure there is a simple solution to this, but using VBA, I need to loop through each sentence in my document and place a carriage return after the first period (.)

    What is the most efficient way to do this?

    Any help much appreciated!

    Thanks, Chris

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: How can I find the first period (.) in a sentence? (Word 2002)

    How do you define a sentence? Usually, a sentence is delimited by a period, so "How can I find the first period in a sentence" sounds strange. Do you mean a paragraph perhaps?

  3. #3
    2 Star Lounger
    Join Date
    Aug 2002
    Location
    Brooklyn, New York, USA
    Posts
    176
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How can I find the first period (.) in a sentence? (Word 2002)

    I don't know if this will help, but if you are in a sentence in Word and press F8 three times you will select that sentence up to the first period. So, for example, if your sentence were: "So that's the answer, Mr. Smith, the whole answer." you will note that there is a period after Mr. If you place the cursor anywhere in the sentence prior to that period and press F8 three times, you will select the portion of the sentence up to and including the "Mr."

    Good luck,

  4. #4
    2 Star Lounger
    Join Date
    Nov 2001
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How can I find the first period (.) in a sentence? (Word 2002)

    Yes, I'm sorry. I mean a paragraph.

    Basically what I am trying to do is loop through a document, search for all Heading 3 style paragraphs, then within each paragraph found, search for and replace the first period with a carriage return.

    Any ideas? [img]/forums/images/smilies/smile.gif[/img]

    Thanks

  5. #5
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: How can I find the first period (.) in a sentence? (Word 2002)

    Edited, you totally redefined the problem while I composed the solution! Sentences are not the same as Heading style 3 paragraphs! This is only a solution for sentences.

    I don't know if there is a more efficient way, because I don't write Word macros, but here is a way that seems simple to me. Remember that Word's algorithm for sentences is dumb, so, for example, if you have "Call Dr. Brown to the phone!", Word thinks that this is two sentences. HTH --Sam <pre>Option Explicit

    Sub Macro1()
    Dim s As Range, i As Long
    For i = 1 To ActiveDocument.Sentences.Count
    ActiveDocument.Sentences(i).InsertAfter vbCr
    Next i
    End Sub</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  6. #6
    2 Star Lounger
    Join Date
    Nov 2001
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How can I find the first period (.) in a sentence? (Word 2002)

    Thanks Sammy.

    Do you know how I might go about looping through all Heading 3 styles in a document?
    Chris

  7. #7
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: How can I find the first period (.) in a sentence? (Word 2002)

    <pre>Option Explicit

    Sub Macro1()
    Dim s As Range, i As Long
    For i = 1 To ActiveDocument.Sentences.Count
    With ActiveDocument.Sentences(i)
    If .Style = "Heading 3" Then _
    .InsertAfter vbCr
    End With
    Next i
    End Sub</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: How can I find the first period (.) in a sentence? (Word 2002)

    Here is code to loop through all paragraphs with Heading3 style (whatever it's named in the local language) without looping through *all* paragraphs:

    Sub ProcessDoc()
    On Error GoTo Err_Doc
    Application.ScreenUpdating = False

    ' Move to start of document (just to be on the safe side)
    Selection.HomeKey Unit:=wdStory

    With Selection.Find
    ' Prepare
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Text = ""
    .Replacement.Text = ""
    ' Find Heading3 style
    .Style = wdStyleHeading3
    End With

    Do While Selection.Find.Execute
    ' Insert code to process heading here
    ' ...
    Loop

    Exit_Doc:
    ' Clean up
    On Error Resume Next
    Application.ScreenUpdating = True
    Exit Sub

    Err_Doc:
    ' Error handling - inform user and force cleanup
    MsgBox Err.Description, vbExclamation
    Resume Exit_Doc
    End Sub

  9. #9
    2 Star Lounger
    Join Date
    Nov 2001
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How can I find the first period (.) in a sentence? (Word 2002)

    Hans, thank you!

    I didn't even know the Do While Selection.Find.Execute command existed [img]/forums/images/smilies/smile.gif[/img]

    As a side note, do you know how I would go about searching for the first period (.) within each paragraph found?

    ie:

    Do While Selection.Find.Execute

    'Search paragraph for first period (.)

    Loop

    Thanks again, Chris

  10. #10
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: How can I find the first period (.) in a sentence? (Word 2002)

    Hans's solution should run a lot faster on long documents. That's why he's a Word programmer and I'm an Excel programmer! I just go buy a faster machine. <img src=/S/wink.gif border=0 alt=wink width=15 height=15>

    BTW, not to ask stupid questions, BUT... Why not just redefine Heading 3 style to have larger spacing after? That would be the most efficient, not to mention simplier. I try to use the KISS <img src=/S/kiss.gif border=0 alt=kiss width=34 height=15> standard.
    <pre>ActiveDocument.Styles("Heading 3").ParagraphFormat.SpaceAfter = 15</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: How can I find the first period (.) in a sentence? (Word 2002)

    You could set the .Text property of Selection.Find to a period:

    With Selection.Find
    ...
    .Text = "."
    ...
    End With

    and insert a paragraph if found (Selection.TypeParagraph).

    You'll have to move two paragraphs with Selection.MoveDown Unit:=wdParagraph, Count:=2 to avoid finding the second etc. period (if existent).

    This makes the code:

    Sub ProcessDoc()
    On Error GoTo Err_Doc
    'Application.ScreenUpdating = False

    ' Move to start of document (just to be on the safe side)
    Selection.HomeKey Unit:=wdStory

    With Selection.Find
    ' Prepare
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Text = "."
    .Replacement.Text = ""
    ' Find Heading3 style
    .Style = wdStyleHeading3
    End With

    Do While Selection.Find.Execute
    ' Code to process heading here
    Selection.InsertParagraph
    Selection.MoveDown Unit:=wdParagraph, Count:=2
    Loop

    Exit_Doc:
    ' Clean up
    On Error Resume Next
    Application.ScreenUpdating = True
    Exit Sub

    Err_Doc:
    ' Error handling - inform user and force cleanup
    MsgBox Err.Description, vbExclamation
    Resume Exit_Doc
    End Sub

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: How can I find the first period (.) in a sentence? (Word 2002)

    Hey! I'm an Access <img src=/S/nuts.gif border=0 alt=nuts width=15 height=15>, not a Word programmer! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  13. #13
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: How can I find the first period (.) in a sentence? (Word 2002)

    Last time I had a Macro like that someone managed to run it on a document where the last paragraph was formatted as a Heading 3. This caused an infinite loop. The workaround was to add
    If Selection.End = ActiveDocument.Content.End then Exit Do
    just before the
    Loop

    StuartR

  14. #14
    2 Star Lounger
    Join Date
    Nov 2001
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How can I find the first period (.) in a sentence? (Word 2002)

    Yes, thanks for that guys!

  15. #15
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How can I find the first period (.) in a sentence? (Word 2002)

    Another way to do that (without a macro) would have been a wildcard find/replace

    Find what: ([!^13]@.)([!^13]@^13) ((+ Format > Style: Heading 3))
    Replace with: 1^p2

    ^13 has to be used in "Find what" instead of ^p when using wildcards (for resons unknown).
    [!^13] is any character but a paragraph mark, [!^13]@ is any number of those.

    ([!^13]@.) will match everything in a "Heading 3" paragraph up to and including the full stop,

    ([!^13]@^13) will match the rest of the paragraph.

    Replace with: 1^p2 inserts a paragraph mark between those two parts.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •