Move files between folders in the server with vba - vba

I'm trying to move a pdf file from a folder to another both located in the server (a Teams sharepoint).
I've used the code below (which is working perfectly on local) but i got the following error :
PS : I tried to access the "SourceFileName1" when debugging by copy paste in my browser and the file is here
Dim FSO As Object
Dim SourceFileName As String, DestinFileName As String
Set FSO = CreateObject("Scripting.Filesystemobject")
SourceFileName1 = "https://comxx.sharepoint.com/.../folder1/myfile.pdf"
DestinFileName1 = "https://comxx.sharepoint.com/.../folder2/myfile.pdf"
FSO.MoveFile Source:=SourceFileName1, Destination:=DestinFileName1
I've tried also with a path like this "\\comxx.sharepoint.com....\myfile.pdf" i got another error (below). I thought it was a permission issue but in my code i'm exporting this file (myfile) into the folder1 without problem.
Do you have any idea about how to handle with this issue ?
Thanks

Related

How to get DirectoryInfo for a Onedrive folder with VB.net

I have some code that uses System.IO to find files with a given extension. This works fine with regular folders but fails when the files are in Onedrive's local cache.
My assumption is that the problem is to do with the Onedrive cache folder because if the files are moved out of Onedrive and into a local folder e.g. c:\temp, it all works fine.
Dim Folder As New IO.DirectoryInfo(TextBox_RootFolder.Text)
Filelist = Folder.GetFiles("*.xlsx", IO.SearchOption.AllDirectories)
This is VB, so I believe there should not be any issues with string literals, so I'm stumped.
The path string comes out as something similar to: "C:\Users\user\OneDrive - ThisPlace\MyFolder" so there's nothing particularly weird about the string.
When presented with a folder on Onedrive, my variable 'Folder' is correctly assigned with the full path but an exception is thrown on calling Folder.Getfiles. This results in the error "The tag present in the reparse point buffer is invalid".
BTW, I'm a novice so example code would be really appreciated as a detailed technical explanation is likely to go whoosh over my head.
I'm please to report that I found an easy way to solve the problem.
Firstly I retained the code that uses Folder.Getfiles to provide a list of files as this is fast, elegant and retrieves files in sub-folders.
It still throws an exception when it encounters a cloud sync folder such as Onedrive, DropBox etc., so in the catch I use the Dir() function to loop through the files as this does not have problems with folders of this type.
Here is some pseudo-code to show an example:
Dim sFile As String
sFile = Dir(sPath + "\*.xlsx")
Do While sFile <> ""
[ do stuff here ]
sFile = Dir() '' Get the next file
Loop
I hope this is helpful to someone.

Missing txt file from Visual Basic

I am working in Visual Basic 2017. I have tried to add the file to the Debug folder, but then it just shows that the txt file ienter image description heres missing. I don't have the option under the "Word Solution".. How can I make the file show up? It keeps telling me it doesn't exist.
Dim inFile As IO.StreamReader
Const FileName As String = "words.txt"
Dim subscript As Integer
You can get the path of the directory (Debug or Release or any other) of the *.exe file with:
Dim directory as String = My.Application.Info.DirectoryPath
Using this information, you can then construct the full path with
Dim path As String = IO.Path.Combine(directory, FileName)
If IO.File.Exists(path) Then
...
You can check in Windows File Explorer to see where the file actually is (notice the Copy Path on the ribbon). In File Explorer you will see that the .exe you are running is down 2 directories from the Words Project directory. The double dots in the path is an old DOS way to navigating around directories without having to type out the whole path. This tells the compiler to find the file up 2 directories from the current directory.
For testing purposes this will work. For a release version you could add the file to Resources and access it the same way in any version.
You don't need a stream for a text file. .ReadAllLines returns an array of the lines in the text file
Private Sub OpCode()
Dim words = File.ReadAllLines("..\..\words.txt")
End Sub

Downloading zip file from website using excel vba (if also able to extract the csv from zip file and open it in excel, then even better)

