這有點長,但我已將代碼包含在常規模塊中,也包含User_Form
。
代碼模塊
Option Explicit
Public PPTFileName As String '<-- defined as Public, will get it from the User_Form's ListBox
Sub SelectPPTPresentation()
' === this loop through all open PowerPoint Presentations is using Late Binding
' === to avoid future problems when working with multiple Office Versions
Dim ppApp As Object
Dim ppPres As Object
Dim ObjPres As Object
If MsgBox("Is the PowerPoint already open?", vbYesNo + vbQuestion) = vbYes Then
On Error Resume Next
Set ppApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
If ppApp Is Nothing Then
MsgBox "No PowerPoint is open!"
Else
If ppApp.Presentations.Count > 0 Then ' check that at least 1 Presentation is open
For Each ObjPres In ppApp.Presentations ' loop through all open presnetations (
UserForm1.OpenPPPres_LB.AddItem ObjPres.FullName '<-- add their full names to the User_Form ListBox
Next ObjPres
End If
End If
UserForm1.Show '<-- show the User_Form with the ListBox of all open PPT presentations
' loop through all open presnetations (check Full Name: Path and name)
For Each ObjPres In ppApp.Presentations
If StrComp(ObjPres.FullName, PPTFileName, vbTextCompare) = 0 Then
Set ppPres = ObjPres ' <-- set the current PPT pres to the selected Item from the ListBox
Exit For
End If
Next ObjPres
MsgBox "Selected Presentation is " & ppPres.Name ' <-- just for confirmation >> show Name (without Path)
Else ' <-- you will need to modify this section to fit the upper section
' MsgBox ("Please choose PowerPoint to open.")
' 'openDialog is a function I have already created
' pptName = openDialog()
' Set myPres = ppt.Presentations.Open(pptName)
'
End If
End Sub
User_Form代碼事件是根據 「OpenPPPres_LB」 列表框DblClick
事件:
Private Sub OpenPPPres_LB_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim i As Long
For i = 0 To OpenPPPres_LB.ListCount - 1
If OpenPPPres_LB.Selected(i) Then
PPTFileName = OpenPPPres_LB.List(i) ' <-- save the PPT filename
Exit For
End If
Next i
Unload UserForm1
End Sub
的User_Form填充的屏幕截圖目前的Open Presentations:
循環儘管所有打開的演示文稿,讓他們所有人的名字,並讓使用選擇一個 –
謝謝!我編輯了包含所有打開的PowerPoint名稱數組的代碼。我如何讓用戶選擇其中之一? – BH57
構建一個帶有數組的ListBox的小型user_form,並且用戶從列表框中選擇某個項目將其設置爲需要演示文稿 –