excel vba to upload file to sharepoint - vba

I am trying to upload a folder from my C drive to a SharePoint library site. I have used the below code, which works fine when the ToPath is not a SharePoint library site but another folder from my C drive. Where am I going wrong?
Sub AddSharePointFiles()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
ToPath = "https://share.name.com/site/folder/_layouts/15/start.aspx#/LibraryName/Forms/AllItems.aspx"
FromPath = "C:\Users\Name\Documents\FolderName"
Set FSO = CreateObject("scripting.filesystemobject")
FSO.CopyFile Source:=FromPath, Destination:=ToPath
End Sub
Thank you!

I noticed that the SharePoint URL starts with https. As such, you'll need to construct your UNC path as \\share.name.com#SSL\DavWWWRoot\site\library\.
A few things to check:
WebClient service is running
The SharePoint site is trusted in Internet Options

Try specifying the Sharepoint path as UNC, and using the CopyFolder method:
Sub AddSharePointFiles()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
ToPath = "\\share.name.com\site\folder"
FromPath = "C:\Users\Name\Documents\FolderName"
Set FSO = CreateObject("scripting.filesystemobject")
FSO.CopyFolder Source:=FromPath, Destination:=ToPath
End Sub

The solution for me was using:
Folder = Environ("USERPROFILE")
This gave me the name of the computer, then I filled the rest manually
Folder = Environ("USERPROFILE") & "\Business\Administration\Documents"
It worked though I donĀ“t think is an elegant solution.

Related

VBA - Run error 76 Path not found in Excel

I am trying to run this code but for some reason eror 76 Path not found pops up. The respected file exists in the listed directory.
Sub AddSharePointFiles()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
ToPath = "\\share.company.com\sites\hmw1\Shared%20Documents\Forms\AllItems.aspx"
FromPath = "C:\Users\user\Downloads\ROSTER_BASELINE_1642279329.xlsx"
Set FSO = CreateObject("scripting.filesystemobject")
FSO.CopyFolder Source:=FromPath, Destination:=ToPath
End Sub
Running process breaks on line FSO.CopyFolder...Any idea what could cause an error?

Move files to folders

I have files and folders with same name how i can move this files to folder has same name.
In VBA you can move files between folders with Scripting.FileSystemObject.
To do that, you can somenthing like in this Sub:
Sub MoveFiles()
Dim FSO As Object
Dim SourceFileName As String, DestFileName As String
Set FSO = CreateObject("Scripting.Filesystemobject")
SourceFileName = "C:\Users\myUser\Documents\Test.xlsx"
DestFileName = "C:\Users\myUser\Desktop\Test.xlsx"
FSO.MoveFile Source:=SourceFileName, Destination:=DestFileName
End Sub
Hope this helps.

How do i create a VB Macro that will save a certain file to all sub folders in a particular directory?

This is what I have so far, might be good might not haha!
I have been trying to save a word document to about 400+ folders without having to go through them all, can this be done through VB Macros? I got it working to just save it to the directory, but I cannot save it to all the Subfolders.
Dim FileSystem As Object
Dim HostFolder As String
Sub DoFolder(folder)
HostFolder = ("H:\test2")
Set FileSystem = CreateObject("Scripting.FileSystemObject")
DoFolder FileSystem.GetFolder(HostFolder)
Dim SubFolder
For Each SubFolder In folder.SubFolders
DoFolder SubFolder
Next
Dim File
For Each File In folder.Files
Set FileSystem = CreateObject("Scripting.FileSystemObject")
' Operate on each file
ActiveDocument.Save
Next
End Sub
I recommended reading: Chip Pearson -Recursion And The FileSystemObject
Make a recursive subroutine to iterate over all the subfolders (and their subfolders) in the root directory.
getAllSubfolderPaths: returns an array that lists all the sub folders in a folder.
Function getAllSubfolderPaths(FolderPath As String, Optional FSO As Object, Optional List As Object)
Dim fld As Object
If FSO Is Nothing Then
Set FSO = CreateObject("Scripting.Filesystemobject")
Set List = CreateObject("SYstem.Collections.ArrayList")
End If
List.Add FolderPath
For Each fld In FSO.GetFolder(FolderPath).SubFolders
getAllSubfolderPaths fld.Path, FSO, List
Next
getAllSubfolderPaths = List.ToArray
End Function
Test
Sub Test()
Const RootFolder As String = "C:\Users\Owner\Pictures"
Const SourcePath As String = "C:\Users\Owner\Documents\Youcam"
Const SourceFileName As String = "Capture.PNG"
Dim fld As Variant, FolderArray As Variant
Dim Destination As String, Source As String
FolderArray = getAllSubfolderPaths(RootFolder)
For Each fld In FolderArray
Destination = fld & "\" & SourceFileName
Source = SourcePath & "\" & SourceFileName
'Delete old copy of file
If Destination <> Source And Len(Dir(Destination)) Then Kill Destination
VBA.FileCopy Source:=Source, Destination:=Destination
Next
End Sub
Gotta love auditing requirements... You're basically on the right path, but you really only need one FileSystemObject. About the only errors I see are that you need the .Path of the folder here...
For Each SubFolder In folder.SubFolders
DoFolder SubFolder.Path '<---Here.
Next
...and you don't need to loop through all the files here (you may be overthinking this one a bit):
For Each File In folder.Files
Set FileSystem = CreateObject("Scripting.FileSystemObject")
' Operate on each file
ActiveDocument.Save
Next
Also, I'd suggest using early binding instead of late binding (although the example below can easily be switched). I'd do something a bit more like this:
Private Sub SaveDocToAllSubfolders(targetPath As String, doc As Document, _
Optional root As Boolean = False)
With New Scripting.FileSystemObject
Dim current As Scripting.folder
Set current = .GetFolder(targetPath)
If Not root Then
doc.SaveAs .BuildPath(targetPath, doc.Name)
End If
Dim subDir As Scripting.folder
For Each subDir In current.SubFolders
SaveDocToAllSubfolders subDir.Path, doc
Next
End With
End Sub
The root flag is just whether or not to save a copy in the host folder. Call it like this:
SaveDocToAllSubfolders "H:\test2", ActiveDocument, True

