View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
GS[_2_] GS[_2_] is offline
external usenet poster
 
Posts: 3,514
Default Automatically copy with save?

Auric__ has brought this to us :

Let me clarify, then. My main biz workbook is in several places: a copy is on
my workstation (and irrelevant to this problem), a copy is on my server (also
irrelevant), and the main working location of the workbook is on my tablet on
an SD card (E:) with a copy on a USB drive (F:).

Before I wrote the code I posted previously, I was manually copying the
workbook from E: to F:. (Sometimes I manage to open the workbook from F:
instead of E:, therefore the part of my code that checks which drive the book
lives in. It also takes into consideration the fact that I might end up with
a copy on, say, the hard drive in the future.)

What my code does is automate the copying. Part of my problem is that the
only place I know of to put the code to do so is in Workbook_BeforeSave,
which of course runs *before* the actual save. What I want is to do the
copying *after* the save, so that the same version of the workbook is on both
E: and F:. To do so requires the gyrations I mentioned in my previous post.


You can do this in the BeforeClose event so all you have to do when
you're finished working in the file is click the close button.
Alternatively, you can us a sub (located in a standard module) named
"Auto_Close". This is my preference as there's various reasons why code
in the ThisWorkbook component may not always work as expected. I doubt
this to be possible with trivial projects, but since most of my stuff
is fairly complex I can't take any chances that shutdown code might not
execute properly.

ThisWorkbook.Path is the location of the file running the code.


I use Me instead of ThisWorkbook simply because it's less typing. In context,
they're identical.


As mentioned, I use an Auto_Close sub and so the "Me" keyword doesn't
work there!<g

You could open a folder browser dialog so you can select the path to
save to. This obviates having to hard code path/drive letters and
folders, and would simplify coding as well as give you some
flexibility.


Hardcoding the paths is not a concern in this specific instance. I'm the only
user of this tablet, and I decide what letters are assigned to what drives.


As for using Save vs SaveAs, I considered using SaveCopyAs, but since I rely
on timestamps for various purposes (which are unimportant here), I *really*
want them identical.


It sounds pretty straight forward, then, that you simply need save one
and copy it to the other drive[s]...

Dim sPath$, sCopyPath
sPath = ThisWorkbook.Path
If Right(sPath, 1) < "\" Then sPath = sPath & "\"

With ThisWorkbook
.Save
Select Case Left(sPath, 1)
Case "E"
sCopyPath = Replace(sPath, "E", "F")

Case "F"
sCopyPath = Replace(sPath, "F", "E")
End Select 'Case Left(sPath, 1)
.SaveCopyAs sCopyPath & ThisWorkbook.Name

...where you can insert as needed if you want to include more locations.
In the case of multiple drives, replace the Select Case with something
like...

Const sSaveToDrives$ = "C,D,E,F" '//edit to suit
Dim vDrv, sDrv$

sDrv = Left(sPath, 1)
For Each vDrv in sSaveToDrives
If vDrv < sDrv Then _
.Save¤CopyAs Replace(sPath, sDrv, vDrv) & ThisWorkbook.Name
Next 'vDrv

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion