VBA to save file in excel and Pdf format to specific folder

Jnb99

Board Regular
Joined
Mar 29, 2016
Messages
84
Good day,



I did this VBA a few years ago (searched online and YouTube), and had the save as function as well. It worked like a machine, but somehow I either deleted the Workbook or something else went wrong. I've never used it again until recently, mostly because i didn't need it.



I would like to save a copy of the file in Pdf and excel format to a specific folder, keep the email function, and then clear the sheet. If the sheet is cleared, It must also automatically count up the next document number.

The code below:



VBA Code:
Sub PostToRegister()

Dim WS1 As Worksheet

Dim WS2 As Worksheet

Set WS1 = Worksheets("Quotation")

Set WS2 = Worksheets("Register")

' Figure out which row is next row

NextRow = WS2.Cells(Rows.Count, 1).End(xlUp).Row + 1

'Write the important values to register

WS2.Cells(NextRow, 1).Resize(1, 5).Value = Array(WS1.Range("J1"), WS1.Range("J3"), WS1.Range("C9"), WS1.Range("C10"), Range("QteTot"))



End Sub





Sub InsertRow()

    Dim Rng, n As Long, k As Long

    Application.ScreenUpdating = False

    Rng = InputBox("Enter number of rows required.")

    If Rng = "" Then Exit Sub

    Range(ActiveCell, ActiveCell.Offset(Val(Rng) - 1, 0)).EntireRow.Insert

    'need To know how many formulas To copy down.

    'Assumesfrom A over To last entry In row.

    k = ActiveCell.Offset(-1, 0).Row

    n = Cells(k, 256).End(xlToLeft).Column

    Range(Cells(k, 1), Cells(k + Val(Rng), n)).FillDown

End Sub



Sub AttachActiveSheetPDF()

  Dim IsCreated As Boolean

  Dim i As Long

  Dim PdfFile As String, Title As String

  Dim OutlApp As Object

 

  ' Not sure for what the Title is

  Title = Range("C10")

 

  ' Define PDF filename

  PdfFile = ActiveWorkbook.FullName

  i = InStrRev(PdfFile, ".")

  If i > 1 Then PdfFile = Left(PdfFile, i - 1)

  PdfFile = PdfFile & "_" & ActiveSheet.Name & ".pdf"

 

  ' Export activesheet as PDF

  With ActiveSheet

    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

  End With

 

  ' Use already open Outlook if possible

  On Error Resume Next

  Set OutlApp = GetObject(, "Outlook.Application")

  If Err Then

    Set OutlApp = CreateObject("Outlook.Application")

    IsCreated = True

  End If

  OutlApp.Visible = True

  On Error GoTo 0

 

  ' Prepare e-mail with PDF attachment

  With OutlApp.CreateItem(0)

   

    ' Prepare e-mail

    .Subject = Title

    .To = "admin@xxxxxxx" ' <-- Put email of the recipient here

    .CC = "neil@xxxxxxxxxx" ' <-- Put email of 'copy to' recipient here

    .Body = "Hi," & vbLf & vbLf _

          & "The quote is attached in PDF format." & vbLf & vbLf _

          & "Regards," & vbLf _

          & "C2C Quotes" & vbLf & vbLf

    .Attachments.Add PdfFile

   

    ' Try to send

    On Error Resume Next

    .Send

    Application.Visible = True

    If Err Then

      MsgBox "E-mail was not sent", vbExclamation

    Else

      MsgBox "E-mail successfully sent", vbInformation

    End If

    On Error GoTo 0

   

  End With

 

  ' Delete PDF file

  Kill PdfFile

 

  ' Quit Outlook if it was created by this code

  If IsCreated Then OutlApp.Quit

 

  ' Release the memory of object variable

  Set OutlApp = Nothing

 

End Sub

Thanks in advance!
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
what error do you get when running it?
 
Upvote 0
The sheet emails the pdf version, but I can’t find where it’s saving a copy (if it’s saving).

I want it to save a excel version as well to a specific and the same folder as above, to be able to edit of needed later.

And lastly would like it to clear specific cells after saving
 
Upvote 0
I’ve been so caught up with searching code, I’ve just realized the pdf version it currently save, is saving it as the master file/vba workbook’s name. I would like it to save as the document number, followed by a name on the document
 
Upvote 0
The sheet emails the pdf version, but I can’t find where it’s saving a copy (if it’s saving).

The file is being saved but then you're deleting it with this line...
VBA Code:
    Kill PdfFile



I would like it to save as the document number, followed by a name on the document

This should be easy enough but you'll have to give us more information. How do we know what the document number is/should be? Is it on the worksheet somewhere? If so, where? Same goes for the name you want on the document.
 
Upvote 0
lol! I stuffed that up completely...

Now that we've established its me that is the problem and not the code, I'll list all the steps below for reference sake:

This document will be used in a multi-service company which we need our quote numbers to follow one another. So saving the excel version I would like to save it as(for later reference)(Services are Electrical, Construction, and carpentry):
Service-year-month-document no-client name
example:
E202002-001-Peter
PDF version:
Service-year-month-document no
example:
E202002-001

Cells that will be applicable:
Service: A15
Document no: I2
Client name: C8

