Buttons to save as .pdf and as .xlsx RRS feed

  • General discussion

  • Hello everyone,

    I have used the following code to save as pdf but what I would like to do something similar not exactly that (found the code in here Macro to create Excel data to PDF file. )

    Sub SavePDF()
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
            Filename:="C:\PDFs\No." & _
            ActiveSheet.Range("AY8").Value & ".pdf", _
    Worksheets("Sheet1").Range("A1:I50").ExportAsFixedFormat _
    End Sub

    - What I want is saving the file automatically by getting the name from three cells. For example K10, A10, K12 and if the name already exists in that folder than automatically put an underscore on the end or something similar.
    - Furthermore I would like to save data only from Sheet 1 (My Sheet name is: Testing) and page 1 and not the whole sheet. Is that possible?
    - Moreover, make the file as read only
    - To end, as title says, I would like to add another button to save the sheet that I am working at as it is and not the whole workbook using the same customization as for the pdf. 

    Thank you in advance.

    Tuesday, January 13, 2015 2:38 PM

All replies

  • Hi Ioannis,

    Base on your code, it is used to export the current sheet to pdf.

    To save specify sheet to a different file, we could use Worksheet.SaveAs method.

    To check whether file is exist, we could use Dir method.

    If Dir([file path]) <> "" Then
        MsgBox "File exists."
        MsgBox "File doesn't exist."
    End If

    To make the file as read only, we could use worksheet.Protect to lock the worksheet. If someone want to change the data, he needs the password. The Workbook.Protect method can protect whole workbook.



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 14, 2015 8:14 AM