Rename file using FSO MoveFile and Name-As not working

I am trying to rename a file from "X" to "XY" in the same folder. I have tried using File System Object and just the Name X as Y function, but neither is working. I do have the Microsoft Scripting Runtime reference installed. The code completes successfully but the file name does not change, Please advise.
Dim FSO As Object
Dim srcPath As String
Dim FromPath As String
Dim ToPath As String
Dim fldrName As String
srcPath = "C:\"
i = 1
Set FileSysObj_1 = New FileSystemObject
For Each Folder_Obj1 In FileSysObj_1.GetFolder(srcPath).SubFolders
i = i + 1
On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
'If the file exists in the folder then rename it
If Dir(srcPath & Folder_Obj1.Name & "_Hotel.xlsx") Then
fldrName = Folder_Obj1.Name
FromPath = srcPath & fldrName & "_Hotel.xlsx"
ToPath = srcPath & "Hotel.xlsx"
'*** Neither of the following two lines work to rename the file
FSO.MoveFile FromPath, ToPath
Name FromPath As ToPath
Else
MsgBox "File doesn't exist."
End If
Next
Your question mentions that you're trying to rename a file in the same folder but, according to your code, you're actually moving it to the root of C:. You can use the following code as a replacement for what you have above. It will rename the file in its original folder.
Dim objFSO As New Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
For Each objFolder In objFSO.GetFolder("c:\").SubFolders
If objFSO.FileExists(objFolder.Path & "\_Hotel.xlsx") Then
' Rename...
objFSO.GetFile(objFolder.Path & "\_Hotel.xlsx").Name = "Hotel.xlsx"
End If
Next
Go to the tools menu in your VBA IDE and select references. Select "Microsoft Scripting Runtime".
Then declare
Dim FSO As FileSystemObject
Then the MoveFile should work.
Big Warning!
When using MoveFile for renaming a file it will only work with complete filenames without wildcards so
fso.MoveFile ( "somepath\myfile1.*" "somepath\myfile2" ) will error while
fso.MoveFile ( "somepath\myfile1.pdf" "somepath\myfile2.pdf" ) will work.
Wildcard in the first argument only works when really moving to a different location without changing the filename. Then the second argument should end with "\" Wonder why the documentation on MSDN is so cryptic.

VBA moving folder using FSO keeps folder in use

I am using the below code to cycle through '.csv' files in a directory and move them to a new directory (strRootDir and strTargetDir are local variables that have been initiated):
Dim objFile As file
Dim objFSO As FileSystemObject: Set objFSO = New FileSystemObject
Dim objFolder As Folder: Set objFolder = objFSO.GetFolder(strRootDir)
For Each objFile In objFolder.Files
If InStr(1, objFile.Name, ".csv") Then
FileFolderExists strTargetDir, True
objFile.Move (strTargetDir)
End If
Next objFile
Set objFile = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
Where the method 'FileFolderExists' is defined as:
Public Sub FileFolderExists(strFullPath As String, bMkDir As Boolean)
Dim bExists As Boolean
If Not Dir(strFullPath, vbDirectory) = vbNullString Then bExists = True
If Not bExists And bMkDir Then MkDir strFullPath
End Sub
My question is that once this process has completed, if I try to delete the directory 'strTargetDir' I get an error message saying the folder is in use by another program.
How do I stop this from happening?
What you see is a Ghost File. The explorer has an uncanny habit of being stubborn :)
Here is another scenario which illustrates the "Ghost File"
Just issue a Dir command to remind explorer that the file no longer exists after you move the file and every thing will be ok :)
Ret = Dir(Path_And_FileName_Which_Was_Moved)
Also why use fso for moving files? Here is a one line command
Name "C:\Path1\File1.Ext" As "C:\Path2\File2.Ext"
This will also not leave a GHOST File