+ Reply to Thread
Results 1 to 3 of 3

Change code so when it prints tabs to PDF, it uses the tab's name as PDF name?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-18-2020
    Location
    Ottawa, ON
    MS-Off Ver
    Professional Plus 2010
    Posts
    131

    Lightbulb Change code so when it prints tabs to PDF, it uses the tab's name as PDF name?

    Hi All,

    I got some code from you lovely people to print all my tabs to PDF files and attached them in an email to myself. This will save me loads of time. One thing I'm hoping to add though, is for each PDF to be named the same thing as the sheet it was printed from. So if the sheet is called "Packing List", the PDF file that attaches in the email would also be called "Packing List". I'm hoping this is an easy fix for you geniuses but how to do this escapes my feeble brain.

    Will be so appreciative if any of you can help me figure out how to do this!

    Sub PDFandSend()
    Dim ws As Worksheet
    Dim sAttach$, fName$, vDir$
    Dim i%
    Dim a
    
    Const erNum As Long = vbObjectError + 1000
    On Error GoTo errHandler
    
    vDir$ = "C:\Temp\Files" & Format(Now, "ddmmyyhhmmss")
    MkDir vDir
    With ActiveWorkbook
        For Each ws In .Worksheets
            With ws
                If .Visible Then
                    If WorksheetFunction.CountA(.Cells) > 0 Then
                        i = i + 1
                        fName = vDir & "\file2send_" & i & ".pdf"
                         sAttach = sAttach & fName & "|"
                        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=fName, openafterpublish:=False
                    End If
                End If
            End With
        Next
    End With
    If Len(sAttach) Then
        With CreateObject("Outlook.Application").CreateItem(0)
            .To = "[email protected]"
            .Subject = "Test Email #2 for Logistics Documents: "
            .Body = "Documents for Global Logistics"
            
                a = Split(Left(sAttach, Len(sAttach) - 1), "|")
                For i = 0 To UBound(a)
                    .Attachments.Add a(i)
                Next
          
            .Display
        End With
        MsgBox "done"
    Else
        Err.Raise erNum, , "No attachments created"
    End If
    finish:
    On Error Resume Next
    CreateObject("Scripting.FileSystemObject").DeleteFolder vDir
    
    Exit Sub
    
    errHandler:
    MsgBox Err.Description
    Resume finish
    End Sub

  2. #2
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,157

    Re: Change code so when it prints tabs to PDF, it uses the tab's name as PDF name?

    Try to change:
    'fName = vDir & "\file2send_" & i & ".pdf"
    fName = vDir & "\" & ws.Name & ".pdf"

  3. #3
    Forum Contributor
    Join Date
    02-18-2020
    Location
    Ottawa, ON
    MS-Off Ver
    Professional Plus 2010
    Posts
    131

    Re: Change code so when it prints tabs to PDF, it uses the tab's name as PDF name?

    Quote Originally Posted by mjr veverka View Post
    Try to change:
    'fName = vDir & "\file2send_" & i & ".pdf"
    fName = vDir & "\" & ws.Name & ".pdf"
    That did it!! Such a simple line to fix it seems at first glance. Thank you for your help; I don't know what I Would do without this community.

+ 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. Template prints twice when running vba code
    By admat in forum Excel General
    Replies: 2
    Last Post: 10-19-2019, 08:19 PM
  2. Calculating Sumifs Across Multiple Tabs; allowing Tabs to change or be added/deleted
    By Charles A. Othon in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-12-2019, 04:49 PM
  3. Replies: 1
    Last Post: 10-30-2018, 05:22 AM
  4. Print Macro Question - One form, multiple page prints with a single change for each.
    By Hal05154 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-11-2016, 01:13 PM
  5. Modification of a code that Prints all Pivot Filters
    By VKS in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-28-2015, 12:35 PM
  6. Prints fine from VBE but not when code runs
    By ThumbsUp in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-26-2014, 03:26 PM
  7. VBA that change the date and prints
    By robrobet in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-19-2013, 10:13 PM

Tags for this Thread

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