Changing the Current Drive

  • Thread starter Montana DOJ Help Desk
  • Start date
M

Montana DOJ Help Desk

Word 2000

I have the code shown below. This code works as long as the current drive
is the same as the drive letter that is stored as part of the document
variable (which contains the full path to the folder where I want to store
the documnet). However, I have discovered a bug with this code. If the
current drive is DIFFERENT from the drive letter that is part of the
document variable, then the code blows up because ChDir does not change the
drive letter.

I've been reading in Help about ChDrive, GetAbsolutePathName, and
GetDriveName. With that information, I think that I'm close to solving this
problem, but I haven't been able to get it working yet, and I've run out of
time for tonight. Can some show me how I can get the current drive changed
so that it matches the drive letter from the full path name stored in the
document variable?

Sub FileSave()

Dim wdFileOpenDirectory As String

On Error GoTo ErrorHandler

' Stores the default directory for opening files, sets DocPath to the value
stored in the
' LocatesDocFullPath document variable, and then changes the default
directory. If the
' LocatesDocFullPath document variable does not exist, or if the variable
does exist, but
' contains the name of a folder that does not exist, then an error will be
generated and code
' execution will branch to ErrorHandler.
wdFileOpenDirectory = CurDir
DocPath = ActiveDocument.Variables("LocatesDocFullPath").Value
ChDir DocPath

' Saves the file as Locates.doc and restores the default file open directory
for Word.
ActiveDocument.SaveAs FileName:="Locates.doc", FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
ChangeFileOpenDirectory wdFileOpenDirectory

Exit Sub

' Allows the user to specify the folder for the Locates document and saves
that folder path in the
' LocatesDocFullPath document variable.
ErrorHandler:

' If the folder name contained in DocPath does not exist (Error 76), or the
LocatesDocFullPath
' document variable does not exist (Error 5825), then the user is given the
opportunity to specify
' the path to Locates.doc.
If Err.Number = 76 Or Err.Number = 5825 Then
With Dialogs(wdDialogFileSaveAs)
.Name = "Locates.doc"

' If the Save button is clicked then the folder selected by the user
is stored in the
' LocatesDocFullPath document variable and in DocPath. Otherwise,
execution is halted.
If .Display = -1 Then
ActiveDocument.Variables("LocatesDocFullPath").Value =
CurDir
DocPath = CurDir
Else
End
End If

End With
End If

End Sub


-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 
W

Word Heretic

G'day "Montana DOJ Help Desk" <[email protected]>,

I use ChDrive FullPathname, it ignores the trailing stuff.



Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Montana DOJ Help Desk reckoned:
 
M

Montana DOJ Help Desk

Steve,

That's basically what I am trying to do. Here's the latest version of my
code (I took out the error handler, and abbreviated the comments in the
interest of making this shorter). Note that CurDir = "C:\Documents and
Settings\cjin21$admin\My Documents" at the top of the routine, and that the
value stored in the LocatesDocFullPath document variable = "M:\Help
Desk\Locates".

***************
Sub FileSave()

Dim FSO As Object
Dim TargetDrive As String
Dim wdFileOpenDirectory As String

On Error GoTo ErrorHandler

' Stores the default directory for opening files and reads the path from the
LocatesDocFullPath document variable.
wdFileOpenDirectory = CurDir
DocPath = ActiveDocument.Variables("LocatesDocFullPath").Value

' Creates a file system object and sets the value of TargetDrive based on
the value of DocPath, which comes
' from the value stored in the LocatesDocFullPath document variable.
Set FSO = CreateObject("Scripting.FileSystemObject")
TargetDrive = FSO.GetAbsolutePathName(DocPath)
TargetDrive = FSO.GetDriveName(FSO.GetAbsolutePathName(DocPath))

' Changes the active drive and folder.
ChDrive DocPath
ChDir DocPath

' Saves the file as Locates.doc, restores the default file open directory
for Word, and releases the system
' resources allotted to the file system object.
DocPath = DocPath & "\Locates.doc"
ActiveDocument.SaveAs FileName:=DocPath, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
ChangeFileOpenDirectory wdFileOpenDirectory
Set FSO = Nothing

End Sub
***************

