Autotext Parent Path

  • Thread starter Thread starter Paul Craig
  • Start date Start date
P

Paul Craig

Hi
I have a group of templates that im using in Word 2002 and I am trying
to get it to display in the footer the PARENT directory that the file
is located. I can currently get the WHOLE path using { FILENAME \p }
which gives me for example:
C:\Templates\Workbook\User\Paul Craig\theTemplate.doc

Im tryin to find a way to get just the parent directory so therefore
all that will be displayed will be:
Paul Craig

Would be great if there was a generic solution to this problem so that
I could use my templates on other versions of word also, but I dont
know what the chances are of this.

Any help will be greatly appreciated,
Cheers
Paul Craig
 
Hi Paul

I can't think of a way to do this in Word directly, although you could
do it with a macro. But I wonder if there might be another way to get
the same text.

Is it possible that "Paul Craig" is also the user (ie the information at
Tools > Options > User Information)? If so, you can get that information
with the following field: { USERNAME \* MERGEFORMAT }

Alternatively, is it possible that "Paul Craig" is the author of the
document (as shown at File > Properties > Summary tab)? If so, you can
get that information with the following field: { DOCPROPERTY Author \*
MERGEFORMAT }

Note that you can't type the {} braces by hand. You must use ctrl-F9 and
type between the braces that Word inserts for you. Once you've typed the
field, use F9 to update it, or ctrl-a F9 to update all fields in the
document.

If neither of these will suit your needs, post back and let us know
whether theTemplate file is actually a .doc or a .dot file. Then we can
work out how to create a solution using a macro.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
Melbourne, Australia
 
Hey Shauna,
Thank you very much for your reply, unfortunatly neither
of these solutions will work for what I am after. The
file that I am opening IS a doc file not a dot file,
sorry about the confusion there with the name of the
file. The way that the documents work is that I have
everyone who will use the document each have their own
copy under their own directory on my computer eg.
C:\Workbook\Paul Craig\week1.doc
C:\Workbook\John Smith\week1.doc
and they just go on the computer open up the file in
there directory fill it out and print it off. There could
potentially be alot of documents that any given person
might have to fill out so to manually type it in will be
very tedious.

So if there is a way with macros to just display in the
document the parent folder that the document resides then
that would be fantastic.
Thanks once again
Paul Craig
 
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
 
Shauna,
Thank you so much for your response it was fantastic help and worked a
treat. We have recently been informed that other similar business may
also be interested in this software along with the documents that have
been created. I thought that everything would be fine but seems that I
need to somehow install the template on to the users computer so that
when they open these documents, the macros dont cause the security
warning to pop up. Is there a better way to tackle this now that I
will be distributing this in such a way that I would like the
installation to be easy for users. We are going to use a program
called setup2go which is great for distributing the program but doesnt
seem to be able to incorporate word templates in it (and we definatly
cant afford some of the widely known setup programs). So is there a
way of finding out the path to add the template to on a computer and
automatically install the template in there with some nice neat way so
that when the documents based on the template are run, the security
macro warning doesnt come up?

If anyone at all can help me with this or at least steer me in the
right direction it would be fantastic.
Any help is greatly appreciated
Cheers
Paul Craig
 
Back
Top