Author Posts

April 25, 2016 at 2:24 am

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

April 25, 2016 at 2:25 am

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