Home > Back-end >  Powershell PowerPoint Header
Powershell PowerPoint Header

Time:10-23

I have a simple script that creates a Word Document and a PowerPoint document for staff that are sitting an exam where the files needed are pre loaded to a user area on a file share. The Word Document opens an creates the document header with the line of code

$Header = $Section.Headers.Item(1);
$Header.Range.Text = "$FirstName $SecondName $ID Activity 1";

PowerPoint is a bit more funnier and when you add a header it asks for dates. Is there a way to do this with powershell? We are printing them afterwards so would be a lot more benificial to have the variables that are in the CSV printed in the header of the document.

If this isn't doable is there a way to edit the first slide to have the variables in the first slide of the PowerPoint.

If anyone is able to look at the code or any ways of simplifying it, it would be massively appreiciated

The CSV is listed below and the full script is at the bottom :)

ID  Four    First   Second
219999  9999    Tech    Support

Have a good day

$Exams = Import-Csv "C:\\techtest.csv"

$fileserver = "C:\\ExamHomes\"

foreach ($User in $Exams)
{
$FirstName = $User.First
$SecondName = $User.Second
$ID = $User.ID
$FourID = $User.Four
$Time = "am"
$Date = "1511"

$Word = New-Object -ComObject Word.Application;
$Word.Visible = $false;
$Doc = $Word.Documents.Add();
$Section = $Doc.Sections.Item(1);
$Header = $Section.Headers.Item(1);
$Header.Range.Text = "$FirstName $SecondName $ID Activity 1";
$Doc.SaveAs("$fileserver\${date}${time}-${FourID}\Desktop\activity 1_${ID}_${FirstName}_${SecondName}.docx");
$Word.Quit()

Write-Host "File 'activity 1_${ID}_${FirstName}_${SecondName}.docx' for $FirstName $SecondName has been created and sent to the folder ${date}${time}-${FourID}" -BackgroundColor Black -ForegroundColor Cyan

add-type -assembly microsoft.office.interop.powerpoint
$Application = New-Object -ComObject powerpoint.application
$slideType = "microsoft.office.interop.powerpoint.ppSlideLayout" -as [type]
$presentation = $application.Presentations.add()
$presentation.SaveAs("$fileserver\${date}${time}-${FourID}\Desktop\activity 1_${ID}_${FirstName}_${SecondName}.pptx")
$presentation.close()
Stop-Process -name POWERPNT -Force

Write-Host "File 'activity 1_${ID}_${FirstName}_${SecondName}.pptx' for $FirstName $SecondName has been created and sent to the folder ${date}${time}-${FourID}" -BackgroundColor Black -ForegroundColor Cyan
}

Write-Host "All Staff have successfully had their document deployed for the exam" -BackgroundColor Black -ForegroundColor Red

CodePudding user response:

Headers are only available for Notes Master and Handout Master objects But you can add a Footer on a slide.

Add-Type -AssemblyName microsoft.office.interop.powerpoint
$Application = New-Object -ComObject powerpoint.application
$presentation = $application.Presentations.add()
$slide = $presentation.Slides.Add(1, [Microsoft.Office.Interop.PowerPoint.PpSlideLayout]::ppLayoutTitle)
$slide.Shapes.Title.TextFrame.TextRange.Text = "My Title"
$slide.Shapes(2).TextFrame.TextRange.Text = "My SubTitle"
$slide.HeadersFooters.Footer.Visible = $true
$slide.HeadersFooters.Footer.Text = "My Name"

You can use some Text on the DateAndTime HeaderFooter object like this:

$slide.HeadersFooters.DateAndTime.Visible = $true
$slide.HeadersFooters.DateAndTime.UseFormat = $true
$slide.HeadersFooters.DateAndTime.Format = [Microsoft.Office.Interop.PowerPoint.PpDateTimeFormat]::ppDateTimeFigureOut
$slide.HeadersFooters.DateAndTime.Text = "Coucou"

EDIT: SLIDE MASTER In fact this can be defined on the SlideMaster so that every Slide inherit the values from the footer, pagenumber or datetime. This is defined at the presentation level. So depending what you want do it on the SlideMaster or on any Slide you create. Probably a Slide may override the values from the SlideMaster.

$presentation.SlideMaster.HeadersFooters.SlideNumber.Visible = $true
$presentation.SlideMaster.HeadersFooters.Footer.Visible = $true
$presentation.SlideMaster.HeadersFooters.Footer.Text = "My Name"

$presentation.SlideMaster.HeadersFooters.DateAndTime.Visible = $true
$presentation.SlideMaster.HeadersFooters.DateAndTime.UseFormat = $true
$presentation.SlideMaster.HeadersFooters.DateAndTime.Format = [Microsoft.Office.Interop.PowerPoint.PpDateTimeFormat]::ppDateTimeFigureOut
$presentation.SlideMaster.HeadersFooters.DateAndTime.Text = "Something"
  • Related