Powershell script to convert a word table to text - powershell

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

Related

How can I set the text direction to RTL in Microsoft Word docx file using powershell?

I am using powershell to create a docx file.
I was able to create a file and set the style, font and alignment.
My problem is with changing the text direction from default (LTR) to RTL.
This is my working code:
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$doc = $word.documents.add()
$selections = $word.Selection
$selections.Font.Name = "Arial"
$selections.Font.Size = 16
$selections.paragraphFormat.Alignment = 2
$selections.TypeText("Hello World!")
$out_path = ".\file.docx"
$doc.SaveAs($out_path)
$doc.Close()
$word.Quit()
I did not find any information to set the text direction.
I will appreciate any help,
Thank you.
in your program use this:
$doc = $word.documents.add()
$doc.Paragraphs.ReadingOrder = 0

Create PowerPoint Document

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)

Powershell - Trim word document

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...

Remove "sharing" from Excel workbook

I'm trying to save a workbook to a new location with a password and keep the filename the same. The filname gets updated weekly with a date appended to it, so it's never the same. There are two things I'm having trouble with:
Using the SaveAs method to save the file with the same name in a different path &
I can't add a password because the workbook is shared.
I'm scripting this out in PowerShell, and if possible, I'd like to unshare the workbook in the script. Unfortunately, I can't seem to find a method that accomplishes this. Here is what I have so far... I really appreciate any advice.
$xls = new-object -com excel.application
$xls.Visible = $False
$xlsWB = $xls.Workbooks.Open("path\*.xlsx")
$xlsWB.Password = "Password"
$xlsWB.SaveAs("differentPath\*.xlsx")
$xls.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xls)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xlsWB)
Turns out, there is a method that can be called to change the sharing status of the workbook. It's ExclusiveAccess().
Here is my working code that solved the problem:
$xls = new-object -comObject excel.application
$xls.Visible = $False
$xls.DisplayAlerts = $False
$xlsWB = $xls.Workbooks.Open("FilePath")
$xlsWB.ExclusiveAccess()
$xlsWB.Password = "AddThisPassword"
$xlsWB.Save()
$xlsWB.Close()
$xls.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xls)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xlsWB)
move "CurrentPath" "NewPath"
Once I change the sharing status of the workbook, the Password method successfully adds a password to the workbook, solving the second issue described in the OP. Rather than use SaveAs(), I decided to simply move the file, which saves me from deleting the source file.
Thanks and I hope someone finds this useful.
this was my attempt at a similar problem. It removes the MultiUserEditing of files within a folder structure.
foreach ($file in (Get-ChildItem "C:\path_to_reports\" -File -Filter "*.xls" -recurse))
{
#The filter seems to also work for *.xlxsx
$Excel = New-Object -comobject Excel.Application
$Excel.Visible = $False
$Excel.DisplayAlerts = $False
$ExcelWorkbook = $Excel.workbooks.open($file.fullname)
If ($ExcelWorkbook.MultiUserEditing -eq "True")
{
$ExcelWorkbook.ExclusiveAccess()
$ExcelWorkbook.Save()
}
#close the workbook and not the file
$ExcelWorkbook.Close()
#Quit the file
$Excel.Quit()
#cleanup
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
#more clean up
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelWorkbook)
#the most clean up
Remove-Variable -Name excel
}

Powershell with Word CheckSpelling and selected dictionary

I would like to spell check some strings using the Microsoft Word API in Powershell and a specific dictionary ("English (US)").
I use the following code to do the checking but it does not seem to take into account the dictionary I want. Any ideas what is wrong? Also, command "New-Object -COM Word.Dictionary" seems to fail.
$word = New-Object -COM Word.Application
$dictionary = New-Object -COM Word.Dictionary
foreach ($language in $word.Languages) {
if ($language.Name.Contains("English (US)")) {
$dictionary = $language.ActiveSpellingDictionary;
break;
}
}
Write-Host $dictionary.Name
$check = $word.CheckSpelling("Color", [ref]$null, [ref]$null, [ref]$dictionary)
if(!$check) {
Write-Host "Spelling Error!"
}
$word.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
Remove-Variable word
The COMobject word.dictionary does not exist (at least not on my machine), here is what worked for me in the short test i did:
$dic = New-Object -COM Scripting.Dictionary #credits to MickyB
$w = New-Object -COM Word.Application
$w.Languages | % {if($_.Name -eq "English (US)"){$dic=$_.ActiveSpellingDictionary}}
$w.checkSpelling("Color", [ref]$null, [ref]$null, [ref]$dic)
Another possibility in addition to Paul's:
$dictionary = New-Object -COM Scripting.Dictionary
I was experiencing the same problem, that the Word.Application.checkSpelling() method seems to ignore any dictionary passed to it. I worked around the issue creating a Word document, defining a text range, changing the LanguageID of this range to the language I want to proof read against, and then inspecting detected spelling errors. Here is the code:
<#Function which helps to pick the language#>
function FindLanguage($language_name){
foreach($element in $Word.Languages){
if($element.Name -eq $language_name){
$element.Name
return $element
}
}
}
$Proofread_text = "The lazie frog jumpss over over the small dog."
$Word = New-Object -COM Word.Application
$Document = $Word.Documents.Add()
$Textrange = $Document.Range(0)
$english = FindLanguage("English (US)")
$Textrange.LanguageID = $english.ID
$Textrange.InsertAfter($Proofread_text)
<#Handle misspelled words here#>
foreach($spell_error in $textrange.SpellingErrors){
Write-Host $spell_error.Text
}
$Document.Close(0)
$Word.Quit()
The output will be:
>>lazie
>>jumpss
>>over
I found it helpful to disable the language autodetection in word before starting the script. Especially when you plan to switch the language.