4

I have added a button in excel sheet and added following codes in vba window of that button. Now when I click this button i.e. when I run the codes it saves the excel sheet in pdf form whose name it takes from cell no H8 and saves it at M:\formats. Moreover it also saves the same excel sheet in .xlsx format at M:\formats\excels. But here the problem is when I run the codes it closes the excel sheet in which I have added the codes and opens the file which is saved by the codes. For example I made abc.xlsm excel sheet and added the codes in vb window, now xyz is written in cell no h8 in abc.xlsm excel sheet, now when I will run the codes it closes abc.xlsm and all codes are shown in xyz.xlsx excel sheet. I want it should only save the file in xlsx format it requisite location. It should not close the base file (which is abc.xlsx in the above example) and should not open the saved file (which is xyz.xlsx in the above example). Moreover I want that the saved file (xyz.xlsx in the above example) should not contain any vba coding. In another words it should be just like the backup copy for the base file (which is abc.xlsx in the above example). Kindly help me in to modify these codes to them as I want. I will be highly obliged to you. Thanks

Sub ExportAPDF_and_SaveAsXLSX()

Dim wsThisWorkSheet As Worksheet
Dim objFileSystemObject As New Scripting.FileSystemObject

Dim strFileName As String
Dim strBasePath As String

strBasePath = "M:\formats\"
strFileName = Range("H8")

On Error GoTo errHandler

Set wsThisWorkSheet = ActiveSheet

wsThisWorkSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=strBasePath & strFileName, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False

MsgBox "PDF file has been created."
strBasePath = "M:\formats\excels\"
strFileName = Range("H8")

Application.DisplayAlerts = False

strFileName = objFileSystemObject.GetBaseName(strFileName) & ".xlsx"

wsThisWorkSheet.SaveAs Filename:=strBasePath & strFileName, 

FileFormat:=xlOpenXMLWorkbook

Application.DisplayAlerts = False

MsgBox "Workbook now saved in XLSX format."


    exitHandler:
    Exit Sub
    errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler

    End Sub
pnuts
  • 58,317
  • 11
  • 87
  • 139
