简体   繁体   中英

How make “Choose location to save dialog in VBA Powerpoint”

I have searched for a method to save current slide as a picture in Slideshow mode, when pressing an action button on the slide. This is what I came up with in the end:

`Sub SaveCurrentSlideAsJpg()`
Dim imagePath As String  
Dim slideNum As Integer
 imagePath = "C:\Users\XXXXX\Pictures\Slides\"
 slideNum = ActivePresentation.SlideShowWindow.View.Slide.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
  ActivePresentation.SlideShowWindow.View.Slide.Export _
    FileName:=imagePath & ActivePresentation.Name & "_" & slideNum & ".jpg", _
    FilterName:="JPG"

End Sub

This is fine except that it saves to a default location on my PC. I will be giving this to other people and I need a dialog that pops up every time they try to save asking them where to save. All my attempts incorporate the Saveas dialog were unsuccessful. I would appreciate if anyone could help me with this. Thanks

You can use this function to select a folder:

Function BrowseForFolder(dialogTitle As String) As String
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    With fd
        .Title = dialogTitle
        .Show
        If .SelectedItems.Count = 0 Then
            ' Browsing cancelled by user.
            BrowseForFolder = ""
        Else
            BrowseForFolder = .SelectedItems.Item(1)
        End If
    End With
End Function

So instead of hard-coding imagePath , you would do this:

imagePath = BrowseForFolder("Save image in this folder...")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM