Consulting

Results 1 to 9 of 9

Thread: VBA Export PDF Help

  1. #1

    VBA Export PDF Help

    Hey Everyone

    I'm very new to VBA used excel a fair bit and can record Marcos ect... the easy stuff but I need to create a button the selects a range and then exports it to a PDF. Obviously you can do this just recording a macro but every time I do this it exports the range to a particular path and filename.... What I want the user to be able to do is select there own path and filename essentially so it stays at the export save screen. I've tried so many ways and just get errors don't really know what I'm doing lol. Even asked ChatGTP! I've seen alot of videos using the SaveAs Function that can even be customised, this would be great but as far as i can tell this doesn't work for what I am looking for! any help would be great.

    TIA

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    414
    Location
    Post the code you have now.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ' Macro12 Macro
        Sheets("Sheet1").Select
        Range("A1:X40").Select
        Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\craig\OneDrive - Van Elle\Desktop\1231.pdf", Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=True
    End Sub
    Last edited by Aussiebear; 01-13-2025 at 12:29 AM. Reason: Added code tags

  4. #4
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    414
    Location
    Try this:
    Sub RangeToPDF()
        Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        GetFolder() & "\" & InputBox("Enter text to use as file name without extension", "Enter text", "MyFileName") & ".pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
    End Sub
    
    
    Function GetFolder() As String
        Dim fldr As FileDialog
        Dim sItem As String
        Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
        With fldr
            .Title = "Select a Folder"
            .AllowMultiSelect = False
            .InitialFileName = Application.DefaultFilePath
            If .Show <> -1 Then GoTo NextCode
            sItem = .SelectedItems(1)
        End With
        NextCode:
        GetFolder = sItem
        Set fldr = Nothing
    End Function
    Last edited by Aussiebear; 01-12-2025 at 01:41 PM.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    hi this works wondered if it could be 1 single window instead of 2? (like the standard save or export window) also if you press cancel error appears is there anyway this not showing up. Thanks so much for your reply

  6. #6
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    622
    Location
    This question has been posted on two additional forums. The OP should check those other forums for the answer he seeks.

    Also in the future, when posting the same question to more than one Forum it is a requirement of all forums that you also provide links to those other locations where your question has been posted.

  7. #7
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    414
    Location
    AFAIK, export cannot select folder and allow file name input in one window. That would be a SaveAs operation and doesn't seem that SaveAs can save just a range.

    As for the Cancel, pull calling the file dialog function out of the Export command and set a variable. Use an If Then condition to determine if variable has value. Use that variable in the Export command

    strFile = GetFolder()
    If strFile <> "" Then
        'code to export
    End If
    Last edited by Aussiebear; 01-13-2025 at 12:38 AM.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    622
    Location
    June7 : The answer to his question is posted on two other forums. Just so you know.

  9. #9
    Administrator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,278
    Location
    Hi Spaceman88,

    Welcome to the forum, if you have posted the same question on multiple forums then it may be worth taking a look at the below link. The rules here are the same as most of he other forums, take a read of the below otherwise you may have issues on all of the forums you frequent.

    https://excelguru.ca/a-message-to-forum-cross-posters/

    Regards

    George
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2408, Build 17928.20080

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •