Jump to content

Delete the 2nd until the last sentence of a set paragraph range on a Word


Go to solution Solved by water,

Recommended Posts

I tried a lot of techniques but still have no luck..

How can I delete the second sentence until the last sentence of a set paragraph range on a Microsoft Word document?

#include <Word.au3>

Global $oWord, $oDoc

$oWord = _Word_Create()
$oDoc = _Word_DocGet($oWord, 1)

Global Const $Count = $oDoc.Paragraphs.Count

For $i = 0 To $Count - 1
   $oRange = _Word_DocRangeSet($oDoc, -1, $wdParagraph, $i, $wdParagraph, 1)

   ; Here will be placed the missing code
Next

Sample of the beginning of a Word document:

This is a sentence 1. This is a sentence 2. This is a sentence 3.

This is a sentence 4. This is a sentence 5. This is a sentence 6.

This is a sentence 7. This is a sentence 8. This is a sentence 9.

Sample of the final result:

This is a sentence 1.

This is a sentence 4.

This is a sentence 7.
Edited by Servant
Link to comment
Share on other sites

  • Solution

This deletes from the first "." in each paragraph to the end of the paragraph.

#include <Word.au3>

Global $oWord = _Word_Create()
If @error <> 0 Then Exit MsgBox(16, "Word UDF: _Word_DocFind Example", "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Global $oDoc = _Word_DocOpen($oWord, "Test Leerzeichen.docx", Default, Default, True)
If @error <> 0 Then Exit MsgBox(16, "Word UDF: _Word_DocFind Example", "Error opening 'Test Leerzeichen.docx'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

Local $oRangeFound, $oRangeText
$oRangeFound = _Word_DocFind($oDoc, ".", 0) ; Search the whole document
If @error Then Exit MsgBox(16, "Word UDF: _Word_DocFind Example 3", "Error locating the specified text in the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; Create a new range (duplicate to not alter the result of the find operating)
$oRangeText = $oRangeFound.Duplicate
$oRangeText = _Word_DocRangeSet($oDoc, $oRangeText, $WdCharacter, 1, $wdParagraph, 1) ; Move the start of the range past the "." and the end of range to the end of the paragraph
$oRangeText = _Word_DocRangeSet($oDoc, $oRangeText, Default, Default, $wdCharacter, -1) ; Move the end of the range one character to the left to not delete the new line character
$oRangeText.Text = ""
While 1
    $oRangeFound = _Word_DocFind($oDoc, ".", 0, $oRangeFound) ; Search the next "."
        If @error Then ExitLoop
    $oRangeText = $oRangeFound.Duplicate
    $oRangeText = _Word_DocRangeSet($oDoc, $oRangeText, $WdCharacter, 1, $wdParagraph, 1)
    $oRangeText = _Word_DocRangeSet($oDoc, $oRangeText, Default, Default, $wdCharacter, -1)
    $oRangeText.Text = ""
WEnd

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

This is my better solution:

#include <Word.au3>

Global $oWord = _Word_Create()
If @error <> 0 Then Exit MsgBox(16, "Word UDF: _Word_DocFind Example", "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

Global $oDoc = _Word_DocGet($oWord, 1)
If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocGet Example", _
"Error accessing collection of documents." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

Global $pCount = $oDoc.Paragraphs.Count
Local $oRange, $oRange2, $sCount, $1st, $p
Local $sFindText, $oFind, $oRangeFound, $oRangeText

For $i = 0 To $pCount - 1
  $oRange = _Word_DocRangeSet($oDoc, -1, $wdParagraph, $i, $wdParagraph, 1)

  $sCount = $oRange.Sentences.Count

  While 1
    If $sCount > 1 Then
      $oRange2 = _Word_DocRangeSet($oDoc, -1, $wdParagraph, $i, $wdSentence, 1)
      $1st = $oRange2.Text
      $oRange2 = _Word_DocRangeSet($oDoc, -1, $wdParagraph, $i, $wdParagraph, 1)
      $p = $oRange2.Text

      $sFindText = StringReplace($p, $1st, "")
      $sFindText = StringReplace($sFindText, @CR, "")
      $oFind = _Word_DocFind($oDoc, $sFindText, $oRange2, Default, Default, False, False, False)

      If @error <> 0 Then
        $oRangeFound = _Word_DocFind($oDoc, ".", $oRange2) ; Search the $oRange2

        If @error Then Exit MsgBox(16, "Word UDF: _Word_DocFind Example 3", "Error locating the specified text in the range." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

        ; Create a new range (duplicate to not alter the result of the find operating)
        $oRangeText = $oRangeFound.Duplicate
        $oRangeText = _Word_DocRangeSet($oDoc, $oRangeText, $WdCharacter, 1, $wdParagraph, 1) ; Move the start of the range past the "." and the end of range to the end of the paragraph
        $oRangeText = _Word_DocRangeSet($oDoc, $oRangeText, Default, Default, $wdCharacter, -1) ; Move the end of the range one character to the left to not delete the new line character
        $oRangeText.Text = ""
      Else
        $oFind.Delete
      EndIf

    EndIf

    $oRange2 = _Word_DocRangeSet($oDoc, -1, $wdParagraph, $i, $wdParagraph, 1)
    $sCount = $oRange2.Sentences.Count
  WEnd
Next

But please review the sentence below:

The FYE 2012 Transfer Pricing Report shows that the set of comparable companies chosen to benchmark the O&M services has a three year period weighted average (“PWAVG”) interquartile range (“IQR”) of 3.3 percent to 19.3 percent and a one year IQR of 4.0 percent to 21.8 percent.

After that code was run, the sentence in the new document was:

The FYE 2012 Transfer Pricing Report shows that the set of comparable companies chosen to benchmark the O&M services has a three year period weighted average (“PWAVG”) interquartile range (“IQR”) of 3.

And this does not seem to happen consistently I think it's because of my new code.

For example, the following sentence before and after did not have this problem.

Before:

For transaction 4, the mark-up on total cost (OI/TC) KPMG calculated is -23.8 percent, while the mark-up of total cost PwC presented in the report is -31.3 percent, which happened to be mark-up on total revenue (OI/Revenue);

And After:

For transaction 4, the mark-up on total cost (OI/TC) KPMG calculated is -23.8 percent, while the mark-up of total cost PwC presented in the report is -31.3 percent, which happened to be mark-up on total revenue (OI/Revenue);

Is it possible to fix this issue?

I think when this code execute:

$oFind = _Word_DocFind($oDoc, $sFindText, $oRange2, Default, Default, False, False, False)

  If @error <> 0 Then

...and produce the error "4 - $sFindText could not be found" it will then execute your code but it will treat the decimal point in a number as the end of the sentence..

Edited by Servant
Link to comment
Share on other sites

 

How can I delete the second sentence until the last sentence of a set paragraph range on a Microsoft Word document?

The solution depends on how you define a "sentence". You need to search for ". " for "sentecnes" within a paragraph or for ".P" where "P" is the control character for a new paragraph.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Would that be ".¶" aka '.' & ChrW(0xB6) ?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Yes.



 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Beware that a correctly written paragraph may in the general case also end in various other characters like ! ? ) … » ” „ ’ ‼ ⁇ ⁈ ⁉ ❩ ❫ ¿ ¡ and most probably another set of quotes and exotic punctuation marks when, for instance the paragraph ends with a citation from some non-english language. Incorrect punctuation only adds more difficulty.

So why not rely on paragraph marks only? Sounds more reliable.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Because in post 2 the OP showed an example with only period as an ending character.

But you are correct, sentences can end with a lot of characters.

Let's see what the OP needs ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I was just saying, both for the OP and/or for future reference.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

I see. So I think to properly identify "sentences" within a paragraph SRE would be needed?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I don't have Office installed here and I don't remember how powerful/painful regexpes are in Word.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...