ramji
  • 75
  • 1
  • 2
  • 8
  • [Workbook.SaveCopyAs Method (Excel)](https://msdn.microsoft.com/en-us/library/office/ff835014.aspx) – Siddharth Rout Sep 29 '15 at 09:57
  • @SiddharthRout - Although SaveCopyAs is a neat solution in some circumstances, it doesn't meet the OPs requirements. He's running VBA, but wants the file saved as an XLSX file. You can do this with SaveCopyAs, but that file will then error when you open it. That's why I suggested the (longer / more convoluted) Open-Original and Close-Current approach. Regards, Ian – EyePeaSea Sep 29 '15 at 11:10

1 Answers1

2

Here is the code, with just two small changes. Both new sets of lines have the comment "New" in front of them.

Also just tidied up the error handling routine a little bit.

The way it works is this:

  1. Store the filename of the current workbook in the variable 'strMasterWorkbookFilename'

  2. The PDF file is created by 'exporting' the worksheet.

  3. The Excel worksheet is then saved as an XLSX. This effectively 'closes' the original workbook.

3.1 The Button ("Button 8") is removed from the new XLSX worksheet and the workbook is saved again.

  1. The code then re-opens the original workbook ('strMasterWorkbookFilename') and closes the current workbook.

Notes - Saving as the XLSX will remove the Macro code from the saved file. The Macro will remain in the main 'master' file.

Sub ExportAPDF_and_SaveAsXLSX()

    Dim wsThisWorkSheet As Worksheet
    Dim objFileSystemObject As New Scripting.FileSystemObject

    Dim strFileName As String
    Dim strBasePath As String

    ' NEW
    Dim strMasterWorkbookFilename As String
    strMasterWorkbookFilename = ThisWorkbook.FullName

    strBasePath = "M:\formats\"
    strFileName = Range("H8")

    On Error GoTo errHandler

    Set wsThisWorkSheet = ActiveSheet

    wsThisWorkSheet.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=strBasePath & strFileName, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

        MsgBox "PDF file has been created."

        Application.DisplayAlerts = False
        strFileName = objFileSystemObject.GetBaseName(strFileName) & ".xlsx"
        wsThisWorkSheet.SaveAs Filename:=strBasePath & strFileName, FileFormat:=xlOpenXMLWorkbook
        wsThisWorkSheet.Shapes("Button 8").Delete
        ActiveWorkbook.Save

        Application.DisplayAlerts = False

        MsgBox "Workbook now saved in XLSX format."

        ' NEW
        Workbooks.Open strMasterWorkbookFilename
        Workbooks(strFileName).Close SaveChanges:=False


    exitHandler:
            Exit Sub
    errHandler:
            MsgBox "Error Saving file.  The error is " & vbCrLf & Chr(34) & Err.Description & Chr(34)

            Resume exitHandler

End Sub

Thanks for posting this as a new question. If I'd carried on modifying the original code in the first question, it would not have been useful for anyone else reading your original post.

EyePeaSea
  • 185
  • 9
  • Thanks a lot dear EyePeaSea. You are really great at vba. I wish i could be as skillful as you are at vba. Dear EyePeaSea i wish to add one more button in excel sheet to clear the contents of some specific cells when i press it. for example i add 'clear' button in excel sheet i want that when i press it, it should clear the contents of cell no I1, H8, F10, F13, F14, H16. It should only clear the values of these cell not the formatting, formulas or conditional formatting if any. please help me...thanks... – ramji Sep 29 '15 at 11:05
  • Hi Ramji. Glad I could be of help again. Firstly, if you're happy with my answer, please mark it as accepted (the Tick icon by my reply). As for your new question, I'm happy to answer, but again, please post this as a question. Stackoverflow works by helping everyone find a problem/solution and that is easier when different questions are posted separately. Regards, Ian – EyePeaSea Sep 29 '15 at 11:07
  • Dear EyePeaSea thanks for your reply. I have already marked your answer as accepted. Keeping your advise in mind, i will ask a new question. I hope you will help me again. Thanks a lot sir... – ramji Sep 29 '15 at 11:17
  • Dear EyePeaSea, it is not letting me ask more questions and saying i have reached my question limit. Please help.. – ramji Sep 29 '15 at 11:47
  • Try again now. If it doesn't work, then please read this article: http://meta.stackoverflow.com/questions/271542/why-wont-the-system-allow-me-to-ask-questions-for-several-days - You may have to wait before you can post the question. An example of the VBA line you need is **Worksheets(ActiveSheet.Name).Range("h8").ClearContents** - **ClearContents** does just that. **Clear** will clear contents and formatting. If you can post a 'question' again soon then I'll post a proper bit of code. However, I can't keep answering in these comments as they are very limited. – EyePeaSea Sep 29 '15 at 12:10
  • Dear EyePeaSea. One more thing i wish to ask, i want that when i save the file in xlsx format at m:\formats\excels\ then it should not show the vba – ramji Sep 30 '15 at 07:27
  • Dear @EyePeaSea. One more thing i wish to ask, i want that when i save the file in xlsx format at m:\formats\excels\ then it should not show the vba (macro) button in this file, however it should remain in the master file. for this i am trying to add a line among these codes. **wsThisWorkSheet.Shapes("Button 8").Delete**but it is not working when i run codes it says error saving document. could you please help me once again. you can see this snapshot. http://i.imgur.com/aG5imJF.png please help me once again – ramji Sep 30 '15 at 07:38
  • Hi Ramji, you nearly got the solution. I'll edit the answer above to show two new lines after the 'SaveAs' line. – EyePeaSea Sep 30 '15 at 09:20
  • Ramji - I tested this with with an ActiveX command button called 'CommandButton1'. But I changed the text in the answer above to reflect the name used in your comment (Button 8). If you have problems, make sure you are using an ActiveX button and don't put spaces in names. – EyePeaSea Sep 30 '15 at 09:31
  • Thanks @EyePeaSea for helping me once again. you are great. sir please tell me what is difference between ActiveX control button and Form controls button. Do not they work same? the codes are working fine in ActiveX control button but not with form controls button. when i try to run these codes by inserting form control button, it again shows error saving file. Thanks a lot. Please guide me. – ramji Sep 30 '15 at 11:52
  • Ramji - they are different components and whilst they can do many of the same things (run VBA code) their properties and methods differ. If Form Control buttons don't work for you, but ActiveX control buttons do work, then use the ActiveX control button. – EyePeaSea Sep 30 '15 at 12:10
  • Thanks @EyePeaSea. sir i am trying to remove one line from excel sheet but could not succeed. i merged two different ranges in the sheet and it left one line between them. i again select both the areas of merged ranges and tried to merge both areas again but it is not removing that line. could you please tell me how to remove this line. see at the snapshot http://i.imgur.com/Z49LUeU.png please help me . thanks a lot sir. – ramji Sep 30 '15 at 13:12
  • Ramji - the comments section is not the place to ask new questions. You should ask a new question. – EyePeaSea Sep 30 '15 at 13:39
  • I have asked a new question **How to assign a tooltip at a macro button added by customize ribbon in ms excel?** kindly help me solving this question. The link of the question is https://stackoverflow.com/questions/32931939/how-to-assign-a-tooltip-at-a-macro-button-added-by-customize-ribbon-in-ms-excel – ramji Oct 04 '15 at 19:19
  • HI EyePeaSea, @EyePeaSea I have asked a new question **How to assign a tooltip at a macro button added by customize ribbon in ms excel?** kindly help me solving this question. The link of the question is https://stackoverflow.com/questions/32931939/how-to-assign-a-tooltip-at-a-macro-button-added-by-customize-ribbon-in-ms-excel – ramji Oct 04 '15 at 19:45