If I put watches on DocPath, TargetDrive, and CurDir, and then step through
the code, it looks like the code works because CurDir gets set to the path
stored in the document variable. However, after the ActiveDocument.SaveAs
command has been executed CurDir immediately reverts to it's original value
("C:\Documents and Settings\cjin21$admin\My Documents"). I just finished
this code and have had very little time for testing. The first few tests
seemed to have mixed results--sometimes it seemed to work, and other times
it didn't seem to work. However, in the last 5 or 6 tests it seems to be
working. I have to knock of for tonight, and will do some more testing
later. Until then, does anyone see any bugs in this code?

FYI: I know that I don't need both "TargetDrive =" commands. I've got them
both in there for now because I don't think that it hurts anything to have
both commands, and because I can comment out one or the other as needed.

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 
W

Word Heretic

G'day "Montana DOJ Help Desk" <[email protected]>,

You have a hundred lines of suss code before your ChDrive. You are
looking for a doc var to get the fullpath? Why? Why not just
ActiveDocument.path?

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Montana DOJ Help Desk reckoned:
 
M

Montana DOJ Help Desk

What do you mean by "suss code?" I've not heard that term before.

I need to control the drive, path, and document name to which the file is
saved. This document will be used by a number of people (but only by one
person per shift), and it is very important that the document be stored in a
centralized location, and that each employee accesses the same document.
This document will contain confidential law enforcement data, and it's one
of those job duties that we really can't afford to screw up. If we do screw
it up, it could result in a person being wrongfully arrested. If I were to
use ActiveDocument.Path, the user would be able to move the file and have it
save to the new folder, which would result in multiple copies of the file
being used, with each copy containing different data, and that would
eventually lead to the afore mentioned screw up.

We could train the users not to make copies of the file. The problem there
is that we have a number of employees who are prone to copying files off
onto their personal network drives--even after they've been told not to do
so--and this makes the data stored in the file available to only that one
employee. Also, we'd be relying on the user to remember a year (or more)
from now that they are not suppose to make copies of the file. Finally,
we'd be relying on supervisors to train new users not to make copies of the
document, and based on my observations regarding the training given to new
users around here, that just ain't gonna happen.

So I figure that it's best to take the human element out of the equation and
force the drive, folder, and name that will be used when saving the
document. I store the full path name in a document variable, and read the
value from that variable each time the document is saved. By reading the
path from the document variable, it will save to the correct folder no
matter what the user tries to do (short of disabling the FileSave routine).
This behavior is explained in the Help that I've built for the document, and
in a "Quick Help" dialog box that the user can open at any time

If the folder stored in the document variable does not exist, or if the
document variable itself does not exist, then the error handler for the
routine (which, in the interest of brevity, I didn't include in the previous
post, but which can be seen in the first message in this thread) will prompt
the user to specify where to save the document, and will store the new
location in the document variable.

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 
H

Helmut Weber

Hi Tom,
I am administrating more than 300,000 Docs, and I think, the key to
it all is a name system (fullname) that defines the location where it
should be stored. I am intercepting filenew, filesave, filesaveas
and even printing, and, if the fullname applies to a certain
convention, the file will be saved to a certain network drive, etc,
no matter what a user will try. All my macros, some 100 or so, check
fullname and execute only, if the name applies to the convention. So,
if the user stores the file somewhere else or renames it on operating
system level, which is the same in a way, all automation is stopped.
In addition, the network drive is monitored and all files, that do not
meet the required file name pattern, will be removed.
If you got a name pattern, you could even prevent editing or opening
a doc at all, or shutting word down as soon as the name is changed.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
M

Montana DOJ Help Desk

Helmut,

Thanks for the information!

300,000 docs! Holy sh**! :) I'm administering, like, 15.

I am currently intercepting FileSave, FileSaveAs, and printing.
Intercepting FileNew did occur to me, so I will have to do some testing on
that, but I'm thinking that it won't really apply on this particular
project. The code in the project I am now working on has the file name
hard-coded, so it should be protected against file renaming at the OS level.

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
,.> Hi Tom,
 
W

Word Heretic

G'day "Montana DOJ Help Desk" <[email protected]>,

suss-pect


Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Montana DOJ Help Desk reckoned:
 
M

Montana DOJ Help Desk

Ahh! That's what I figured, but just wanted to make sure I wasn't missing
something.

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top