I need to find and replace text in Embedded document of a word doc .

This topic contains 1 reply, has 1 voice, and was last updated by Profile photo of Faizaan Khan Faizaan Khan 5 months, 1 week ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #38206
    Profile photo of Faizaan Khan
    Faizaan Khan
    Participant

    function aword{
    #$path="E:\proj\"
    [Console]::Out.Flush()
    $list=$null
    #$list = gci -Path $path -Include *.docx,*.doc -Force -recurse -Exclude ~$*
    $list = gci "D:\3RD year\PROJECT\" -Include *.docx,*.doc -Force -recurse
    Add-Type -AssemblyName Microsoft.Office.Interop.Word
    echo "Processing Find And Replace Operation in word"

    foreach ($foo in $list)
    {
    echo "Processing Find And Replace Operation in word $foo"

    $counter = 0
    $objWord1 = New-Object -ComObject word.application
    $objWord1.Visible = $False
    $objDoc1 = $objWord1.Documents.Open("$foo")
    $numObjects = $objDoc1.InlineShapes.Count
    echo $numObjects
    if($numObjects -gt 0)
    {
    for($i=1;$i -le $numObjects;$i++)
    {
    if($objDoc1.InlineShapes($i).Type -eq 1)
    {
    $inlineShape = $objDoc1.InlineShapes($i)
    echo $objDoc1.InlineShapes($i).Type

    $PoorId= $objDoc1.InlineShapes($i).OLEFormat.ProgID.ToLower()
    echo $PoorId
    if ($PoorId.StartsWith("word.document."))
    {
    $objDoc1.InlineShapes($i).OLEFormat.Activate
    echo $PoorId.StartsWith("word.document.")
    $msoTrue = [Microsoft.Office.Core.MsoTriState]::msoTrue
    $objDoc1.InlineShapes($i).OLEFormat.PreserveFormattingOnUpdate =$msoTrue
    #$doc = $inlineShape.OLEFormat.Object
    $objDoc1.InlineShapes($i).OLEFormat.Open() # **************its till nw**********
    $objDoc1.InlineShapes($i).OLEFormat.Object.Visible=$False # **********i dont know what i should do here******* active window has to e made visible but whose??
    $objDoc1.InlineShapes($i).OLEFormat.Edit()

    $objSelection = $objDoc1.InlineShapes($i).OLEFormat.Object.Selection #*** same problem here

    $findtext= "hello"
    $ReplaceText = "world"
    $wdReplaceOne = 1
    $FindContinue = 1
    $ReplaceAll = 2
    $MatchFuzzy = $False
    $MatchCase = $True
    $MatchPhrase = $false
    $MatchWholeWord = $True
    $MatchWildcards = $True
    $MatchSoundsLike = $False
    $MatchAllWordForms = $False
    $Forward = $True
    $Wrap = $FindContinue
    $Format = $False
    [bool]$rec = $objSelection.Find.execute(
    $FindText,
    $MatchCase,
    $MatchWholeWord,
    $MatchWildcards,
    $MatchSoundsLike,
    $MatchAllWordForms,
    $Forward,
    $Wrap,
    $Format,
    $ReplaceText,
    $wdReplaceAll
    )

    $objDoc1.InlineShapes($i).OLEFormat.Object.save()#***the area of previous error is the sol for it...
    $objDoc1.InlineShapes($i).OLEFormat.Object.Close()#****

    }

    }

    }
    }

    }

    $objDoc1.save()
    $objDoc1.close()
    }

    aword

    #38207
    Profile photo of Faizaan Khan
    Faizaan Khan
    Participant

    But i DOn't really know where I'm stuck

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.