473,657 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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:

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
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!)
Mar 23 '11 #2
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.
Mar 23 '11 #3
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.
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 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:

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,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.
Mar 24 '11 #6

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

Similar topics

1
7380
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
2
359
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
3289
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
1759
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
0
1361
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
0
6297
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...
0
1434
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...
7
6237
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...
2
1393
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...
1
6225
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 ?
0
8407
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, 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...
0
8837
Oralloy
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...
1
8512
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,...
0
7347
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, 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...
1
6175
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 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...
0
5638
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();...
0
4171
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...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.