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?
Related
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.
There is an unzipping code I'd like to adjust 4 my needs.
Sub Unzip()
Dim FSO As Object
Dim oApp As Object
Dim Fname As Variant
Dim FileNameFolder As Variant
Dim DefinePath As String
' Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", MultiSelect:=False)
Fname = "C:\Users\michal\SkyDrive\csv\bossa\mstcgl.zip"
If Fname = False Then
'Do nothing
Else
'Destination folder
DefinePath = "C:\Users\michal\SkyDrive\csv\bossa\mstcgl_mst\" ' Change to your path / variable
If Right(DefinePath, 1) <> "\" Then
DefinePath = DefinePath & "\"
End If
FileNameFolder = DefinePath
' Delete all the files in the folder DefPath first if you want.
' On Error Resume Next
' Kill DefPath & "*.*"
' On Error GoTo 0
'Extract the files into the Destination folder
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items
' MsgBox "You find the files here: " & FileNameFolder
On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
End If
End Sub
Somewhere here:
`Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameFolder).CopyHere`
a dialog box appears asking me if I want to overwrite the file that have the same names - and Yes I do want to overwrite them, but without answering the dialog box - I would like to hardcode it into the code, please.
I've found this page https://msdn.microsoft.com/en-us/library/windows/desktop/bb787866(v=vs.85).aspx but I just don't know how to add this parameter #16 which is "Respond with "Yes to All" for any dialog box that is displayed."
Can U help me with that?
And the last thing:
can You explain oApp.Namespace(Fname).items line for me.
I've really tried to guess it myself, but I thing I'm to short 4 this.
the code that results in no questions or no prompting of any kind is as follows:
Option Explicit
Sub Bossa_Unzip()
Dim FSO As Object
Dim oApp As Object ' oApp is the object which has the methods you're using in your code to unzip the zip file:
'you need to create that object before you can use it.
Dim Fname As Variant
Dim FileNameFolder As Variant ' previously Dim FileNameFolder As Variant
Dim DefinePath As String
' Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", MultiSelect:=False)
Fname = "C:\Users\michal\SkyDrive\csv\bossa\mstcgl.zip"
If Fname = False Then
'Do nothing
Else
'Destination folder
DefinePath = "C:\Users\michal\SkyDrive\csv\bossa\mstcgl_mst\" ' Change to your path / variable
If Right(DefinePath, 1) <> "\" Then
DefinePath = DefinePath & "\"
End If
FileNameFolder = DefinePath
' Delete all the files in the folder DefPath first if you want.
' On Error Resume Next
' Kill DefPath & "*.*"
' On Error GoTo 0
'Extract the files into the Destination folder
Set oApp = CreateObject("Shell.Application") ' you need to create oApp object before you can use it.
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items, 16
'MsgBox "You'll find the files here: " & DefinePath
On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
End If
End Sub
Of course this site helped me a lot - its CpyHere explanation site.
One thing I don't understand is why Fname and FileNumberFolder need to be declared as variant. In my opinion, they should be declared as String. Just look at this screenshot.
But when I declare them that way, the code gives me error.
Just look here, when the variables already have their values (first picture). The FileNameVariable and DefinePath variable have the exact same value, and it looks like a string 4 me. How is that necessary, that I need to declare another variable - FileNameVariable in that case (in 17th line) with the same value, but variant type.
please explain that to me, someone.
I am getting this error while i am trying to read one by one all files in a dir, and also reading the text inside
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe
Additional information: Object reference not set to an instance of an object.
It is going good only for the first file , but when it starts another loop and gets the secound file from that folder there comes the problem.
This is my code
Dim theString As String
Dim path As String
Dim StrFile, str_5 As String
Dim fso As New FileSystemObject
Dim file As TextStream
Dim line As String
'theString = ""
path = "C:\IN\"
StrFile = Dir(path & "*.txt")
Do While StrFile <> ""
file = fso.OpenTextFile(path & StrFile)
Do While Not file.AtEndOfLine
line = file.ReadLine
If InStr(1, line, line.Substring(142, 1), vbTextCompare) = 5 Then
'Debug.Print(StrFile)
str_5 = str_5 & line
End If
Loop
file.Close()
file = Nothing
fso = Nothing
StrFile = Dir()
Loop
Just for other people with the same problem, the answer is to just remove this line
fso = Nothing
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.
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