单击操作按钮,在幻灯片显示视图中触发时VBA不会运行

时间:2015-01-14 15:04:34

标签: vba button action powerpoint powerpoint-vba

我创建了一个简短的VBA代码,以JPEG格式导出幻灯片视图中的当前幻灯片。当幻灯片正在运行时,它从VBA运行时有效。它不适用于幻灯片上的操作按钮。

Sub SaveCurrentSlideAsJpg()    
Dim imagePath As String
Dim slideNum As Integer   
imagePath = "C:\Users\####\Pictures\Slides\"
slideNum = ActiveWindow.Selection.SlideRange(1).SlideIndex  
' first check if this already exists then delete it    
If Dir(imagePath & ActivePresentation.Name & "_" & slideNum & ".jpg") <> ""Then    
    Kill imagePath & ActivePresentation.Name & "_" & slideNum & ".jpg"    
End If    
' now save the slide    
 ActiveWindow.View.Slide.Export _    
 FileName:=imagePath & ActivePresentation.Name & "_" & slideNum & ".jpg", _ 
 FilterName:="JPG"   
End Sub

1 个答案:

答案 0 :(得分:0)

评论内联解释了您出错的地方,并提出了一些建议以避免将来出现问题:

Sub SaveCurrentSlideAsJpg()

Dim imagePath As String
'Dim slideNum As Integer
Dim slideNum As Long
Dim oSld As Slide

' temporarily to suit my testing neeeds:
' imagePath = "C:\Users\####\Pictures\Slides\"
imagePath = "C:\Temp\"

' You can't select anything during a slide show so there can be
' no .Selection object
'slideNum = ActiveWindow.Selection.SlideRange(1).SlideIndex
Set oSld = SlideShowWindows(1).View.Slide
slideNum = oSld.SlideIndex

' first check if this already exists then delete it
' Safer to convert the long to a string before appending it to other strings:
'If Dir(imagePath & ActivePresentation.Name & "_" & slideNum & ".jpg") <> "" Then
If Dir(imagePath & ActivePresentation.Name & "_" & CStr(slideNum) & ".jpg") <> "" Then
    Kill imagePath & ActivePresentation.Name & "_" & CStr(slideNum) & ".jpg"
  End If
' now save the slide
' use our previously declared slide object instead and convert slideNum to string
 'ActiveWindow.View.Slide.Export
 ' NOTE: you might also want to make sure that the presentation's been saved
 ' at least once, else .Name will be blank and you'll get files like _1.jpg
 oSld.Export _
 FileName:=imagePath & ActivePresentation.Name & "_" & CStr(slideNum) & ".jpg", _
 FilterName:="JPG"
End Sub