"Bad path or filename" runtime error 52 - sharepoint pathway issue

Mr_Ragweed2

Board Regular
Joined
Nov 11, 2022
Messages
145
Office Version
  1. 365
Platform
  1. Windows
I'm guessing i just need a fresh set of eyes to look at this. I have some pdf files on my c:drive that i am wanting to move to sharepoint. I have other code in which i save directly to sharepoint and it works fine. I ran a test to see if i can save to another folder in my C:drive and it works as well. When i run a messagebox the pathway addresses look to be correct but i get the "bad path or filename error" when i try and execute the code. (I marked the line where the error occurs). My File.Name and sourceFilePath are correct and like i said the destFilePath looks to be correct. What am i missing here?


VBA Code:
Option Explicit
Private Sub CommandButton1_Click()

    Dim sourceFilePath As String
    Dim destFilePath As String
    Dim sourceFolderPath As String
    Dim fileName As String
    Dim SWOPath As String 'destination folder path 1
    Dim TMSPath As String 'destination folder path 2
  
    'Dim newpath As String 'debugging pathway test
  
    Dim FSO As Object
    Dim SourceFolder As Object
    Dim File As Object

    Application.ScreenUpdating = False
  
    Dim MyName As String
    MyName = Environ$("Username")
      
    sourceFolderPath = "C:\Users\" & MyName & "\OneDrive - my company Inc\Desktop\Propane Forms"
 
    'newpath = "C:\Users\" & MyName & "\OneDrive - my company Inc\Desktop\Path Test Folder" ' debugging pathway test
    SWOPath = "https://abcd.sharepoint.com/teams/mycompany/Shared%20Documents/General/Crystal,%20Mary,%20Anna/Propane%20Service%20Work%20Orders/"
    TMSPath = "https://abcd.sharepoint.com/teams/mycompany/Shared%20Documents/General/Crystal,%20Mary,%20Anna/Tank%20Movement%20Sheet"
  
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set SourceFolder = FSO.GetFolder(sourceFolderPath)
  
    For Each File In SourceFolder.Files
  
        fileName = File.Name
  
    If fileName Like "*SWO*.PDF" Then
  
        sourceFilePath = File.Path
        destFilePath = SWOPath & File.Name
        'destFilePath = newpath & "/" & File.Name ' debugging pathway test
      
        FSO.MoveFile Source:=sourceFilePath, Destination:=destFilePath   ' error is on this line
        'MsgBox "W: " & File.Name & ", " & sourceFilePath & ", " & destFilePath
  
        ElseIf fileName Like "*TMS*.PDF" Then
  
        sourceFilePath = File.Path
        destFilePath = TMSPath & "/" & File.Name
        FSO.MoveFile Source:=sourceFilePath, Destination:=destFilePath
        'MsgBox "T: " & File.Name & ", " & sourceFilePath & ", " & destFilePath
  
    End If
  
    Next
  
    Set SourceFolder = Nothing
    Set FSO = Nothing
  
End Sub

if this looks familiar you are correct. i had an earlier thread with a different error on the same code. Since i have a new question/error i thought i should start a new thread. Here is the link to my other thread if that is relevant.
"invalid procedure call or argument"

Thanks for any help you can provide. I do mark solutions when i get them and i click the thumbs up even though i'm not sure what that actually does lol.
 
So, while i'm sure your method works fine and is probably easier, I changed my approach and found a method that seems to work for me. Instead of converting the files to pdfs on the users folder i leave them as .xlsx files. on my version of syncing is when i change them to pdfs and for some reason the pathway to teams works better as a Save event than a Move event (even though it was the same pathway i was trying earlier.) I will type up a better version of my solution as soon as i run it through a little more testing. I will post those results here and to my other related threads that have dealt with various issues along the way.
Thank you for your help!
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
update and partial solution. (I shortened the loop for the example). My problem now is the pdfs wont open in Sharepoint/Teams. They only open from the C:Drive Documents folder.
Is this still a sync issue?
VBA Code:
Private Sub CommandButton1_Click()

    Dim sourceFolderPath As String
    Dim oldName As String
    Dim newName As String
    Dim wb2 As Workbook
    Dim FSO As Object
    Dim SourceFolder As Object
    Dim File As Object
    
    Application.ScreenUpdating = False
    
    Dim MyName As String
    MyName = Environ$("Username")
        
    sourceFolderPath = "C:\Users\" & MyName & "\OneDrive - my companyInc\Desktop\Propane Forms"
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set SourceFolder = FSO.GetFolder(sourceFolderPath)
    
    '12-26 12:30am this mostly works. files go to correct folders as pdfs and are deleted from original folder. "ZSync" is left alone.
    ' problem is that the pdfs wont open in Teams but will open on the C:drive....
    
    On Error Resume Next
    
    For Each File In SourceFolder.Files
    
        oldName = File.Name
    
    newName = Left(oldName, Len(oldName) - 5)
    
    If oldName Like "*ZSync*.xlsm" Then
    Exit Sub
    
    ElseIf oldName Like "*SWO*" & ".xlsx" Then
        Set wb2 = Workbooks.Open(File)
        wb2.SaveAs fileName:= _
            "https://abcd.sharepoint.com/teams/mycompany/Shared%20Documents/General/Crystal,%20Mary,%20Anna/Propane%20Service%20Work%20Orders" _
             & "/" & newName & ".pdf"
            wb2.ExportAsFixedFormat Type:=xlTypePDF
        ActiveWindow.Close
        Kill "C:\Users\" & MyName & "\OneDrive - my company Inc\Desktop\Propane Forms\" & File.Name
        
        ElseIf oldName Like "*TMS*" & ".xlsx" Then
        Set wb2 = Workbooks.Open(File)
        wb2.SaveAs fileName:= _
            "https://abcd.sharepoint.com/teams/my company/Shared%20Documents/General/Crystal,%20Mary,%20Anna/Tank%20Movement%20Sheet/" _
            & newName & ".pdf"
            wb2.ExportAsFixedFormat Type:=xlTypePDF
        ActiveWindow.Close
        Kill "C:\Users\" & MyName & "\OneDrive - my company Inc\Desktop\Propane Forms\" & File.Name
       
    End If
 
    Next File
    
    Set SourceFolder = Nothing
    Set FSO = Nothing
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

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