Hi!
I’m using Impress version 25.2.1.2.
I give English online classes and I’m using Impress to explain grammar. I often have to jump to ‘general rules’ to show why something is as it is and then jump back to the last slide I presented.
After some extensive search, I understand that this isn’t possible out of the can and I tried to create such a functionality using macros, which seems to be over my head.
I have cobbled this code together and annotated the different functions:
' === GLOBALS ===
Dim gLastSlideIndex As Integer
Dim gIsInitialized As Boolean
Const SHOW_DEBUG As Boolean = True ' Set to False when done testing
' === TRACK CURRENT SLIDE BEFORE JUMPING TO INDEX ===
Sub TrackCurrentSlide
On Error Resume Next
Dim oPres As Object, oController As Object, oShow As Object
Dim nIdx As Integer
oPres = ThisComponent
oController = oPres.getCurrentController()
If oPres.Presentation.IsRunning Then
oShow = oController.getFrame().getController()
nIdx = oShow.getCurrentSlideIndex()
Else
nIdx = oController.getCurrentPage().getPropertyValue("Number") - 1
End If
gLastSlideIndex = nIdx
gIsInitialized = True
If SHOW_DEBUG Then
MsgBox "Tracking current slide: " & (gLastSlideIndex + 1), vbInformation, "DEBUG"
End If
End Sub
' === BUTTON: GO TO INDEX SLIDE (e.g. SLIDE 1) ===
Sub GoToIndexWithTracking
On Error Resume Next
TrackCurrentSlide
Dim oShow As Object
oShow = ThisComponent.getCurrentController().getFrame().getController()
If Not oShow Is Nothing Then
oShow.gotoSlideIndex(0) ' Slide 1 = index 0
Else
MsgBox "Unable to access slideshow controller.", vbExclamation, "Navigation Error"
End If
End Sub
' === BUTTON: GO BACK TO LAST RECORDED SLIDE ===
Sub GoToPreviousSlide
On Error GoTo ErrorHandler
If Not gIsInitialized Then
MsgBox "First use – nothing to go back to.", vbExclamation, "NAVIGATION"
Exit Sub
End If
Dim oPres As Object, oController As Object, oDrawPages As Object
Dim oFrame As Object, oShow As Object
Dim nTargetIdx As Integer, nCurrentIdx As Integer
Dim sDebug As String, bIsSlideShow As Boolean
oPres = ThisComponent
If oPres Is Nothing Then ExitWithError("No active presentation found")
oController = oPres.getCurrentController()
oDrawPages = oPres.getDrawPages()
If oDrawPages.getCount() = 0 Then ExitWithError("Presentation contains no slides")
Set oFrame = oController.getFrame()
Set oShow = oFrame.getController()
bIsSlideShow = (Not oShow Is Nothing)
If bIsSlideShow Then
nCurrentIdx = oShow.getCurrentSlideIndex()
Else
nCurrentIdx = oController.getCurrentPage().getPropertyValue("Number") - 1
End If
nTargetIdx = gLastSlideIndex
If nTargetIdx < 0 Or nTargetIdx >= oDrawPages.getCount() Then
MsgBox "Invalid stored slide index.", vbCritical, "NAVIGATION ERROR"
Exit Sub
End If
If SHOW_DEBUG Then
sDebug = "DEBUG INFO:" & vbCrLf & _
"Current Slide: " & (nCurrentIdx + 1) & vbCrLf & _
"Last Stored Slide: " & (nTargetIdx + 1) & vbCrLf & _
"Presentation Mode: " & bIsSlideShow
MsgBox sDebug, vbInformation, "DIAGNOSTICS"
End If
If nCurrentIdx = nTargetIdx Then
MsgBox "You're already on the last tracked slide.", vbInformation, "NO ACTION"
Exit Sub
End If
If bIsSlideShow Then
oShow.gotoSlideIndex(nTargetIdx)
If oShow.getCurrentSlideIndex() <> nTargetIdx Then
Dim dispatcher As Object
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(oFrame, ".uno:GoToSlide", "", 0, Array())
End If
If oShow.getCurrentSlideIndex() <> nTargetIdx Then
oShow.gotoSlideIndex(nTargetIdx)
End If
Else
oController.setCurrentPage(oDrawPages.getByIndex(nTargetIdx))
End If
Dim nVerified As Integer
If bIsSlideShow Then
nVerified = oShow.getCurrentSlideIndex()
Else
nVerified = oController.getCurrentPage().getPropertyValue("Number") - 1
End If
If SHOW_DEBUG Then
If nVerified = nTargetIdx Then
MsgBox "✅ Navigation succeeded. Now on slide: " & (nVerified + 1), vbInformation, "SUCCESS"
Else
MsgBox "❌ Navigation failed!" & vbCrLf & _
"Expected: " & (nTargetIdx + 1) & vbCrLf & _
"Actual: " & (nVerified + 1), vbCritical, "ERROR"
End If
End If
If Not oFrame Is Nothing Then oFrame.getContainerWindow().repaint()
Exit Sub
ErrorHandler:
MsgBox "Critical error during navigation!" & vbCrLf & _
"Error " & Err.Number & ": " & Err.Description, vbCritical, "ERROR"
End Sub
' === ERROR MESSAGE HANDLER ===
Sub ExitWithError(sMessage As String)
MsgBox "FATAL ERROR: " & sMessage, vbCritical, "TERMINATING"
End
End Sub
As far as I understand it, I have to start with the sub TrackCurrentSlide
to register my last slide used, then invoke using a ‘button’ the sub GoToIndexWithTracking
. I always have the general rules at the position ‘slide 1’, which makes it easier to cut it out when converting to a pdf.
The sub GoToPreviousSlide
should lead me back to the originating slide. I tried to track the progress using debug messages.
There are two problems I can’t overcome:
- When invoking
GoToIndexWithTracking
, the slide doesn’t change when in presentation mode. - Jumping back to the originating slide is mostly wrong. It misses normally by one slide before or after.
Are there any wizards in this forum which could help me out with this? I wasn’t able to find a solution anywhere and maybe I’m just trying to invent the wheel again, in this case, please point me to an existing solution.
Thank you!