I have a word document, that I read with the following code:
$objWord = New-Object -Com Word.Application
$objWord.Visible = $false
$objDocument = $objWord.Documents.Open($Formularpath, $false, $true)
$documenttext = $objDocument.wordopenxml
Now I have my document, but there is alot of text I don't need. How can I cut the text, e.g. until 1 specific word? I know split(';') but I will need a whole word...
Related
Am I able to use Powershell to create a PowerPoint Document with one slide and then save the document? But keep it hidden in the background so it isn't visible. i use something Similar for word documents
$word.Application.Visible = $false
$doc = $word.documents.Add()
$doc.SaveAs("C:\Users\MYUSERAREA\test.doc")
$word.Quit()```
there's a reddit post where a user states is it possible to create the presentation and add blank slides with below code, you can give it a try, even thougs he/she's looking on how to add content to such blank slides.
powershwel and msPowerPoint
add-type -assembly microsoft.office.interop.powerpoint
$Application = New-Object -ComObject powerpoint.application
$application.visible = [Microsoft.Office.Core.MsoTriState]::msoTrue
$slideType = "microsoft.office.interop.powerpoint.ppSlideLayout" -as [type]
$blanklayout = $slideType::ppLayoutTitleOnly
$presentation = $application.Presentations.add()
$slide = $presentation.slides.add(1,$blanklayout)
I would like to extract formatting information from Word documents with PowerShell. Using Word you can search formatted pieces of texts. This way Word highlights the parts satisfying the criterion (e.g. green underlined text). With this one I can find italic text in PowerShell as well:
$objWord = New-Object -Com Word.Application
$myWordFile = 'C:\My\Word\File.docx'
$objDocument = $objWord.Documents.Open($myWordFile)
$objDocument.Paragraphs[0].Range.Find.Font.Italic = $true
$objDocument.Paragraphs[0].Range.Find.Execute()
However, I'm curious about the italic text itself, a similar thing as the content of the $matches for -match.
Here is an example of what you are trying to do …
This is find and replace, so, ignore that replace part if that is not your end goal - it is also find words and then applying italics, but the same approach can be used to just find all italicized words.
$application = New-Object -comobject word.application
$application.visible = $true
$document = $application.documents.open("C:fsoTest.docx")
$selection = $application.Selection
$words = "exchange","sql"
$matchCase = $false
$matchWholeWord = $true
$matchWildCards = $false
$matchSoundsLike = $false
$matchAllWordForms = $false
$forward = $true
$wrap = 1
$format = $true
$replace = 2
Foreach ($word in $words)
{
$findText = $word
$replaceWith = $word
$selection.find.replacement.font.italic = $true
$exeRTN = $selection.find.execute($findText,$matchCase,
$matchWholeWord,$matchWIldCards,$matchSoundsLike,
$matchAllWordForms,$forward,$wrap,$format,$replaceWith,
$replace)
}
… as documented here:
Hey, Scripting Guy! How Can I Italicize Specific Words in a Microsoft Word Document?
I have this script that is supposed to convert the first table from a word document to text, but unfortunately it does nothing:
$word = New-Object -ComObject word.application
$word.visible = $false
$folderpath = "C:\mypath\myfile.docx"
$Doc = $word.Documents.open($folderpath)
$Doc.Tables.item(1).ConvertToText
$Doc.saveas([ref]$folderpath, [ref]$SaveFormat::wdFormatDocumentDefault)
$Doc.close()
$word.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word)
However using a VB script with essentially the same code does do this seemingly easy task perfectly:
Dim wrdApp: Set wrdApp = WScript.CreateObject(""Word.Application"")
Dim wrdDoc
Set wrdDoc = wrdApp.Documents.Open("C:\mypath\myfile.docx")
wrdDoc.Tables(1).ConvertToText
wrdDoc.SaveAS ("C:\mypath\myfile.docx")
wrdDoc.Close SaveChanges=True
wrdApp.Quit
Set wrdApp = Nothing
Set wrdDoc = Nothing
Can anyone point me towards the reason the powershell script is simply not converting the table?
Thank you!
Tiaan
I have tried the following code and it worked for me. Pretty same as yours. You need to add parenthesis at the end of method convertToText(not required in vbscript) and simply save the document.
$word = New-Object -ComObject word.application
$word.visible = $false
$path = "C:\mypath\myfile.docx"
$Doc = $word.documents.open($path)
$Doc.Tables.item(1).convertToText() # <--- Add parenthesis () at the end
$Doc.save
$Doc.close
$word.quit
I want to replace header in word document.
$pth = "d:\test\test.docx"
$objWord = New-Object -ComObject word.application
$objWord.Visible = $True
$objDoc = $objWord.Documents.Open($pth)
$objSelection = $objWord.Selection
$Section = $objDoc.Sections.Item(1)
$header = $Section.Headers.Item(1)
This return me a plain text:
Write-Host $header.Range.Text
But my header have an image and table. Can i replace string in header without destroying header? I replace strings in word document and works great. My only problem is header.
Link to example Word document header below.
http://zapodaj.net/223c522426648.png.html
Try this:
$replaceWith = "New Text !"
$replace = [Microsoft.Office.Interop.Word.WdReplace]::wdReplaceAll
$findWrap = [Microsoft.Office.Interop.Word.WdFindWrap]::wdFindContinue
$find = $header.Range.find
$find.Execute($header.Range.Text,
$false, #match case
$false, #match whole word
$false, #match wildcards
$false, #match soundslike
$false, #match all word forms
$true, #forward
$findWrap,
$null, #format
$replaceWith,
$replace)
The pictures and other tables, should remain untouched.
I don't know solution in powershell but I use VBA runed from powershell.
// code is changed so if it don't work let me know
$objWord = New-Object -ComObject word.application
$objWord.Visible = $True # don't have to be true
$pathToFile = "d:\Delivery_Templates\filename.docx" #path to your file
$objDoc = $objWord.Documents.Open(pathToFile )
$objSelection = $objWord.Selection
$objWord.Run('myReplace', [ref] $currentVersion); # myReplace - macro name, currentVersion - macro parameter
I have a powershell script that writes to a large table in a word document, but it is slow. I was wondering if there was a way I could speed it up or is there a more efficient way to do it. I'm only writing to the last row of the table
$wd = New-Object -ComObject Word.Application
$wd.Visible = $false
$doc = $wd.Documents.Open("test.doc" )
$iTable = $doc.Tables.Item(1)
$r = $iTable.Rows.Count
$c = 1
#write to doc
$iTable.Cell($r, $c).Range.Text=$int
$iTable.Cell($r, $c+1).Range.Text=(get-item env:$x).Value
$iTable.Cell($r, $c+2).Range.Text=$lob.ToUpper()
$iTable.Cell($r, $c+3).Range.Text=$line