after quite some searching I've not been able to make a macro that would download a .zip file from a specific website. I mean I've been able to find similar problems but have not been able to apply the changes necessary in order for my problem to be solved. The website that contains the zip files is: https://nio.gov.si/nio/data/prvic+registrirana+vozila+v+letu+2014+po+mesecih, under table header "Priponke" are the files. For example: December 2014 (959 kb), November 2014 (1061 kb), ... The url that downloads the zip file for December 2014 is "cms/download/document/a7605005b6879fe5f7dbab6d60d4ae787dbced6b-1422453741279". I thank you in advance and am awaiting your reply.
My current code is:
Public Sub DownloadFile()
Dim objWHTTP As Object
Dim strPath As String
Dim arrData() As Byte
Dim lngFreeFile As Long
On Error Resume Next
Set objWHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
If Err.Number <> 0 Then
Set objWHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
End If
On Error GoTo 0
strPath = "https://nio.gov.si/nio/data/prvic+registrirana+vozila+v+letu+2014+po+mesecih"
strPath = "https://nio.gov.si/nio/cms/download/document/a7605005b6879fe5f7dbab6d60d4ae787dbced6b-1422453741279"
objWHTTP.Open "GET", strPath, False
objWHTTP.send
arrData = objWHTTP.responseBody
If Len(Dir("C:\FootieFile", vbDirectory)) = 0 Then
MkDir "C:\FootieFile"
End If
lngFreeFile = FreeFile
Open "C:\FootieFile\MyFile.xml" For Binary Access Write As #lngFreeFile
Put #lngFreeFile, 1, arrData
Close #lngFreeFile
Set objWHTTP = Nothing
Erase arrData
End Sub
Kind regards
You need to download a binary file, and save it. This can be done using the MSXML2.XMLHTTP60 object to download, and ADODB.Stream object for saving.
See e.g. http://www.motobit.com/tips/detpg_read-write-binary-files/
I've used this succesfully to download JPG files from a server and display them in an MS Access front end.
Also I hope you realize you can't start your url with 'cms', as you will need the fully qualified domain name plus resource (aka http:// etc)
Also be careful with unicode data. For that you might need to use StrConv(). Check after downloading that the size of your file is the same as it is on the server.

Visual Basic: File is said to be in the wrong folder

Ok, here is my story:
I am building a fileviewer, and i am trying to delete the selected file in the listview.
when i try to delete it, it gave me an error saying the file wasnt found. I looked at my desktop and the file was there. here is the original code:
dim f as string = lv1.focuseditem.text
my.computer.filesystem.deletfile(f)
lv1.update()
this gave me that error. My updated code is supposed to show me where the computer thinks my file is:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
this shows a messagebox that shows the path of:
C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt
but the real file location is:
C:\Users\tgs266\Desktop\test.txt
please help
How are you getting the filenames for the ListView? Is it just the filename and no path?
If, for example, lv1.FocusedItem.Text is "test.txt", and that is the value you use (without the path), by default the program will look in the directory it's executing in. This is most likely why you're seeing C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt as the location, instead of what you expected.
If all the files are on your desktop, you can use Environment.GetFolderPath in conjunction with the Environment.SpecialFolder Enumeration to get the file, like this:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\" + file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
However, if you're going to have files scattered throughout your system, you'd be better off storing the full path as #Plutonix indicates in his comment.
It looks like your code is looking in your applications path on the server while you want to look at the users desktop location.

Run-time Error 429 on Scripting.FileSystem object after scrrun.dll registered

There are quite a few entries on here about Error Code 429, but I want to make it clear that this is a slightly different question. This is a VBA excel program.
The code causing the problem :
Dim i As Integer
Dim j As Integer
Dim builder As String
Dim file As Object
Dim csv As Object
Set file = CreateObject("Scripting.FileSystem")
Set csv = file.CreateTextFile("configexport.csv")
The program breaks on the
Set file = CreateObject("Scripting.FileSystem")
I have a reference to scrrun.dll
I have registered scrrun.dll using an administrator command prompt and the command 'regsvr32 scrrun.dll'
I have restarted my computer
I am still seeing the error
NOTE: All other questions on this topic get resolved by this point so I do not consider this a duplicate. Any help would be appreciated.
I assume you meant FileSystemObject, and I wouldn't use file as your variable name:
Set aFile = CreateObject("Scripting.FileSystemObject")