In my Access database, I have a button on a form that copies a set of existing Word documents to a location (associated with the record on that form).
However, I don't want an existing file to be overwritten (as it currently happens), but rather I want to make a copy (filename + "(2)" or "(3)", etc., as appropriate).
Here's the code as it stands: - Function Public_Copy_Files(strChartNum As String, strAccount As String)
-
On Error GoTo Err_Public_Copy_Files
-
-
Dim strFileName As String
-
-
' Make the directories, if necessary
-
Public_Make_Directories strChartNum, strAccount
-
-
' Copy forms into Account folder
-
strFileName = "18-1.DOC"
-
FileCopy conFormSource & strFileName, _
-
conPath & strChartNum & "\" & strAccount & "\" & strFileName
-
strFileName = "FAX COVER.DOT"
-
FileCopy conFormSource & strFileName, _
-
conPath & strChartNum & "\" & strAccount & "\" & strFileName
-
strFileName = "Medi-Cal UR Review.doc"
-
FileCopy conFormSource & strFileName, _
-
conPath & strChartNum & "\" & strAccount & "\" & strFileName
-
strFileName = "UR FORM.DOC"
-
FileCopy conFormSource & strFileName, _
-
conPath & strChartNum & "\" & strAccount & "\" & strFileName
-
-
Exit_Public_Copy_Files:
-
Exit Function
-
-
Err_Public_Copy_Files:
-
MsgBox Err.Description
-
Resume Exit_Public_Copy_Files
-
-
End Function
-
In the following illustrative procedure I've assumed that conFormSource and conPath are set up to terminate with a backslash (\) char. - Public Function Public_Copy_Files(strChartNum As String, strAccount As String)
-
On Error GoTo Err_Public_Copy_Files
-
Dim strFromName As String, strToName As String, strFileList As String
-
Dim intX As Integer
-
-
' Make the directories, if necessary
-
Call Public_Make_Directories(strChartNum, strAccount)
-
-
' Loop through and note all .DOC files found in conFormSource
-
strFromName = Dir(conFormSource & "*.DOC")
-
Do While strFromName > ""
-
strFileList = strFileList & "," & strFromName
-
strFromName = Dir
-
Loop
-
-
' For each .DOC file found copy to destination
-
Do While strFileList > ""
-
intX = InStrRev(strFileList, ",")
-
strFromName = Mid(strFileList, intX + 1)
-
strFileList = Left(strFileList, intX - 1)
-
' If file already exists then add "(2)" to the end
-
strToName = conPath & strChartNum & "\" & strAccount & "\" & strFromName
-
If Dir(strToName) > "" Then
-
intX = InStrRev(strToName, ".")
-
strToName = Left(strToName, intX - 1) & "(2)" & _
-
Mid(strToName, intX)
-
End If
-
Call FileCopy(conFormSource & strFromName, strToName)
-
Loop
-
-
Exit_Public_Copy_Files:
-
Exit Function
-
-
Err_Public_Copy_Files:
-
MsgBox Err.Description
-
Resume Exit_Public_Copy_Files
-
-
End Function
Basically, it first lists the matching files found in conFormSource (into strFileList). Next it loops through the list and checks, for each item, whether the same-named file exists in the destination folder. If it does, it simply adds the text "(2)" just before the ".DOC" bit at the end. It then takes the resultant filename and copies the original file to that. Each iteration of the loop processes a different file until there is none left.
5 2386
(in addition, i'd love it if i could avoid hard-coding the filenames, but make this code smart enough to simply find each each document it in the "source location", and make a copy in the new location!)
You'll want to look into the Scripting.FileSystemObject. It has functions and attributes for .FileExists(), .FolderExists(), .Subfolders, and .Files.
NeoPa 32,556
Expert Mod 16PB
In the following illustrative procedure I've assumed that conFormSource and conPath are set up to terminate with a backslash (\) char. - Public Function Public_Copy_Files(strChartNum As String, strAccount As String)
-
On Error GoTo Err_Public_Copy_Files
-
Dim strFromName As String, strToName As String, strFileList As String
-
Dim intX As Integer
-
-
' Make the directories, if necessary
-
Call Public_Make_Directories(strChartNum, strAccount)
-
-
' Loop through and note all .DOC files found in conFormSource
-
strFromName = Dir(conFormSource & "*.DOC")
-
Do While strFromName > ""
-
strFileList = strFileList & "," & strFromName
-
strFromName = Dir
-
Loop
-
-
' For each .DOC file found copy to destination
-
Do While strFileList > ""
-
intX = InStrRev(strFileList, ",")
-
strFromName = Mid(strFileList, intX + 1)
-
strFileList = Left(strFileList, intX - 1)
-
' If file already exists then add "(2)" to the end
-
strToName = conPath & strChartNum & "\" & strAccount & "\" & strFromName
-
If Dir(strToName) > "" Then
-
intX = InStrRev(strToName, ".")
-
strToName = Left(strToName, intX - 1) & "(2)" & _
-
Mid(strToName, intX)
-
End If
-
Call FileCopy(conFormSource & strFromName, strToName)
-
Loop
-
-
Exit_Public_Copy_Files:
-
Exit Function
-
-
Err_Public_Copy_Files:
-
MsgBox Err.Description
-
Resume Exit_Public_Copy_Files
-
-
End Function
Basically, it first lists the matching files found in conFormSource (into strFileList). Next it loops through the list and checks, for each item, whether the same-named file exists in the destination folder. If it does, it simply adds the text "(2)" just before the ".DOC" bit at the end. It then takes the resultant filename and copies the original file to that. Each iteration of the loop processes a different file until there is none left.
Oh, this is beautiful and (naturally) works exactly right!
However, now, of course, I want to go a little further. Thinking about some of my more unsophisticated users, I'd like to modify this so that, for every click of the "copy files" button, a separate set of files are created; i.e., the third time they click that button without checking to see what happened in the Explorer window, they get "filename (3).doc".
I'd like to change lines 23 through 27 to something like: - Do While Dir(strToName) > ""
-
intX = InStrRev(strToName, ".")
-
strToName = Left(strToName, intX - 1) _
-
& "(" & #LOOPINDEX# & ")" _
-
& Mid(strToName, intX)
-
Loop
-
Of course, I can manually manage a loop index, but isn't there one already available? (you see how lazy i am...)
NeoPa 32,556
Expert Mod 16PB
There is a For N = X To Y Step Z ... Next N construct, but I don't really see that fitting what you describe. Does it even make sense to choose a different number depending on how many times the request has been made? Should the count be session related, or should it continue one day where it left off the previous one?
Frankly, I'm not sure exactly what you want. The solution must match the requirement, and I don't understand what that is.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ellixis |
last post by:
Hello,
How can I use fwrite() and fseek() in order to write data in the
middle (or anywhere else) of a file without overwriting existing data
?
People told me that I should load the file into...
|
by: sudha |
last post by:
Hi
I need to write a c# program which has to merge selected
word documents into one word document.
Kindly advise the efficient way to achieve this.
Thanks in advance
sudha
|
by: Stewart |
last post by:
is there any way this can be done? I've looked at the help files and
checked out as many tutorials as i could find on the net (as always) but no
joy.
thanks
|
by: Chance Lerro |
last post by:
Here's another way to PREVENT the install program for run-time Access
from
overwriting existing file associations:
1. Edit the Runtime MSI file with Orca, InstallShield, etc..
2. Go to the...
|
by: Maheshwari |
last post by:
Hi
Can you please help with some info on how to open
existing word documents / merge two word documents from a
C# program ?
My user will select 2 or many word docs and i have to
create a c# exe...
|
by: mharris |
last post by:
I need help with merging two Word documents into one through C#
code. The problem isn't so much getting the documents put into one as
it is maintaining the appropriate formatting, or rather...
|
by: Sharon |
last post by:
Following up, I've found success at last with Aspose, combining both
Aspose.Word to create an xml file, and then and Aspose.Pdf to create the
pdf file.
This works out to be quite a considerable...
|
by: Dave |
last post by:
Apologies for the newbie question. I have created a vb.net program for
my company that is designed to work with Word Templates (about forty of
them that we commonly use) that are selected by the...
|
by: bbasberg |
last post by:
Hello,
I have been asked to move a paragraph to a new location in over 360 Word documents. I was shocked to find out that changing an attached template will only affect documents produced from...
|
by: mamin |
last post by:
Hi,
I need to output result of my query to txt file. So I'm using -o
parameter, for example:
osql.exe -s (local) -d database1 -U sa -P sa -i 'c:\\queryFile.sql' -o
'c:\\output.txt'
But it...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |