Create PowerPoint Document - powershell

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)

Related

Dialog for browsing and select folders

In order to render more user-friendly my script I would like to load a dialog in which a complete folder tree (from the C:\ root) is browsable and where I could select multiple (sub)folders through check boxes. How can I accomplish such task?
P.S.: the best solution should also include hidden folders
Edit: after the evaluation of the comments and the early reply I will clarify what I am looking for. I would like to create a dialog in which the folder tree is showed, where I can expand or collapse the branches and where I can select parent folders and/or some of the children/leaves (subfolders and/or files) at the same time.
By using New-object System.Windows.Forms.OpenFileDialog and change
$FormOpenFileDialog.Multiselect = $False to True you have a File-browser that can select several folders.
$FormOpenFileDialog = New-object System.Windows.Forms.OpenFileDialog
$FormOpenFileDialog.initialDirectory = ""
$FormOpenFileDialog.Title = ""
$FormOpenFileDialog.Filter = ""
$FormOpenFileDialog.Multiselect = $True
$FormOpenFileDialog.showHelp = $true
$FormOpenFileDialog.ShowDialog()
$ApplicationForm.Controls.Add($FormOpenFileDialog)
By Adding this to a Form, binded to a button maby is the best solution ?
$ApplicationForm = New-Object System.Windows.Forms.Form
$BtnOpenFileDialog = New-object System.Windows.Forms.Button
$BtnOpenFileDialog.text= "Open Folder"
$BtnOpenFileDialog.location = "50,50"
$BtnOpenFileDialog.size = "80,20"
$BtnOpenFileDialog.Add_Click({ GenerateFormOpenFileDialog })
$ApplicationForm.Controls.Add($BtnOpenFileDialog)
function GenerateFormOpenFileDialog { # Start GenerateFormOpenFileDialog
$FormOpenFileDialog = New-object System.Windows.Forms.OpenFileDialog
$FormOpenFileDialog.InitialDirectory = ""
$FormOpenFileDialog.Title = ""
$FormOpenFileDialog.Filter = ""
$FormOpenFileDialog.Multiselect = $True
$FormOpenFileDialog.ShowHelp = $true
$FormOpenFileDialog.ShowDialog()
} # End GenerateFormOpenFileDialog
# Initlize the form
$ApplicationForm.Add_Shown({$ApplicationForm.Activate()})
[void] $ApplicationForm.ShowDialog()
Why Filebrowser and not folderbrowserdialog ?
folderbrowserdialog has more limitations.
FileBrowser
FolderBrowser
If you want to create a form, Sapians has a great forum for this.
https://www.sapien.com/
https://www.sapien.com/forums/

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

Powershell script to convert a word table to text

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

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

How can i speed up populating a table in Microsoft Word using powershell?

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