+ Reply to Thread
Results 1 to 12 of 12

macro to save as PDF

  1. #1
    Registered User
    Join Date
    12-29-2015
    Location
    Essex, England
    MS-Off Ver
    2015
    Posts
    6

    macro to save as PDF

    Can anyone help with why this macro is not saving my file as a PDF?

    Sub Save()
    Dim Path As String
    Dim FileName1 As String
    Dim FileName2 As String
    Path = "C:\Users\Mellissa Matthews\Desktop\Dog works\"
    FileName1 = Range("B9")
    FileName2 = Range("B10")
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & FileName1 & FileName2 & ".pdf", OpenAfterPublish:=True

    End Sub


    Thanks

    Pete

  2. #2
    Registered User
    Join Date
    10-18-2015
    Location
    Lisbon, Portugal
    MS-Off Ver
    2010
    Posts
    38

    Re: macro to save as PDF

    Greetings,

    What is the text/value of the cells B9 and B10 and what exactly do you want from both ranges?

    Regards,
    Daniel

  3. #3
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: macro to save as PDF

    Just the destination filename


    Please Login or Register  to view this content.
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: macro to save as PDF

    Welcome to the forum!

    It should have shown an error if the path does not exist. I like to check for it first. See the Immediate Window for output from a run where Debug.print was used. Also note what you filename resolves to.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    12-29-2015
    Location
    Essex, England
    MS-Off Ver
    2015
    Posts
    6

    Re: macro to save as PDF

    Hi Daniel,
    B9 is a date and B10 is a name. Both come from formulae.

    Pete

  6. #6
    Registered User
    Join Date
    12-29-2015
    Location
    Essex, England
    MS-Off Ver
    2015
    Posts
    6

    Re: macro to save as PDF

    Any idea why the filename doesn't work? I can get a fixed file name to work.

  7. #7
    Registered User
    Join Date
    12-29-2015
    Location
    Essex, England
    MS-Off Ver
    2015
    Posts
    6

    Re: macro to save as PDF

    Hi Kenneth, thanks for the welcome!

    I'm out of my depth with what you showed me! The file does save in the correct folder if I have a fixed name, but when I try to be clever it does not, would it have anything to do with the quotes?

    Pete

  8. #8
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: macro to save as PDF

    No, quotes are not the issue. It is the subfolder path that is the issue I suspect.

    Try this and then comment out the 3rd Path line and uncomment the 2nd Path line and run as the 2nd Path will always exist.

    Of course I assumed that the filename in your cells creates a valid filename. They can not have quotes or other illegal filename characters. If you uncomment the Debug.Print line, you can see the built filename in the Immediate Window. Make sure that string for the filename is legal. I guess one could force a background write to a text file using that path to see if the filename is valid but with no On Error line, it should throw an error if filename is not valid.

    Please Login or Register  to view this content.
    Last edited by Kenneth Hobson; 12-29-2015 at 12:32 PM.

  9. #9
    Registered User
    Join Date
    12-29-2015
    Location
    Essex, England
    MS-Off Ver
    2015
    Posts
    6

    Re: macro to save as PDF

    OK so that worked, thanks, it now saves it to the desktop, but how would I get it to save into the Dog works folder on the desktop? Sorry if I'm being dopey!

  10. #10
    Registered User
    Join Date
    10-18-2015
    Location
    Lisbon, Portugal
    MS-Off Ver
    2010
    Posts
    38

    Re: macro to save as PDF

    Greetings,

    I think this code worked but you just needed to add ".value" at the end of the range:

    Please Login or Register  to view this content.
    To create the folder, put this piece of code below:

    Please Login or Register  to view this content.
    after Mr. Kenneth's code below:

    Please Login or Register  to view this content.
    However Mr.Kenneth Hobson has the right code to check the existence of the folder and it is a very cool snipet to use in the future

  11. #11
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: macro to save as PDF

    If it does not exist, then make it manually or make Excel make it. If you need help with the latter, see MkDir(). I can show you that method or a Shell() method if needed. In this case, MkDir() should suffice. You just need to modify the If() structure to do the check first and then MkDir Path for the Then case. The Export would be after the If() structure.
    Last edited by Kenneth Hobson; 12-29-2015 at 12:51 PM.

  12. #12
    Registered User
    Join Date
    12-29-2015
    Location
    Essex, England
    MS-Off Ver
    2015
    Posts
    6

    Re: macro to save as PDF

    Thanks to all for your help. Got it working now, and learnt a little more about how macros work.

    Thanks again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Need to loop a macro to save images to file - but save to user defined directory.
    By superfurry in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-19-2015, 07:49 AM
  2. Macro to save worksheet doesnt save correctly
    By 1967chris in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-27-2015, 10:19 PM
  3. Macro to Save current XLS, then to save as a CSV. Then close CSV and reopen XLS
    By cricker in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-02-2014, 06:17 AM
  4. Macro to Auto save a backup copy in a seperate location OR save file with a pop up
    By kdsanderson30 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-12-2014, 12:38 PM
  5. how to specify a file save path to a save macro
    By Solidstan in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-04-2013, 05:33 PM
  6. Macro with cell reference as a name, but prompt for save location and save as csv
    By tomham in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-15-2012, 06:21 PM
  7. where can i find macro codes for various functions such print, save and save as
    By fellayaboy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-23-2011, 02:06 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1