3
votes
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName Office
Add-Type -AssemblyName Microsoft.Office.Interop.Powerpoint
#Getting Error - Add-Type -AssemblyName Office Microsoft.Office.Interop.Powerpoint.Slide.SlideShowTransition.Hidden

Function RunSlide {
    $pptx = "h:\Test.ppt"
    $osld = "Slide"
    [bool] $sldState = "Slide.SlideShowTransition.Hidden"
    $state = "False"

    $hiddenCount = 0
    $cc = 0

    $application = New-Object -ComObject powerpoint.application
    $presentation = $application.Presentations.open($pptx)

    $count = 1
    Start-Sleep -Seconds 3

    ForEach ($osld In $Presentation.Slides)
        {
        $cc = $cc + 1
        If ($sldState -eq "False") {$hiddenCount = $hiddenCount + 1; Write-Host "Slide State $sldState"}
        Write-Host "Hidden Count is $hiddenCount"

        $endCount = $presentation.Slides.Count
        Write-Host "Total slide count is $endCount"
        #Present Slide Show
        [System.Windows.Forms.SendKeys]::SendWait("{F5}")

        do
            {
            Start-Sleep -Seconds 6
            [System.Windows.Forms.SendKeys]::SendWait("{RIGHT}")
            $count = $count + 1
            Write-Host "Slide Count is $count"
            }
        while ($count -le $endCount)

        Write-Host "POWER POINT RESTARTING"
        Stop-Process -processname POWERPNT
    }
$b -eq 1
do  {
    $d = Get-Date
    Write-Host "Slide ran as at $d"

    CheckSlide
    RunSlide
    Start-Sleep -Seconds 3
    }
While ($b = 1)

So I'm restricted to using powershell (new work policy) and thus am adapting our existing wscript and vbscript files to Powershell.

We have an info wall that uses a hyperlink to a powerpoint presentation. It gets updated regularly so it needs to open, present then close and reopen.

So far I've managed to make this happen. But because of the amount of hidden slides and the nature of my code the script will sometimes run far longer than it has too (sometimes up to 40 slides).

My script checks if the POWERPOINT process is running, if not then it continues and opens the file. Then it will it do a slide count, and run the presentation for the length of the presentation (adds count until it reaches total).

Once finished it will stop the process and the Do loop at the bottom will open the file again. Rinse repeat.

The problem is, is that the Total slide count also counts hidden slides.

What I'm trying to achieve with a ForEach loop is for the script to look at each slide state, if hidden it adds to a count. This count is subtracted from the total and the Do loop uses this count to then present the correct amount of shown slides. Right now the ForEach loop is just reporting the count, not subtracting (because I can't get it to count the hidden amount yet)

I realise there maybe simpler methods for presentation but the translation from Vb to PS has done my head in, it's just plain not working! Getting some tunnel vision and the limited internet access we have is also hindering progress. If anyone can have a look and tell what's redundant and what I maybe missing that would be greatly appreciated. Thank you,

1
My first guess is this will always evaluate to $true: [bool] $sldState = "Slide.SlideShowTransition.Hidden" - Mike Shepard

1 Answers

2
votes

I don't understand how you suppose [bool] $sldState = "Slide.SlideShowTransition.Hidden" should work.

You are turning (casting) a string (always the same for all slides, in fact) into a boolean value ; this will always be evaluated to $true.

I'd suggest to remove that ligne and try this in the ForEach loop :

(...)

$cc++ # equivalent to $cc = $cc + 1
$sldState = $osld.SlideShowTransition.Hidden
if($sldState) { # equivalent to if($sldState -eq $true)
    $hiddenCount++
    Write-Host "Slide State $sldState"
}
Write-Host "Hidden Count is $hiddenCount"

(...)

$sldState will now, for each slide, get the SlideShowTransition.Hidden property on the current slide object $osld (seems correct according to MSDN).

I guess you could also simplify things by directly counting the non hidden slides, like this :

$presentation.Slides | ForEach-Object {
    if(-not $_.SlideShowTransition.Hidden) {
        $visibleSlidesCount++
    }
}
Write-Host "There are $visibleSlidesCount visible slides in this presentation"