473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to copy Word documents to a location without overwriting existing file?

sueb
379 256MB
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:

Expand|Select|Wrap|Line Numbers
  1. Function Public_Copy_Files(strChartNum As String, strAccount As String)
  2. On Error GoTo Err_Public_Copy_Files
  3.  
  4.     Dim strFileName As String
  5.  
  6.     ' Make the directories, if necessary
  7.     Public_Make_Directories strChartNum, strAccount
  8.  
  9.     ' Copy forms into Account folder
  10.     strFileName = "18-1.DOC"
  11.     FileCopy conFormSource & strFileName, _
  12.              conPath & strChartNum & "\" & strAccount & "\" & strFileName
  13.     strFileName = "FAX COVER.DOT"
  14.     FileCopy conFormSource & strFileName, _
  15.              conPath & strChartNum & "\" & strAccount & "\" & strFileName
  16.     strFileName = "Medi-Cal UR Review.doc"
  17.     FileCopy conFormSource & strFileName, _
  18.              conPath & strChartNum & "\" & strAccount & "\" & strFileName
  19.     strFileName = "UR FORM.DOC"
  20.     FileCopy conFormSource & strFileName, _
  21.              conPath & strChartNum & "\" & strAccount & "\" & strFileName
  22.  
  23. Exit_Public_Copy_Files:
  24.    Exit Function
  25.  
  26. Err_Public_Copy_Files:
  27.     MsgBox Err.Description
  28.     Resume Exit_Public_Copy_Files
  29.  
  30. End Function
  31.  
Mar 23 '11 #1

✓ answered by NeoPa

In the following illustrative procedure I've assumed that conFormSource and conPath are set up to terminate with a backslash (\) char.
Expand|Select|Wrap|Line Numbers
  1. Public Function Public_Copy_Files(strChartNum As String, strAccount As String)
  2. On Error GoTo Err_Public_Copy_Files
  3.     Dim strFromName As String, strToName As String, strFileList As String
  4.     Dim intX As Integer
  5.  
  6.     ' Make the directories, if necessary
  7.     Call Public_Make_Directories(strChartNum, strAccount)
  8.  
  9.     ' Loop through and note all .DOC files found in conFormSource
  10.     strFromName = Dir(conFormSource & "*.DOC")
  11.     Do While strFromName > ""
  12.         strFileList = strFileList & "," & strFromName
  13.         strFromName = Dir
  14.     Loop
  15.  
  16.     ' For each .DOC file found copy to destination
  17.     Do While strFileList > ""
  18.         intX = InStrRev(strFileList, ",")
  19.         strFromName = Mid(strFileList, intX + 1)
  20.         strFileList = Left(strFileList, intX - 1)
  21.         ' If file already exists then add "(2)" to the end
  22.         strToName = conPath & strChartNum & "\" & strAccount & "\" & strFromName
  23.         If Dir(strToName) > "" Then
  24.             intX = InStrRev(strToName, ".")
  25.             strToName = Left(strToName, intX - 1) & "(2)" & _
  26.                         Mid(strToName, intX)
  27.         End If
  28.         Call FileCopy(conFormSource & strFromName, strToName)
  29.     Loop
  30.  
  31. Exit_Public_Copy_Files:
  32.    Exit Function
  33.  
  34. Err_Public_Copy_Files:
  35.     MsgBox Err.Description
  36.     Resume Exit_Public_Copy_Files
  37.  
  38. 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
sueb
379 256MB
(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!)
Mar 23 '11 #2
Rabbit
12,516 Expert Mod 8TB
You'll want to look into the Scripting.FileSystemObject. It has functions and attributes for .FileExists(), .FolderExists(), .Subfolders, and .Files.
Mar 23 '11 #3
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.
Expand|Select|Wrap|Line Numbers
  1. Public Function Public_Copy_Files(strChartNum As String, strAccount As String)
  2. On Error GoTo Err_Public_Copy_Files
  3.     Dim strFromName As String, strToName As String, strFileList As String
  4.     Dim intX As Integer
  5.  
  6.     ' Make the directories, if necessary
  7.     Call Public_Make_Directories(strChartNum, strAccount)
  8.  
  9.     ' Loop through and note all .DOC files found in conFormSource
  10.     strFromName = Dir(conFormSource & "*.DOC")
  11.     Do While strFromName > ""
  12.         strFileList = strFileList & "," & strFromName
  13.         strFromName = Dir
  14.     Loop
  15.  
  16.     ' For each .DOC file found copy to destination
  17.     Do While strFileList > ""
  18.         intX = InStrRev(strFileList, ",")
  19.         strFromName = Mid(strFileList, intX + 1)
  20.         strFileList = Left(strFileList, intX - 1)
  21.         ' If file already exists then add "(2)" to the end
  22.         strToName = conPath & strChartNum & "\" & strAccount & "\" & strFromName
  23.         If Dir(strToName) > "" Then
  24.             intX = InStrRev(strToName, ".")
  25.             strToName = Left(strToName, intX - 1) & "(2)" & _
  26.                         Mid(strToName, intX)
  27.         End If
  28.         Call FileCopy(conFormSource & strFromName, strToName)
  29.     Loop
  30.  
  31. Exit_Public_Copy_Files:
  32.    Exit Function
  33.  
  34. Err_Public_Copy_Files:
  35.     MsgBox Err.Description
  36.     Resume Exit_Public_Copy_Files
  37.  
  38. 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.
Mar 23 '11 #4
sueb
379 256MB
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:

Expand|Select|Wrap|Line Numbers
  1.         Do While Dir(strToName) > ""
  2.             intX = InStrRev(strToName, ".")
  3.             strToName = Left(strToName, intX - 1) _
  4.                         & "(" & #LOOPINDEX# & ")" _
  5.                         & Mid(strToName, intX)
  6.         Loop
  7.  

Of course, I can manually manage a loop index, but isn't there one already available? (you see how lazy i am...)
Mar 24 '11 #5
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.
Mar 24 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
2
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
8
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
1
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...
0
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...
0
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...
0
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...
7
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...
2
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...
1
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...
0
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
0
BarryA
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...
1
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...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.