Moved PDFs from one location to another

babar2019

Board Regular
Joined
Jun 21, 2019
Messages
93
Hi. Not a pro coder but this is what I have so far.

Sub SaveasPDF()

Dim wsA As Worksheet
Dim wbA As Workbook
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler

Set wbA = ActiveWorkbook
Set wsA = ActiveSheet

Sheets(Array("total", wsA.Name)).Select

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & ""

strName = "WORK AS OF" & " " & Format(Date, "MM-DD-YYYY")

'create default name for saving file
strFile = strName & ".pdf"
strPathFile = strPath & strFile

'Create DAily folder under Today's dte
MkDir ("P:\INFORMATION TECHNOLOGY\Non-Public\Applications" & Format(Date, "MM-DD-YYYY"))

'export to PDF in current folder
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strPathFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& strPathFile

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


I want to move the converted pdf to be saved in the folder that I'm creating under today's date. Currently it is being saved in the directory that the excel is in. There will just be one pdf labeled 'WORK AS OF MM-DD-YYYY' format which I want to move.

For example, if you run the code, the WORK AS OF 06-25-2019 should move to the 06-25-2019 folder that I'm creating.

Please help.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Your macro only saves the active sheet in PDF:

Code:
[B][I]Set wsA = [COLOR=#ff0000]ActiveSheet[/COLOR]
[/I][/B][B][I]wsA.ExportAsFixedFormat _[/I][/B]

I do not know why you select the sheets in this part:
Code:
[B][I]Sheets(Array("total", wsA.Name)).Select[/I][/B]

------
For the final folder

Code:
strPath = "P:\INFORMATION TECHNOLOGY\Non-Public\Applications\" & Format(Date, "MM-DD-YYYY")

Option1
Code:
strPath = "P:\INFORMATION TECHNOLOGY\Non-Public\Applications\06-25-2019
or Option 2
Code:
strPath = "P:\INFORMATION TECHNOLOGY\Non-Public\Applications06-25-2019

I used option 1 in the macro

-----
I made some adjustments to your macro.
Try this

Code:
Sub SaveasPDF()
    Dim wbA As Workbook, wsA As Worksheet
    Dim strName As String, strPath As String, strPathFile As String
    
    On Error GoTo errHandler
    Set wbA = ActiveWorkbook
    Set wsA = ActiveSheet
    Sheets(Array("total", wsA.Name)).Select
     
    'create default name for saving file
    strName = "WORK AS OF" & " " & Format(Date, "MM-DD-YYYY") & ".pdf"
    
    'Create DAily folder under Today's dte
    strPath = "P:\INFORMATION TECHNOLOGY\Non-Public\Applications[B][COLOR=#ff0000]\[/COLOR][/B]" & Format(Date, "MM-DD-YYYY")
    If Dir(strPath, vbDirectory) = "" Then MkDir (strPath)
    strPathFile = strPath & "\" & strName
    
    'export to PDF in current folder
    wsA.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPathFile, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " & vbCrLf & strPathFile
    Exit Sub


errHandler:
    MsgBox "Could not create PDF file"
End Sub
 
Last edited:
Upvote 0
Perhaps something like this.
Code:
Sub SaveasPDF()

Dim wsA As Worksheet
Dim wbA As Workbook
Dim strName As String
Dim strPath As String
Dim strSavePath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant

    On Error GoTo errHandler

    Set wbA = ActiveWorkbook
    Set wsA = ActiveSheet

    Sheets(Array("total", wsA.Name)).Select

    'get active workbook folder, if saved
    strPath = wbA.Path
    
    If strPath = "" Then
        strPath = Application.DefaultFilePath
    End If
    
    strFile = "WORK AS OF" & " " & Format(Date, "MM-DD-YYYY")

    'create default name for saving file
    strFile = strName & ".pdf"
    strPathFile = strPath & strFile

    strSavePath = "P:\INFORMATION TECHNOLOGY\Non-Public\Applications" & Format(Date, "MM-DD-YYYY")
    'Create DAily folder under Today's dte
    MkDir strSavePath

    'export to PDF in current folder
    wsA.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=strSavePath & Application.PathSeparator & strFile, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
           & vbCrLf _
           & strPathFile

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
    
End Sub
 
Upvote 0
This worked perfectly. Thanks for that. I have some signature images of my users as PNG files on my desktop. What I would want to do is, depending on the user who is logged in , I want the signature image and a date stamp to be attached at the time of creating the pdf.

Can you assist me with this please?
 
Upvote 0
This worked perfectly, Thanks for that.

What I would also like to do is, I have a few signature snips (PNg files) on my desktop. depdning on the user logged in, I want it to attached the signature image with a date/time stamp at the time of creating the pdf.

Can you please assist me with this?
 
Upvote 0
This worked perfectly, Thanks for that.

What I would also like to do is, I have a few signature snips (PNg files) on my desktop. depdning on the user logged in, I want it to attached the signature image with a date/time stamp at the time of creating the pdf.

Can you please assist me with this?


Try this.
Change data in red by your information

Code:
Sub SaveasPDF()
    Dim wsA As Worksheet, strName As String, strPath As String, strPathFile As String
    Dim wUser As String, wDesk As String
        
    On Error GoTo errHandler
    Set wsA = ActiveSheet
    'create default name for saving file
    strName = "WORK AS OF" & " " & Format(Date, "MM-DD-YYYY") & ".pdf"
    'Create DAily folder under Today's dte
    strPath = "P:\INFORMATION TECHNOLOGY\Non-Public\Applications\" & Format(Date, "MM-DD-YYYY")
    strPath = "C:\trabajo\books\" & Format(Date, "MM-DD-YYYY")
    If Dir(strPath, vbDirectory) = "" Then MkDir (strPath)
    strPathFile = strPath & "\" & strName
    
    'attach signature image in cell A2
    wUser = Application.UserName
    wDesk = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    If Dir(wDesk & "\" & wUser & ".png") <> "" Then
        With wsA.Pictures.Insert(wDesk & "\" & wUser & ".png")
            .ShapeRange.LockAspectRatio = msoFalse
            .Top = wsA.Range("[COLOR=#ff0000]A2[/COLOR]").Top
            .Left = wsA.Range("[COLOR=#ff0000]A2[/COLOR]").Left
            .Width = wsA.Range("[COLOR=#ff0000]A2[/COLOR]").Width
            .Height = wsA.Range("[COLOR=#ff0000]A2[/COLOR]").Height  '
        End With
    End If
    
    'date/time stamp at the time of creating the pdf in cell B2
    wsA.Range("[COLOR=#ff0000]B2[/COLOR]").Value = Now


    'export to PDF in current folder
    wsA.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPathFile, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " & vbCrLf & strPathFile
    Exit Sub


errHandler:
    MsgBox "Could not create PDF file"
End Sub
 
Upvote 0
Will try this tomorrow.. Can I use multiple conditions in that if statement? Example: if user is xyz use image 1, if user is abc use image 2 so on and so forth?
 
Upvote 0
Will try this tomorrow.. Can I use multiple conditions in that if statement? Example: if user is xyz use image 1, if user is abc use image 2 so on and so forth?

In reality, you did not give any example of how the user-image relationship will be. Then the macro gets the username and looks for a .png image with that name in the desktop folder.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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