none
Powershell command to replace or remove Watermark in Microsoft Word RRS feed

  • Question

  • Hi Fellows,

    I have a huge list of Word documents (multi-page) with text based watermark enabled such as "confidential" or "Internal"

    Is there a PowerShell way to bulk remove or modify the text watermark?

    Any guidance would be highly appreciated.


    J.A

    Monday, September 30, 2019 5:21 AM

Answers

  • Hi,
    please refer to the below:

    function ModifyWaterMarkText($file_path, $new_text){
      $objApp = New-Object -ComObject Word.Application
      $objApp.Visible = $true
      $objDocs = $objApp.Documents #Word.Documents
      $objDoc = $objDocs.Open($file_path) #Word.Document
      $objWin = $objDoc.ActiveWindow #Word.Window
      $objPane = $objWin.ActivePane #Word.Pane
      $objView = $objPane.View #Word.View
      $tmpSv = $objView.SeekView
      $objView.SeekView = 9 #WdSeekView.wdSeekCurrentPageHeader
      $objSel = $objApp.Selection #Word.Selection
      $objHF = $objSel.HeaderFooter #Word.HeaderFooter
      $objShapes = $objHF.Shapes #Word.Shapes
      If($objShapes.Count -gt 0){
        $objShape = $objShapes.Item(1) #Word.Shape
        $objTef = $objShape.TextEffect #Word.TextEffectFormat
        $objTef.Text = $new_text
      }
      $objView.SeekView = $tmpSv
      $objDoc.Close(-1) #WdSaveOptions.wdSaveChanges
    
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objTef) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objShape) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objShapes) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objHF) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objSel) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objView) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objPane) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objWin) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objDoc) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objDocs) > $null
    
      [GC]::Collect()
      [GC]::WaitForPendingFinalizers()
      [GC]::Collect()
    
      $objApp.Quit()
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objApp) > $null
    
      [GC]::Collect()
      [GC]::WaitForPendingFinalizers()
      [GC]::Collect()
    }
    
    ModifyWaterMarkText "C:\wk\SampleWaterMark.docx" "Internal"

    Best Regards,
    kinuasa
    • Marked as answer by JunaidAhmedpk Tuesday, October 1, 2019 1:39 PM
    Tuesday, October 1, 2019 2:13 AM

All replies

  • Hi,
    please refer to the below:

    function ModifyWaterMarkText($file_path, $new_text){
      $objApp = New-Object -ComObject Word.Application
      $objApp.Visible = $true
      $objDocs = $objApp.Documents #Word.Documents
      $objDoc = $objDocs.Open($file_path) #Word.Document
      $objWin = $objDoc.ActiveWindow #Word.Window
      $objPane = $objWin.ActivePane #Word.Pane
      $objView = $objPane.View #Word.View
      $tmpSv = $objView.SeekView
      $objView.SeekView = 9 #WdSeekView.wdSeekCurrentPageHeader
      $objSel = $objApp.Selection #Word.Selection
      $objHF = $objSel.HeaderFooter #Word.HeaderFooter
      $objShapes = $objHF.Shapes #Word.Shapes
      If($objShapes.Count -gt 0){
        $objShape = $objShapes.Item(1) #Word.Shape
        $objTef = $objShape.TextEffect #Word.TextEffectFormat
        $objTef.Text = $new_text
      }
      $objView.SeekView = $tmpSv
      $objDoc.Close(-1) #WdSaveOptions.wdSaveChanges
    
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objTef) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objShape) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objShapes) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objHF) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objSel) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objView) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objPane) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objWin) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objDoc) > $null
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objDocs) > $null
    
      [GC]::Collect()
      [GC]::WaitForPendingFinalizers()
      [GC]::Collect()
    
      $objApp.Quit()
      [Runtime.Interopservices.Marshal]::ReleaseComObject($objApp) > $null
    
      [GC]::Collect()
      [GC]::WaitForPendingFinalizers()
      [GC]::Collect()
    }
    
    ModifyWaterMarkText "C:\wk\SampleWaterMark.docx" "Internal"

    Best Regards,
    kinuasa
    • Marked as answer by JunaidAhmedpk Tuesday, October 1, 2019 1:39 PM
    Tuesday, October 1, 2019 2:13 AM
  • Hi Kinuasa,

    Thank you so much dear. You saved my day (actually days) :).

    I tested the script and it worked for individual document. Now I will convert it to run for bulk files in a folder.

    Thanks again.


    J.A

    Tuesday, October 1, 2019 1:39 PM