Is there an argument for copyfile that will change the hidden property? - vba

I have a database that writes data into a copied excel template. The template is hidden to keep the end user from tampering with it, however the final result is also hidden. Is there a way to change the hidden property when saving the new file?
Currently, the db copies the template and renames it.
fso.CopyFile "C:\Upload\Rebate_Upload_Files\Standard Form (Template)
protected.xlsx", "C:\Upload\Rebate_Upload_Files\Rebate Contract " &
Contract_Number & " " & Date$ & ".xlsx"
After that, it transfers the appropriate table and saves the file.
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml,
"export_table", "C:\Upload\Rebate_Upload_Files\Rebate Contract " &
Contract_Number & " " & Date$ & ".xlsx", False, "A12:L65000"
The process works fine, except that the final file is also hidden and I'd like it to be a normal file.
Thanks

Not for CopyFile-which is a FileSystemObject method, but there is one for a File object. We'll just update it after the copy is complete.
For simplicity i've replaced your file output path to a string variable.
originalFileName = "yourStartingFile"
copyFileName = "yourCopiedFile"
set fso.CopyFile OriginalFileName, CopyFileName
--after copying, get file that was copied
--set attributes value of file to 0. 0 = Normal, 2 = Hidden
f = fso.GetFile(copyFileName)
f.attributes = 0
More reading for additional details.
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/file-object
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/attributes-property

Mike's code above worked, however there was a few more steps involved for me so I wanted to provide the full code for anyone stumbling upon this.
First, in order to use FileSystemObject, you need to enable it in your VBA Editor by going to Tools->References and enabling "Microsoft Scripting Runtime". Then, using the code below, you can copy a hidden file and set the new file (via f.Attributes) to not be hidden:
' SourceFile is the full path name to my original file
' FileNAme is the full path name to my new file
Dim fso As Scripting.FileSystemObject
Dim f As File
Set fso = New Scripting.FileSystemObject
Call fso.CopyFile(SourceFile, FileName, False) ' Set to true to overwrite
Set f = fso.GetFile(FileName)
f.Attributes = 0

Related

VBA FileExists and Sharepoint

I'm running into issues trying to pull info from files stored in Sharepoint.
Namely, FileExists isn't working and Overwrite file doesn't seem to be working either.
There was a discussion here, but few answers -> posting this question again in hopes some things have changed
My code runs like this:
strFileExists = Dir(Filepath & Filename)
And returns: File path not found -> I checked the path and even opened a file and recorded the macro to make sure it was the same file path without issue, but it appears DIR() is the issue.
The business dept I'm working with is entirely switching over to Sharepoint so hoping there's a straightforward solution without setting up network shares or doing C/personal/OneDrive things
You can navigate and look for files on OneDrive like this
Sub check_File_Exists()
Dim path As String
Dim strType As String
Dim file As Variant
Dim yourFile As String
'replace uname with your user name
path = "C:\Users\uname\OneDrive\"
strType = "*txt"
yourFile = "test.txt"
file = Dir(path & strType)
Do While (file <> "")
If file = yourFile Then
Debug.Print ("File: " & file & " found!")
Exit Do
End If
file = Dir
Loop
End Sub
Hope it helps

Copy file with progress bar bypass file replacement confirmation

