Welcome › Forums › General PowerShell Q&A › Seaching and replacing in MS Word with regex
- This topic has 1 reply, 2 voices, and was last updated 1 month, 3 weeks ago by
Senior Moderator.
-
AuthorPosts
-
-
November 25, 2020 at 2:15 am #273957
Hi folks,
I’m new to this forum and to powershell.
I have a Word document that needs to be transformed with pandoc to an asciidoc document. However, the cross references are not set. The word document somewhat looks like:
1.2.3.4 Chaptername
…..
1.3 ChapternameWhat I would like to do is to find each chapter numbering via regex and replace or add something to make it look like this:
1.2.3.4 Chaptername [[Chaptername_1.2.3.4]]
…..
1.3 Chaptername [[Chaptername_1.3]]I was able to get the things correctly, however, I cannot replace it within the existing word document. Here is a code snippet:
PowerShell1234567891011$application = New-Object -comobject word.application$application.visible = $True #just to check what is happening$document = $application.documents.open("document.docx")$paras = $document.Paragraphsforeach($line in $paras) {if ($line.Range.ListFormat.ListString -match '\d.{1,14}\d') {$numbering = $matches[0]$line -replace ($numbering + " " +$line.Range.Text + " [[Chaptername_"+$numbering +"]]")}Obviously, the way I run through the document does not allow to use Set-Content and Find.Execute () function does not seem to be appropriate for what I want to do. How can I replace the lines in the document.
Any hint/help would be appreciated.
Thanks in advance,
Wulf-
This topic was modified 1 month, 3 weeks ago by
kvprasoon. Reason: Code formatting
-
This topic was modified 1 month, 3 weeks ago by
-
November 25, 2020 at 5:12 am #274008
I never used this module, but you can give PSWriteWord PowerShell module ashot.
https://evotec.xyz/hub/scripts/pswriteword-powershell-module/-
This reply was modified 1 month, 3 weeks ago by
kvprasoon.
-
This reply was modified 1 month, 3 weeks ago by
-
-
AuthorPosts
- You must be logged in to reply to this topic.