Jump to content
Sign in to follow this  
Servant

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

Recommended Posts

Servant

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

Share this post


Link to post
Share on other sites
FireFox

Hi,

I don't use the Word UDF, so this may not be the best method :

;$s is the text of your paragraph
;$s2 is the replacement text
$s2 = StringRegExpReplace($s, "(?m)(.*?\.)(?:.*?)$", "$1")

_

Br, FireFox.

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
water

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
  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Servant

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

Share this post


Link to post
Share on other sites
water

 

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.

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
jchd

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)

Share this post


Link to post
Share on other sites
water

Yes.



 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
jchd

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)

Share this post


Link to post
Share on other sites
water

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 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
jchd

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)

Share this post


Link to post
Share on other sites
water

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 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
jchd

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)

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • colombeen
      By colombeen
      Hi guys,
      I'm trying to get some information using WMI, from the Win32_EncryptableVolume class.
      I exec my query, filter out the C-drive, but when I need more info using the objects methods, I only get 1 value back and I can't seem to retrieve the other out params that should be there.
      A very minimal version of what I'm trying to do (no error checking etc, very basic). You need to start SciTE as admin or you won't see any results in the console!
      #RequireAdmin $strComputer = @ComputerName $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\CIMV2\Security\MicrosoftVolumeEncryption") $objWMIQuery = $objWMIService.ExecQuery("SELECT * FROM Win32_EncryptableVolume WHERE DriveLetter='C:'", "WQL", 0) For $objDrive In $objWMIQuery ConsoleWrite("> " & $objDrive.GetConversionStatus() & @CRLF) ConsoleWrite("> " & $objDrive.GetConversionStatus().ConversionStatus & @CRLF) ConsoleWrite("> " & $objDrive.GetConversionStatus().EncryptionPercentage & @CRLF) Next The result from the console is : 
      > 0 > > What I'm expecting to get back is : 
      > 0 > 0 > 0 When using powershell I get this (run as admin is required!!!) : 
      PS C:\WINDOWS\system32> (Get-WmiObject -namespace "Root\cimv2\security\MicrosoftVolumeEncryption" -ClassName "Win32_Encryptablevolume" -Filter "DriveLetter='C:'").GetConversionStatus() ... ConversionStatus : 0 EncryptionFlags : 0 EncryptionPercentage : 0 ReturnValue : 0 ... All I seem to be getting is the ReturnValue when I use the method.
      I've tried this on multiple methods, always ending up with the same result
      Anyone here who has experience with this type of thing?
       
      Greetz
      colombeen
    • nooneclose
      By nooneclose
      My program has to first search for names in Column D that do not match up with column C. I got that search to work using arrays. It was slow and I could not figure out how to delete them so I just manually put coded the names that do not belong. I found their cell location but I do not know how to store that location and delete it.
      This is what I have so far.
      Local $NameToDelete1[6]  = _Excel_RangeFind($OpenWorkbook, "Smith, Bill") _ArrayDisplay($NameToDelete1, "Excel UDF: _Excel_RangeFind Example 1", "", 0, "|", "Sheet|Name|Cell|Value|Formula|Comment") _Excel_RangeDelete($OpenWorkbook.ActiveSheet, $NameToDelete1[2], $xlShiftUp)  
      Please help, I wanted to have this program done yesterday but I did not see this problem until yesterday. 
    • lavascript
      By lavascript
      I have a Word document containing a 9-column table where row 1 is the column headers. My goal is to read the table into a 2d array, remove some rows, update some fields, and add a few rows to the end. The resulting array will likely be a different length. Next, I want to write the data back into the table. If it's easier, I can write the data to a new document from a template containing the same table header with a blank 2nd row.
      Here's my early attempt:
      Local $oWord = _Word_Create() Local $oDoc = _Word_DocOpen($oWord, $sFile) Local $aData = _Word_DocTableRead($oDoc, 1) $aData[3][5] = "Something else" Local $oRange = _Word_DocRangeSet($oDoc, 0) $oRange = _Word_DocRangeSet($oDoc, $oRange, $wdCell, 9) _Word_DocTableWrite($oRange,$aData) This, unfortunately, writes the entire array into the first cell of row 2. What am I doing wrong?
       
    • Subz
      By Subz
      Backstory:
      Our Microsoft Office Templates shared folder was changed from a DFS share to an Isilon share. example:
      Old Server: \\Domain.com\Office\Templates
      New Server: \\Templates.domain.com\Office\Templates
      The team making the changes overlooked that several hundred thousand documents, had been attached to the old template documents.  So when you open a document which has been attached, it will take a couple of minutes to open, while it tries to locate the old server path.  I've been asked to come in and fix it, so after several hours found that the data is being held in document.zip\word\_rels\settings.xml.rels, I now need to replace the old server path with the new server path.  I didn't want to use dom as that would take too long and found a tool wtc https://github.com/NeosIT/wtc which  works perfectly, takes about 8 minutes to scan a single directory with 4000 documents and fix them.  The problem is the documents are all held on sharepoint and they want to retain the file timestamp, which is easy enough, but they also don't want to keep the "Modified By" apparently they don't like seeing all the documents appearing as "Modified by: Subz"  Anyone know of way to retain the "Modified By" info,
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I am working with Word UDF ( thanks @water! ), and, especially, with the function _Word_DocFindReplace().
      The replace does work everywhere in the document, but, it does not work in Headers or Footers.
      Am I missing something or am I forced to use the code below?
      I have already looked in the Help file ( about _Word_DocFindReplace() ), but there are no mentions about replace text in Headers/Footers.
      Sub FindAndReplaceFirstStoryOfEachType() Dim rngStory As Range For Each rngStory In ActiveDocument.StoryRanges With rngStory.Find .Text = "find text" .Replacement.Text = "I'm found .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Next rngStory End Sub Thanks everyone in advance


      Best Regards.
×