I'm trying to create a program that repeatedly copies a file to test a flash drive's true size. Since overwriting a file isnt allowed, how would i name the files that go to the destination differently so that i could copy it multiple times?
I would append your loop variable to the end of your destination file name
Related
I encountered the following issue:
When accidentally passing a folder path to the Documents.Open function in VBA Word I get the runtime error 5174 as one would expect.
However all files with names that begin with an underscore get deleted in that moment from that folder.
To replicate:
Assume folder C:/Test/
In said folder have two files:
test.txt
_test.txt
In Word VBA execute the command:
Documents.Open("C:/Test/")
(As part of a subroutine or in the immediate window.)
Result: Runtime Error 5174 and _test.txt is now missing.
Note: Passing a nonexisting file like "C:/Test/abc.txt" or a wrong folder path like "C:/Test" (without the last slash) will not have this effect and won't cause the runtime error.
I have only tested this on one system on a network drive under windows 10 with Microsoft Professional Plus 2019. It's entirely possible that it's an issue with the file system. Does anyone have any suggestions as to why is this happening? I now included the workaround to check if I'm passing a folder, but it's still unnerving.
The Documents.Open method opens the specified document and adds it to the Documents collection. It is designed to take the file path, not a folder. If you want to allow users to pick file(s) you may consider using the file open dialog. The FileOpenDialog triggered by your code which opens a folder for picking files allows specifying file formats that should be supported and visible via the dialog window.
Set dlgSaveAs = Application.FileDialog(msoFileDialogFilePicker)
dlgSaveAs.Filter = "Text Files (.txt)|*.txt|Word Documents (.docx)|*.docx|Word Template (.dotx)|*.dotx|All Files (*.*)|*.*"
dlgSaveAs.ValidateNames = true
Res = dlgSaveAs.Show
I have a macro that asks the user to choose an excel file and then outputs two text files based on the data.
I am looking for a way to just drop the excel file onto the macro file and have it process without the need for opening the macro file, a command button, an open file dialog, etc. I would like to drop the file on the other file and just have the two text files output.
I saw something that looked promising using a VBS file, but was unable to get it to work.
Here's the bare bones of what you need to do:
Wscript.echo "Begin..." 'just letting you know it's working
Set objArgs = Wscript.Arguments 'capture arguments; arg 0 is the name of the dropped file
Wscript.echo "The file name you dropped is:" & objArgs(0)
'DO STUFF TO THE FILE HERE
Wscript.echo "...Finished" 'all done
Save this to a file with a "vbs" extension.
Drag and drop a file onto it.
If your Windows file associations are properly setup,
you'll see this output a message for each of the wscript.echo
lines.
Facing one issue....
Basically, I have a bouton on Word to launch a macro treatment. This treatment saves the open file, copies the open file from C:/ to Z:/, writes an XML file in Z:/, launches another treatment that will read both files in Z:/ and then, clears the files in Z:/.
C:/ is my local computer, Z:/ is a shared drive...
I managed to copy the file from C:/ to Z:/ with :
fsoObject.CopyFile ActiveDocument.Path & "\" & ActiveDocument.name, "Z:\" & ActiveDocument.name, True
Treatment goes well, but I'm completely unable to delete the file in Z:\ with this :
fsoObject.DeleteFile "Z:\*", True
I always get "Permission Denied"... Cannot delete it manually as well, except if I close ENTIRELY Word (and not only the file that was copied)...
Is there a way to avoid the file locking by Word ?
Thx !
Use a vba command to close the file after you have performed your operations. Then apply the delete command.
I have a large log file located on a network drive that is being constantly written to. How can I copy it through code without locking it?
Try
Microsoft.VisualBasic.FileSystem.FileCopy("sourcefile", "destinationfile")
Catch ex As Exception
' Handle Error
End Try
The code above, unfortunately does not work well, because while its copying the file, nothing can be written to that file.
Edit 1:
It was suggested to read the content of the file and write it to the output. I would prefer not to do this, because the file is several gigabytes. I reserve this option only as a last resort, only if there is no other way to copy the file without locking it and without readying the content.
how about using FileOpen in shared mode and reading and writing the output file yourself
FileOpen(1, "SourceFile", OpenMode.Input, OpenAccess.Default, OpenShare.Shared)
// open destination file here read and while not at end of sourcefile read it // and write blocks out to destinatioon file.
FileClose(1)
If you can't copy the file to work on it, or don't want to open the file in shared mode and then copy it that way, then another option is to have the system that generates the log file generate a series of smaller log files. Maybe one per unit, or production run, or hour, or day, or some other arbitrary break point.
Is there any way to overwrite an excel file when it is opened using vba code?
I think you can't overwrite a file that is open. This is a windows limitation and as far as I know it is not possible to do it.
So I guess you'll have to save it with a different name.
EDIT: unless you are the person that has the file open.
If you open the file via VBA code, just workbookname.save should work
You can overwrite a file that is open in read only mode. To achieve this you need to set the file's attributes to be read only (thus forcing everyone, who's opening it to have it open in read only mode).
This can be done either within the Windows OS (change the file's properties), or through VBA using the SetAttr function https://www.techonthenet.com/excel/formulas/setattr.php
From my own experimentation I reached the following code, which you can use as an example. Assuming you're attempting to save a copy of the currently open workbook to strFileLocation, which is a string variable holding the output file's path, where the file may or may not already exist:
' Verify that the file exists.
If Dir(strFileLocation) <> vbNullString Then
' For some reason the read only flag needs to be removed for VBA to be able to overwrite the file.
SetAttr strFileLocation, vbNormal
End If
' Do the actual (over)writing
ThisWorkbook.SaveCopyAs strFileLocation
' Set the read only flag once more
SetAttr strFileLocation, vbReadOnly
```