PDA

View Full Version : Solved: Insert FileName with Path as Text, Not Field



davidlgibbs
03-11-2013, 09:53 AM
I am working with Word 2010 on several Windows 7 computers. I am relatively new to VBA, but have been coding simple macros using Word going back to its first version.

I have a macro for creating a new letter. The macro opens a dialog box, collects information and inserts it in the correction location on the letterhead template. Next, the macro prompts the user to save the file, after which I want it to type into the first-page footer (as text, NOT the FileName field with the path switch) the path and filename.

With a prior version of this macro, we simply sent the cursor/insertion point to the end of the letter, and had it insert the FileName field with the upper-case switch and path switch. We now cannot do that, and I want the file name and path to be inserted in the first page footer, and it must be text rather than the field, because I have to redact part of the path.

With my current version, I've had to just put the FileName field in the first-page footer of the Word Template file, and it updates when the file is printed. The problem is that I cannot redact what I need from the path (looks like this \\servername\UserShares\Company Shared Documents\Firm Documents\clientname-fileno\file name.docx. I need to get rid of everything up to \clientname-fileno in the footer. For several reasons, I can't/don't want to use a mapped drive and replace \\servername\UserShares\Company Shared Documents\Firm Documents with a drive letter, though if I must I suppose I will have to.

My feeling is that if I can get the FileName filed in as text, I can then programmatically redact it down to what I want.

Secondary problem - I can't get the insertion point moved to a bookmark in the first page footer - says it can't find it. How else can I move the insertion point into the first page footer as opposed to the second (and subsequent page) footer?

Please help - this has been driving me crazy, and I need to get this finalized so we can switch over to new letterhead.

macropod
03-11-2013, 02:29 PM
Without posting the code, you're not giving yourself much chance of specific help.

For starters, you don't need to involve a FILENAME field anywhere in the process. You can extract the part of the name you want to output with code like:
Dim StrName As String, StrTmp As String
StrTmp = ActiveDocument.FullName
StrName = Split(StrTmp, "\")(UBound(Split(StrTmp, "\")) - 1) & " \" & Split(StrTmp, "\")(UBound(Split(StrTmp, "\")))
MsgBox StrName

fumei
03-11-2013, 05:22 PM
Agreed. The filename is a string and therefore can be manipulated by standard string functions Although....I am not sure what you mean by

"and it updates when the file is printed"

Updates in what way.

"I can't get the insertion point moved to a bookmark in the first page footer" You do not need to move the insertion point into any footer. Any text in the footer can be manipulated directly, again by standard string functions.

Doug Robbins
03-11-2013, 08:09 PM
You cannot insert a bookmark into the header or footer of a document.

To get what you want into the footer of the document as ordinary text, replace the

MsgBox strName

in the code that Macropod supplied with:

ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range.InsertBef ore strName

macropod
03-11-2013, 08:51 PM
You cannot insert a bookmark into the header or footer of a document.
Um, yes you can:
Sub Demo()
With ActiveDocument
.Bookmarks.Add Name:="MyBookmark", Range:=.Sections.First.Headers(wdHeaderFooterPrimary).Range
End With
End Sub

Doug Robbins
03-11-2013, 10:42 PM
Hi Paul

I'm sure it used not to be so, but my memory could be failing me.

Doug

fumei
03-11-2013, 10:50 PM
I can't remember pre-2002, but certainly as of 2002 you can put bookmarks in headerfooter. You can perform action on a bookmark via code, but you can NOT programmatically use a GoTo on a bookmark in a headerfooter.

So... you can change it (all properties and methods of bookmarks apply), but not put the cursor there (a method of the Selection, not bookmarks).

fumei
03-11-2013, 11:11 PM
Although of course you CAN get the cursor there by using bookmark.select. There can be odd effects however, depending on what View is being used.

davidlgibbs
03-12-2013, 02:44 PM
First, thank you everyone for the help with this. I intentionally did not include the code on the post because it is very long (and I'm sure you who code for a living could find 100 things wrong with it). Additionally, all other parts of the code are working fine; I just have no code right now for the part of the macro I am trying to create. I know that my macro code is not the most efficient in the world because we started this Macro pre-VBA, so it is a real dog. I'll go through and clean up the code later once it is working correctly.

What I was after is exactly what macropod posted. I knew that I no longer wanted to use a field to display this information, but I did not know how to call the file name and path programatically. I needed to know how to call it and save it as a string, from there I know how to use it.

I also wasn't sure about the bookmark in the first-page footer. I think all three of you are correct when it comes to this question - you can still programatically "GoTo" a bookmark in the header or footer. You don't appear to be able to go to a bookmark in the FirstPageFooter or Header. In any event, no need for that when I can use Doug's suggested code and programatically get it there. I'll post back after I get a chance to implement it and mark it solved.

davidlgibbs
03-12-2013, 02:59 PM
You guys are awesome. I made just two tweaks to fit my template. I changed Doug's code from "Before" to "after" to place it exactly where I wanted it in the first page footer. I also added three periods (...) before the string to indicate that there is a portion of the path not shown! I am really stoked - thank you three so much for your help.

fumei
03-13-2013, 09:22 PM
Glad we could help. I do have comments.

"because it is very long (and I'm sure you who code for a living could find 100 things wrong with it). Additionally, all other parts of the code are working fine"

This is a very good reason to start breaking up your code into viable working chunks. Working with called chunks makes debugging MUCH MUCH easier. The fact you could not bring out the one non-working chunk demonstrates the principle.

oh, and few of us code for a living.