Results 1 to 14 of 14
  1. #1
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Open .ppsx file with Word VBA

    I've been struggling with this for a while...

    I have a .ppsx file I would like to open using Word VBA (2013). Every attempt opens the file in edit mode...

    I would like the file to open in SlideShow mode (or even kiosk mode) ONLY - no edit mode visible (not even minimized).

    I have attempted to use a shell command but can't get the syntax right... the thing is that the name of the ppsx file is a variable found in "control.tag".

    Could somebody please tell me how to do this?

    Thank you!
    Stylus

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    This seems to work OK for me:
    Code:
    Sub Demo()
    Application.ScreenUpdating = True
    Dim PwPtApp As Object, PwPtShow As Object, StrFlNm As String, bStrt As Boolean, bFound As Boolean
    StrFlNm = "C:\Users\" & Environ("Username") & "\Documents\Presentations\Introduction to PowerPoint\Powerpoint.ppsx"
    If Dir(StrFlNm) = "" Then
      MsgBox "Cannot find the designated Presentation: " & StrFlNm, vbExclamation
      Exit Sub
    End If
    ' Test whether PowerPoint is already running.
    On Error Resume Next
    bStrt = False ' Flag to record if we start PowerPoint, so we can close it later.
    Set PwPtApp = GetObject(, "PowerPoint.Application")
    'Start PowerPoint if it isn't running
    If PwPtApp Is Nothing Then
      Set PwPtApp = CreateObject("PowerPoint.Application")
      If PwPtApp Is Nothing Then
        MsgBox "Can't start PowerPoint.", vbExclamation
        Exit Sub
      End If
      ' Record that we've started PowerPoint.
      bStrt = True
    End If
    On Error GoTo 0
    'Check if the Presentation is open.
    bFound = False
    With PwPtApp
      For Each PwPtShow In .Presentations
        If PwPtShow.FullName = StrFlNm Then ' It's open
          Set PwPtShow = PwPtShow
          bFound = True
          Exit For
        End If
      Next
      ' If not open by the current user.
      If bFound = False Then
        ' Check if another user has it open.
        If IsFileLocked(StrFlNm) = True Then
          ' Report and exit if true
          MsgBox "The PowerPoint Presentation is in use." & vbCr & "Please try again later.", vbExclamation, "File in use"
          If bStrt = True Then .Quit
          Exit Sub
        End If
        ' The file is available, so open it.
        Set PwPtShow = .Presentations.Open(FileName:=StrFlNm, ReadOnly:=True)
        If PwPtShow Is Nothing Then
          MsgBox "Cannot open:" & vbCr & StrFlNm, vbExclamation
          If bStrt = True Then .Quit
          Exit Sub
        End If
      End If
      ' Process the Presentation.
      With PwPtShow
        With .SlideShowSettings
          .ShowType = 1 'ppShowTypeSpeaker
          .Run
        End With
        .Saved = True
      End With
    End With
    ' Release PowerPoint object memory
    Set PwPtShow = Nothing: Set PwPtApp = Nothing
    Application.ScreenUpdating = True
    End Sub
    
    Function IsFileLocked(strFileName As String) As Boolean
      On Error Resume Next
      Open strFileName For Binary Access Read Write Lock Read Write As #1
      Close #1
      IsFileLocked = Err.Number
      Err.Clear
    End Function
    Last edited by macropod; 2015-10-12 at 22:09. Reason: Code revision for late binding
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thank you Paul!

    When I run this I get two PPT windows: one with the speaker view and another with kiosk view that fills my second screen. If I manually close the SpeakerView window, then that plus the KioskView close, yet a new window becomes visible with the whole show in edit mode (on my primary screen).

    If I change ".showtype = 1" to ".ShowType = ppShowTypeWindow" then the presentation is in the format I want: a window on my main screen. However, the edit screen is still there in the background... And: when I experiment by manually closing the edit window then the presentation window is still open and functioning, but then if I close the presentation window then I am left with an empty PPT window...

    Strange...

    By the way I loved all your error traps. We may need one more: "If Universe is Nothing then..." after which of course there is no more code...

    PS. This may possibly be a factor: this macro is activated from a toolbar menu on a custom ribbon supplied by a global template. Both the global template and the PPT file are out on a server somewhere. The same menu shows a word file, and it opens with no problem (but then, of course Word is already open!)
    Stylus

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    I don't know why you'd be getting two PowerPoint windows - I only get the one, in Presenter view and, when I exit that, my PowerPoint session closes. Are you sure you didn't already have a PowerPoint session running? As you seem to imply, though, the code behind your custom ribbon may have something to do with the behaviour.

    As for "If Universe is Nothing then..." How about:
    If Universe is Nothing then CreateObject("Universe")
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Mmmm yes...

    Chapter one, verse one of the programmer's bible:

    If Universe is Nothing then CreateObject("Universe")

    Discovering that the universe is run by visual basic would explain many things...

    I will do some experimenting to see if the Global/server aspect has anything to do with this. I don't see how it could, but then again, this is Word we are dealing with...

    Another thing: my two screens are controlled by DisplayFusion... No issue with apps in the past 3 years, but there's always a first time. Do you have two screens active when you run this macro?

    As I write it occurs to me that I should also create a new ppsx file and make a note of exactly which settings I use... possibly something there.

    I will report back.
    Stylus

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by Stylus View Post
    Do you have two screens active when you run this macro?
    No, and it wasn't apparent you were, either. I also have no idea what DisplayFusion is - presumably some software that came with your graphics adaptor?
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  7. #7
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Ah, I can see that I was not explicit about the two screens.

    DisplayFusion is software that permits adjusting how two screens interact or don't interact. For example, behaving as one screen, or having different display settings on each, etc. When I installed it I got more options than I had with Windows 7; I don't know what Windows 10 offers (but I will be finding out, soon enough).

    I can create a ppsx file so that when I run it on my system (clicking on it) then the kiosk display shows either on the primary or secondary monitor. (Believe that's a built in feature of PPT, and nothing to do with DisplayFusion). I suspect that two screens is not an issue in this case.

    I will see what I can find out tomorrow.

    Time to go home here. Thank you for your continued interest!
    Stylus

  8. #8
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Ok...

    I'm running Windows 7 and office 2013. I took my laptop out of its dock, so it was not connected to the network at all, nor to any other screens. No network, single screen, DisplayFusion not involved at all.

    While in this laptop-only mode I opened PPT and created a Test.ppsx. (In other words, this test.ppsx was created in a single-screen environment).

    I copied your macro into an experimental template and modified the address where it looks for the file (to point to D: Documents). I opened Word and attached the template to the blank document.

    I ran the macro and it brought up test.ppsx as a window... with a window displaying the edit view behind it. Closed the edit window... slideshow window remained open. Used Esc to close the slide show, and was left with a PPT window showing no file.

    I am stumped.

    Is there a quick 'n dirty workaround that would close the edit view when the ppsx opens? Or, would using the shell commands that I see posted here and there (but can't get to work properly) be a better strategy?
    Stylus

  9. #9
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    I suspect the behaviour has something to do with your PowerPoint setup.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  10. #10
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Well, here is an interesting quote from David M. Marcovitz, PowerPoint MVP:

    http://www.techtalkz.com/microsoft-o...oint-show.html

    When you double-click on a PPS file it opens in Show View and exits PowerPoint
    entirely when the show is done. However, if you open a PPS from within
    PowerPoint, it opens in Normal/Edit View and returns to Normal/Edit View
    after running the show.

    Since the macro first creates a PPT object, I figure that the ppsx file is being opened from within PPT. I had sort of gotten to this understanding earlier, which is why I was struggling to understand and implement the syntax of a shell command.

    Yet when you run the macro it only opens the presentation... no edit mode window...

    May I ask you to see how a shell command to open the ppsx file would work?
    Stylus

  11. #11
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    This works fine for me:
    Code:
    Shell "PowerPnt /s ""C:\Users\%UserName%\Documents\Presentations\Introduction to PowerPoint\Powerpoint.ppsx""", vbNormalFocus
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  12. #12
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thank you - there has been progress!

    Your code worked in its original form. It did not work if the the name of the ppsx file contained spaces, or if it was received as a variable

    Making it digest a variable, and a variable containing a space, was another matter...

    So far this works:

    Sub RunShell()
    Dim strFname As String
    strFname = "D:\Documents\PowerPoint\Test Seven.ppsx"
    Shell "powerpnt /s " & Chr(34) & strFname & Chr(34), vbNormalFocus
    end sub

    The syntax changes I have added allows the shell function to digest strFname that is a variable populated elsewhere (in my case: "strFname = control.tag")

    However... a split second after the ppsx file displays, it jumps a couple of millimeters down towards the bottom of the screen and back up, and then carries on normally. I have a suspicion that the ppsx is started twice. Interestingly, if the ppsx file is created as a Kiosk display (full screen) there is no jump.

    These syntax changes don't mean that I have the slightest idea of what I'm doing - I found 'em suggested in a forum, experimented, and got them to work (finally...).

    Can you replicate the "jump", and if you can, can you figure out a cure?

    Thanks again for your continuing support!
    Stylus

  13. #13
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by Stylus View Post
    Your code worked in its original form. It did not work if the the name of the ppsx file contained spaces, or if it was received as a variable
    You'll note, though, that the code I posted used a file path with spaces! You'll also see that it had extra double quotes to enclose the file path/name. That's what your Chr(34) does.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  14. #14
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    A badly sprained ankle (interesting colors!) interrupted work for a while...

    Thank you for your help. The Chr(34) allows me to use control.tag instead of the string it contains.

    The PPT file does its little skip just after opening, but never mind. It works!
    Stylus

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •