Seaching and replacing in MS Word with regex

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: Chaptername

1.3 Chaptername

What I would like to do is to find each chapter numbering via regex and replace or add something to make it look like this: 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:

$application = New-Object -comobject word.application
$application.visible = $True  #just to check what is happening
$document = $"document.docx")
$paras = $document.Paragraphs
foreach($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,

I never used this module, but you can give PSWriteWord PowerShell module ashot.