This is a follow up to this question and great answer:
Copy files with progress bar
So I added the code from Siddharth Rout's answer and it does exactly what I want to happen with a minor exception. When I copy the files, I am looping through each file in the directory and copying it up as long as it is not *List.xml. Because I am replacing an existing library the 97% of the documents are pre-existing and I get prompted to replace existing documents each time.
Is there a way to get it to prompt me to choose to replace for all files? Do I need to reformat/structure the sequence of my code?
Function UploadToSharepoint(Folderpath As String, Foldername As String, Filenames() As String, SharepointLinks() As String) As Boolean
'upload file to sharepoint library based on the folder name
Dim SharePointLib As String
Dim LocalAddress As String
Dim DestinationAddress As String
Dim xCounter As Long
On Error GoTo loadFailed
Pickafolder:
Folderpath = FolderPick
Foldername = Left(Folderpath, Len(Folderpath) - 1)
Foldername = RIght(Foldername, Len(Foldername) - InStrRev(Foldername, "\"))
Select Case Foldername
Case "OPSS", "SSP", "OPSD", "MTOD", "SSD"
SharePointLib = "\\my.company.com\Subsite\" & Foldername & "\"
Case "West", "Eastern", "Northeastern", "Northwestern", "Head Office"
SharePointLib = "\\my.company.com\Subsite\NSP\" & Foldername & "\"
Case "NSP", "NSSP"
MsgBox "Pick the NSP regional sub folder: West, Eastern, Northeastern, Northwestern, Head Office"
GoTo Pickafolder
Case Else
MsgBox "Inappropriate directory to upload from. Please select one of the CPS download directories"
GoTo Pickafolder
End Select
Filenames = GetFilesDir(Folderpath)
ReDim SharepointLinks(LBound(Filenames) To UBound(Filenames))
For xCounter = LBound(Filenames) To UBound(Filenames)
LocalAddress = Folderpath & Filenames(xCounter)
DestinationAddress = SharePointLib & Filenames(xCounter)
'**********************************************************
Call VBCopyFolder(LocalAddress, DestinationAddress)
'**********************************************************
SharepointLinks(xCounter) = "#http:" & Replace(DestinationAddress, "\", "/") & "#"
Next xCounter
UploadToSharepoint = True
Exit Function
loadFailed:
UploadToSharepoint = False
End Function
And by the looks of things I am not excluding the file I was referring to earlier...must be doing that else where.
Update
Based on comment received at the linked question, the solution is to declare a public constant at the start:
Public Const FOF_NOCONFIRMATION As Long = &H10
and then in the copy procedure change the line of code to:
.fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION
Now, this does solve the problem of being constantly asked to confirm the replacement. I am very happy about this. The problem now is the progress window displays for the first file to be copied then disappears but fails to reappear for subsequent files. The remaining files still get copied and the prg carries on like it's supposed to. The whole point of the progress bar though was to let people know that "THINGS" were still happening in the background and now that is not happening. Is there something I need to adjust?
Update 2
After running my code and choosing a source directory on the network drive instead of the local computer, the copy window is popping up for every single file like I was expecting. I notice that sometimes the progress bar closes before reaching 100%. This leads me to believe that since the file sizes are so small that when it is copying from my local drive to sharepoint, the operation completes so fast that it does not have time to draw and update the progress window before its time to close it.

VBA open a csv file with unknow file name

I have got a rather simple question as i think but i couldnt find out it myself.
I want to open a csv File in a defined binder but with an unknow filename. I would asume that it should work with simply "path/*.csv" however it is not :( The errormessage says "Wrong Filename". Do i need to use something else in VBA.
path = ActiveWorkbook.path & "\input\"
Open path & "*.csv" For Binary As #1
The above code does not work for me :( The CSV is called xyz.csv
path = ActiveWorkbook.path & "\input\"
Open path & "xyz.csv" For Binary As #1
The code above is working however i have fix added the csv filename, which in this case is xyz.
Somebudy knows how to get that thing to work?
Cheers and thx for your time
Marc
Dim path As String
Dim csvFiles As String
path = ActiveWorkbook.path & "\input\"
csvFiles = Dir(path & "*.csv")
Do While Len(csvFiles) > 0
Debug.Print csvFiles
csvFiles = Dir
Loop
You can use the Dir() Function to check the files in your folder if you don't know the filename.

Shell.namespace not accepting string variable, but accepting string itself

I have a code, in which I want to loop through files in a folder, check their built-in or custom document properties (without opening them), and later open those, which are meant to be open.
To do this, I'm using Shell and to set the folder I'm using Shell.Namespace.
The problem is with the Namespace, I guess. When I use a variable strSuborCesta for the path it doesn't work. When I print the variable strSuborCesta into immediate window and use the printed string inside the Shell.Namespace("....") it does work.
By it doesn't work I mean I get:
run-time error : 91 Object variable or With block not set
when I try to loop through the files in folder (which is not set in that case, so I understand why the error occurred, but don't understand why it's not accepting a string variable)
The path is correct in both ways. But I need it to be a variable, not a hardcoded string.
Where do I error?
Is there any better way, to check document properties (like comments, title, author, etc.) without opening the Excel files?
Below is the section (currently just in testing phase) that is giving me a hard time.
str[name of variable] variables are string data types. sFile, oShell, oDir are as Variants
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'get a root path
strPriecinokCesta = ThisWorkbook.Path 'path to this file
strPriecinokCesta = Left(strPriecinokCesta, Len(strPriecinokCesta) - (Len(strPriecinokCesta) - InStrRev(strPriecinokCesta, "\"))) 'root path is one level above this file
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'input files are in a subfolder
strSuborCesta = strPriecinokCesta & "Zdroje\"
strSuborPripona = "Formular_BD_kotolna*.xls" 'name of a file with extension
strSuborNazov = Dir(strSuborCesta & strSuborPripona) 'actual file name
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'get access to the file system
Set oShell = CreateObject("Shell.Application")
Set oDir = oShell.Namespace(strSuborCesta) '<----this will produce an error. In contrast to using a hard coded string. Why?
For Each sFile In oDir.Items '<---- run time error 91 occurs on this line
Debug.Print test & " : " & oDir.GetDetailsOf(sFile, 24) 'comments
Next
Variable strSuborNazov should be a variant
Dim strSuborNazov as variant
oShell.Namespace(strSuborNazov)
https://msdn.microsoft.com/en-us/library/windows/desktop/bb774085(v=vs.85).aspx

Vb file exists script

I have this script but would like to enhance it that in the absence of the file on the C: copy the one from the R: Drive. Currently on check if file is there.
Currently this script will run on multiple files in a single folder \SCRIPTS.
As I repeat the statement for each of different file name (I do know the file names) A more economic way of checking all files in the the R:\SCRIPTS and comparing to the C:\SCRIPTS copying or overwriting file would be good if anyone has a snippet that might help
Const OverwriteExisting = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLocalFile = objFSO.GetFile("C:\SCRIPT\SCRIPTTEXT.txt")
dtmLocalDate = objLocalFile.DateLastModified
Set objServerFile = objFSO.GetFile("R:\SCRIPT\SCRIPTTEXT.txt")
dtmServerDate = objServerFile.DateLastModified
If dtmLocalDate < dtmServerDate Then
objFSO.CopyFile objServerFile.Path, objLocalFile.Path, OverwriteExisting
End If
using System.IO;
Dim ServerFolder As New IO.DirectoryInfo("R:\SCRIPT")
Dim LocalFolder As New IO.DirectoryInfo("C:\SCRIPT")
For Each ServerFile In ServerFolder.GetFiles
If IO.File.Exists(LocalFolder.FullName & "\" & ServerFile.Name) Then
Dim LocalFile As New IO.FileInfo(LocalFolder.FullName & "\" & ServerFile.Name)
If ServerFile.LastWriteTime > LocalFile.LastWriteTime Then
IO.File.Copy(ServerFile.FullName, LocalFile.FullName, True)
End If
Else
IO.File.Copy(ServerFile.FullName, LocalFolder.FullName & "\" & ServerFile.Name)
End If
Next