简体   繁体   中英

How to autosave by creating new folder in filepath

Based on codes I found on the website, I have created a macro to export a sheet from an Excel workbook as a PDF attached to an email. Based on other code I managed to create an autosave to a folder, but the save is hardcoded now. See the code below, I have 3 questions about the code.

  1. Now the save location is hardcoded into the macro. How can I adjust the macro to use the location where I open the file from as autosave location? I mainly need this to be dynamic, because we would copy this file into a new folder month, it cannot be hardcoded.
  2. I would like the macro to check if a separate folder exists in the save location, and if not create a folder for it. So basically, I would open the file from the folder C:\User:\CompanyDrive:\Client:\January2020, I would like the macro to check if this folder includes another folder like C:\User:\CompanyDrive:\Client:\January2020:\Individual Exports, and if it doesn't exist, to create one.
  3. Is there a way to avoid using the SharePoint path when saving? We use the company OneDrive/SharePoint local sync to open documents, but it automatically converts to the SharePoint document and tries to save to SharePoint. However, we get a failed upload all the time, so I was wondering how we can avoid that?

Thanks in advance!

See code below:

Sub Saveaspdfandsend()
Dim xSht As Worksheet
Dim xFileDlg As FileDialog
Dim xFolder As String
Dim xYesorNo As Integer
Dim xOutlookObj As Object
Dim xEmailObj As Object
Dim xUsedRng As Range
Dim xPath As String
Dim NameOfWorkbook

NameOfWorkbook = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1))

Dim myPath As String
Dim folderPath As String

folderPath = Application.ActiveWorkbook.Path
myPath = Application.ActiveWorkbook.FullName

Set xSht = ActiveSheet
xPath = "C:\User:\CompanyDrive:\Client:\January2020:\Individual Exports" 'here "Individual Exports" is the destination folder to save the pdf files
xFolder = xPath + "\" + NameOfWorkbook + " " + xSht.Name + ".pdf"
If Len(Dir(xFolder)) > 0 Then
    xYesorNo = MsgBox(xFolder & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
                      vbYesNo + vbQuestion, "File Exists")
    On Error Resume Next
    If xYesorNo = vbYes Then
        Kill xFolder
    Else
        MsgBox "if you don't overwrite the existing PDF, I can't continue." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
        Exit Sub
    End If
    If Err.Number <> 0 Then
        MsgBox "Unable to delete existing file.  Please make sure the file is not open or write protected." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
        Exit Sub
    End If
End If
 
Set xUsedRng = xSht.UsedRange
If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
    'Save as PDF file
    xSht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder, Quality:=xlQualityStandard
     
    'Create Outlook email
    Set xOutlookObj = CreateObject("Outlook.Application")
    Set xEmailObj = xOutlookObj.CreateItem(0)
    With xEmailObj
        .Display
        .To = ""
        .CC = ""
        .Subject = NameOfWorkbook + " " + xSht.Name
        .Attachments.Add xFolder
        If DisplayEmail = False Then
            '.Send
        End If
    End With
Else
  MsgBox "The active worksheet cannot be blank"
  Exit Sub
End If
End Sub
  folderPath = Application.ActiveWorkbook.Path
  myPath = Application.ActiveWorkbook.FullName

  Set xSht = ActiveSheet

  '1. use the location where I open the file from as autosave location
  xFolder = folderPath + "\" + NameOfWorkbook + " " + xSht.Name + ".pdf"
  '2. create folder in save location if it doesn't exist
  Const SubFolder = "Individual Exports"
  Dim sPath As String
  sPath = folderPath & Application.PathSeparator & SubFolder
  If Len(Dir(sPath, vbDirectory)) = 0 Then
    MkDir sPath
  End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM