Hi Paul
The code below will insert the name of the folder containing the active
document into the footer. You will need to change this to suit your
needs. First, a user could re-run this code many times and thus insert
the text into the footer many times. Second, you may have existing
footer text, so you may want to insert the folder name before or after
existing text. One way to cope with this may be to create a bookmark in
the document called, say, "FolderName". Then, insert the text into that
bookmark. To do that successfully, see
Inserting text at a bookmark without deleting the bookmark
http://www.mvps.org/word/FAQs/MacrosVBA/InsertingTextAtBookmark.htm
However, the real issue here is: where are you going to store this
macro? You probably don't want to store it in the document, for two
reasons. First, it will trigger a macro security warning. Second, if you
need to update the code, you would need to change every document that
holds that code. Ideally, put this code in the *template* on which these
documents are based. Ensure that all users' machines have access to that
template (eg it could be in a folder on a server, and each person's
machine sets Tools > Options > File Locations > Workgroup Templates to
that folder).
Now the question is: how or when is this code going to run? There is no
use getting Word to run the code each time it creates a new document
from the template (because at that point the new document has no
folder). You could, however, tell Word to run the code each time it
*opens* a document based on this template. But that will be no good if
people send the documents to one another (because if I email you my
document and you open that document, it's now in a different folder, and
Word will change the footer to reflect the folder the document is now
in). You could get Word to run the code automatically each time the
document closes, although that has its problems, too. The easiest way
would be to create a toolbar in the template, attach the macro to a
button on that toolbar, and tell everyone to click the button to run the
code.
(The real solution to this, by the way, is not to distribute documents
to individuals. Rather, create a template and tell everyone to do File >
New and create their own document from that template. In the
Document_New event, force the user to save the document and then insert
the folder name.)
By the way, as it stands, the code won't work for documents opened from
a web site; and it won't cope if documents are saved in the root
directory (eg at c:\).
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
Melbourne, Australia
Sub InsertFolderNameIntoFooter()
'Shauna Kelly, 11 January 2004
'for microsoft.public.word.docmanagement
'
'Inserts the name of the folder holding the active document
'into the footer
Dim sFolderName As String
Dim nLastSlash As Long
Dim nSecondLastSlash As Long
Dim sPathSeparator
sPathSeparator = Application.PathSeparator
sFolderName = ActiveDocument.FullName
nLastSlash = InStrRev(sFolderName, sPathSeparator)
nSecondLastSlash = InStrRev(sFolderName, sPathSeparator,
nLastSlash - 1)
If nLastSlash > 0 And nSecondLastSlash > 0 Then
sFolderName = Mid(sFolderName, _
nSecondLastSlash + 1, _
nLastSlash - nSecondLastSlash - 1)
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary) _
.Range.InsertAfter sFolderName
Else
MsgBox "Please save your file into an appropriate folder."
End If
End Sub