Steps:
1. Click button on Quote sheet
2. Save in both excel and pdf format
3. When saved, count document number +1 (E202002-001; E202002-002; CA202002-003. This number should remain the same if sheet is closed and opened again.
4. When saved, Carry over Date, document no, client name, Amount over to quote register sheet.
5. Clear body of quote when saved (if possible)
 
Upvote 0
lol! I stuffed that up completely...
Haha. It happens. Don't worry about it.


I'll list all the steps below for reference sake:
Okay I can help with this but unfortunately I am about to leave work for the day and I'll not be able to log on to the forums again until tomorrow due to other commitments. Hopefully somebody else will be along to help in the meantime but if not I'll be back tomorrow and should be able to post a solution for you then.
 
Upvote 0
Haha. It happens. Don't worry about it.



Okay I can help with this but unfortunately I am about to leave work for the day and I'll not be able to log on to the forums again until tomorrow due to other commitments. Hopefully somebody else will be along to help in the meantime but if not I'll be back tomorrow and should be able to post a solution for you then.
No problem, Thanks a lot!
 
Upvote 0
Hi @Jnb99

I've amended your existing code a bit to achieve what you want.

This assumes you want both the PDF and the copy of the workbook saved in the same directory as the workbook from which you're running the code. If that's not the case simply change ThisWorkbook.Path to suit your needs (E.g. "C:\path\to\my\directory").
VBA Code:
    Dim IsCreated As Boolean
    Dim i As Long
    Dim PdfFile As String, Title As String, wbName As String
    Dim OutlApp As Object


    ' Not sure for what the Title is
    Title = Range("C10")


    'Define Workbook and PDF filenames
  
    'Construct the first part of the workbook filename
    wbName = ThisWorkbook.Path & "\" & Left(Range("A15").Text, 1) & Year(Date) & Format(Month(Date), "00") & "-" & Format(Range("I2").Value, "000")
    'Assign the same to the PDF filename
    PdfFile = wbName & ".pdf"
    'Add the last bit of the workbook filename
    wbName = wbName & "-" & Range("C8").Text & ".xlsm"
    

    'Export activesheet as PDF
    With ActiveSheet
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    End With
    
    
    'Increment document number
    ActiveSheet.Range("I2").Value = ActiveSheet.Range("I2").Value + 1
    
    
    'Save a copy of the workbook
    ActiveWorkbook.SaveCopyAs wbName


  ' Use already open Outlook if possible
    On Error Resume Next
    Set OutlApp = GetObject(, "Outlook.Application")
    If Err Then
        Set OutlApp = CreateObject("Outlook.Application")
        IsCreated = True
    End If
    OutlApp.Visible = True
    On Error GoTo 0


  'Prepare e-mail with PDF attachment
  With OutlApp.CreateItem(0)

    ' Prepare e-mail
    .Subject = Title
    .To = "admin@xxxxxxx" ' <-- Put email of the recipient here
    .CC = "neil@xxxxxxxxxx" ' <-- Put email of 'copy to' recipient here
    .body = "Hi," & vbLf & vbLf & _
          "The quote is attached in PDF format." & vbLf & vbLf & _
          "Regards," & vbLf & _
          "C2C Quotes" & vbLf & vbLf
    .Attachments.Add PdfFile

   
    ' Try to send
    On Error Resume Next
    .Send
    Application.Visible = True
    If Err Then
      MsgBox "E-mail was not sent", vbExclamation
    Else
      MsgBox "E-mail successfully sent", vbInformation
    End If
    On Error GoTo 0

  End With
 

    ' Quit Outlook if it was created by this code
    If IsCreated Then OutlApp.Quit


    ' Release the memory of object variable
    Set OutlApp = Nothing


I still need a little clarification around your last two requirements though:

4. When saved, Carry over Date, document no, client name, Amount over to quote register sheet.
What is the structure of the "quote register sheet"? As in; where does each bit of data need to go? What cells/ranges?
Is the "quote register sheet" a cumulative record of all quotes?

5. Clear body of quote when saved (if possible)
Presumably the quote is on the sheet you are running the above code from? In that case I'm assuming you want the relevant data copied to the "quote register sheet" as in Q4 above, and then the "quote" sheet (the currently active sheet) cleared (apart from the document number) ready for the next quote. Is that correct?


If you could provide a sample workbook or use XL2BB to post an example that would be helpful. If not be as descriptive as possible and I'll do my best to help :)
 
Upvote 0
You Legend!!! Thank you so much!

Regarding the rest of the things above. I've made a complete new sheet, and transferred my old code before I heard back from you, so some of the cells differ, and I've managed it to save the sheet both ways, and with the "E" and "CU" or "CA", but couldn't get around the current month input on the quote number. So I'll be putting your code in, changing cells again and then the only thing left will be the Quote register sheet as you asked about below.

"4. When saved, Carry over Date, document no, client name, Amount over to quote register sheet. "
Quote Register sheet layout is from left to right:
Cell no: A1 B1 C1 D1 E1 F1
Cell description: Date Trade Doc No Client Site Amount

The values of each of the above (Date, trade etc.) are located on Quotation sheet in cells:
Cell description: Date Trade Doc No Client Site Amount
Cell no: J3 J2 K2 C8 C11 I42

"5. Clear body of quote when saved (if possible) "
I don't this this will be possible, because all quotes wont be the same amount of rows, so is there a way to have it recognize the quote body?
Quote sample.jpg

Quote sample1.jpg
Quote sample2.jpg
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,204
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top