sueb 379
Contributor
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
-
5 2400 sueb 379
Contributor
(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!)
Rabbit 12,516
Recognized Expert Moderator MVP
You'll want to look into the Scripting.FileS ystemObject. It has functions and attributes for .FileExists(), .FolderExists() , .Subfolders, and .Files.
NeoPa 32,568
Recognized Expert Moderator MVP
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.
sueb 379
Contributor
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,568
Recognized Expert Moderator MVP
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 memory (a variable)
and use concatenation.
But, according to me, It isn't clean to load an entire 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 InstallExecuteSequence table
3. find the following entries and set them to false:
RegisterExtensionInfo
|
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 which add all word docs to one word
document.
Thanks in Advance
Maheshwari
| |
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 reformating,
after the merge. This is a full description of my needs.
I have a C# class library that creates two Crystal Reports, and then
exports them to the harddrive as Word documents. One's orientation is
landscape, the other is portrait. I then...
|
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 additional expense, though,
with single-licence costs of AU$790 for Aspose.Word and AU$526 for
Aspose.Pdf.
I've tried to use the Word-generated xml document (thinking that I might
be able to do something with microsoft.office.interop.word) as...
|
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 user and populated
(with info from an Access database) at run-time, then saved as Word
documents. The program I have coded works fine -- it does what I need
it to do. But it has two problems: (1) it runs very slowly, and (2) it
does not seem to...
|
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 the template in the future. So it doesn't work like a style sheet, for example. But, I digress, my question is: is there a means in Visual Basic, or using a macro, or anything else that can help me automate this? I have no doubt the boss will ask...
|
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 clears existing output.txt file first and then outputs the
result, while I need to append the result of my query without clearing
existing file content. Is it possible ?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |