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-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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

    • 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. 
    • CarlD
      By CarlD
      Is there a reliable way to ensure that data assigned to variables in a script is overwritten or deleted when the script exits? I have scripts that encrypt/decrypt data and would like to ensure, if possible, that the encryption keys and decrypted data do not stay in memory after the script exits. Thanks.
    • rudi
      By rudi
      Hello,
      Propably not an absolute clean approach, (not checking/caring about little / big endian), but it's doing, what I need: Return the last modified time stamp including the milliseconds:
       
      #include <Date.au3> $file = "c:\temp\test.txt" ; file must already exist $TSLastModMs = GetFileLastModWithMs($file) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $TSLastModMs = ' & $TSLastModMs & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console Func GetFileLastModWithMs($_FullFilePathName) local $h = _WinAPI_CreateFile($_FullFilePathName, 2, 2) local $aTS = _Date_Time_GetFileTime($h) _WinAPI_CloseHandle($h) local $aDate = _Date_Time_FileTimeToArray($aTS[2]) ; [2] = LastModified Return StringFormat("%04d-%02d-%02d %02d:%02d:%02d.%03d", $aDate[2], $aDate[0], $aDate[1], $aDate[3], $aDate[4], $aDate[5], $aDate[6]) EndFunc ;==>GetFileLastModWithMs >Running AU3Check (3.3.14.5) from:C:\Program Files (x86)\AutoIt3 input:C:\temp\filetime.au3 +>12:10:00 AU3Check ended.rc:0 >Running:(3.3.14.5):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\temp\filetime.au3" --> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop @@ Debug(8) : $TSLastModMs = 2018-09-07 10:09:54.073 >Error code: 0 +>12:10:00 AutoIt3.exe ended.rc:0 +>12:10:00 AutoIt3Wrapper Finished. >Exit code: 0 Time: 0.9068 Regards, Rudi.
       
      --- original posting - what was my problem, below ---
       
      doing some search I found postings, stating that 2s will be the smallest time resolution for filegettime(). 2s seem to be fact for FAT as FS, NTFS has a much finer granularity.
       
      This posting states, that NTFS has a granularity of 100ns:
      https://superuser.com/questions/937380/get-creation-time-of-file-in-milliseconds
      is it possible to get more than just the "second" information? The reason, why I need this is, that I need to sort files by their creation sequence, and it can happen, that two files are created within the same second, so I cannot resolve their creation order without "millsecond info".
       
      Regards, Rudi.

       
      edit: I just tried PowerShell, there it's possible to retrieve even more than millisecond information:
       
      Millisecond : 336
      Ticks       : 636719150403363219
      TimeOfDay   : 11:04:00.3363219
       
      PS C:\Users\Rudi> echo test > test.txt PS C:\Users\Rudi> $(Get-ChildItem .\test.txt).creationtime | format-list Date : 07.09.2018 00:00:00 Day : 7 DayOfWeek : Friday DayOfYear : 250 Hour : 11 Kind : Local Millisecond : 336 Minute : 4 Month : 9 Second : 0 Ticks : 636719150403363219 TimeOfDay : 11:04:00.3363219 Year : 2018 DateTime : Freitag, 7. September 2018 11:04:00 regards, Rudi.
    • 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
    • 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?
       
×