Your code isn't working because the Application.OnTime method isn't available in PowerPoint VBA; it's specific to Excel VBA. Additionally, the property Loaded you're trying to access may not be valid for the Power BI report object in PowerPoint. Here's how you can modify your code to achieve the desired functionality:
Solution:
-
Replace Application.OnTime with a Loop and Delay:
Since Application.OnTime doesn't exist in PowerPoint VBA, you can use a loop with a delay to periodically check if the Power BI report has loaded.
-
Use the Sleep Function for Delays:
You can use the Windows API Sleep function to create delays without freezing the application.
-
Check the Validity of the Loaded Property:
Ensure that the Loaded property is valid for your Power BI report object. If not, you may need to find an alternative property or method to determine if the report has finished loading.
Revised Code:
First, declare the Sleep function at the top of your module:
#If VBA7 Then
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
Then, update your macros as follows:
Sub Auto_Open()
' Start checking if the Power BI report has finished loading
CheckPowerBIReport
End Sub
Sub CheckPowerBIReport()
Do
If IsPowerBIReportLoaded() Then
' Switch to Reading View
CommandBars.ExecuteMso "ReadingView"
Exit Do
Else
' Wait for 1 second
Sleep 1000 ' Sleep for 1000 milliseconds (1 second)
DoEvents ' Keep PowerPoint responsive
End If
Loop
End Sub
Function IsPowerBIReportLoaded() As Boolean
On Error Resume Next
' Get the Power BI report shape
Dim pbReport As Shape
Set pbReport = ActivePresentation.Slides(1).Shapes("Power BI Report")
If pbReport Is Nothing Then
IsPowerBIReportLoaded = False
Else
' Attempt to interact with the OLE object to check if it's loaded
Dim oleObj As Object
Set oleObj = pbReport.OLEFormat.Object
If oleObj Is Nothing Then
IsPowerBIReportLoaded = False
Else
' Replace the following line with the appropriate property or method
' to check if the Power BI report has finished loading.
' This may involve accessing a specific property or method provided
' by the Power BI OLE object.
IsPowerBIReportLoaded = True ' Assuming it's loaded for this example
End If
End If
End Function
Notes:
- 64-bit Compatibility: The
PtrSafe keyword and LongPtr data type ensure compatibility with both 32-bit and 64-bit versions of Office.
- Adjust the Loading Check: Replace the placeholder
IsPowerBIReportLoaded = True with the actual condition that accurately checks if your Power BI report has finished loading. This might require accessing specific properties or methods provided by the Power BI OLE object.
- Shape Naming: Ensure that the shape name
"Power BI Report" matches exactly the name of the shape in your slide. You can check or set the name by selecting the shape in PowerPoint and using the Selection Pane (Home tab ➔ Select ➔ Selection Pane).
Alternative Simplification:
If you cannot reliably check whether the Power BI report has loaded, you might consider using a fixed delay before switching to Reading View:
Sub Auto_Open()
' Wait for 10 seconds to allow the report to load
Sleep 10000 ' Sleep for 10000 milliseconds (10 seconds)
DoEvents ' Keep PowerPoint responsive
' Switch to Reading View
CommandBars.ExecuteMso "ReadingView"
End Sub
Explanation:
- The
Sleep function pauses the code execution for the specified number of milliseconds.
DoEvents allows PowerPoint to remain responsive during the delay.
- By using a fixed delay, you assume that the Power BI report will load within that time frame.
Conclusion:
PowerPoint VBA doesn't support Application.OnTime, so using a loop with Sleep and DoEvents is a practical alternative. Ensure you have the correct name for your Power BI report shape and replace any placeholder code with the actual logic required to determine if your report has loaded.