Page 1 of 3 123 LastLast
Results 1 to 15 of 34
  1. #1
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Canton, Ohio, USA
    Posts
    270
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Custom Footer (Word 2002)

    I need to be able to create a custom footer that can extract a pre-determined portion of folder names. For example, our current footer is C:Current CustomersNortheast RegionSa-SmJoe Smith 12345.00orders 0001jsmith.docTAMpjf
    The last item in the footer is the user initials-lower case, the second to last is author and the remainder of the footer is created via the autotext insert of the filename and path.

    I would like to create the following revised footer: Sa-Sm12345.000001jsmith.docTAMpjf . I need to do this to ease privacy concerns about the detail revealed in the footer of the sales letter. As you can see from the revised footer, I no longer want to use the full path. The first item in the new footer is the folder Sa-Sm (i.e., the folder name will vary as its used to capture customers whose last name starts with Sa to Sm--I will always use the full folder name). Additionally, the second and third element of the new footer are numbers extracted from the respected folders (the folder Joe Smith 12345.00 shows in the footer as 12345.00 (i.e., I will always need the last eight charaters of the folder) and the folder orders 0001 becomes 0001 in the footer (i.e., I will always need the last 4 characters of this folder). Then the document name, author and user initials remain unchanged from the original footer. Once we start to use the new footer, the word document naming convention will also be changed to NOT reveal the customer name (i.e. this does not impact the creation of the new footer).

    I have searched through the lounge and can't find anything close to this issue. Can this be done with VBA? If so, I need some sample code to get me started and finally, I need to understand if the code will change the footer anytime the file is saved to a new location or will the user need to re-run the code if the user changes any of the elements contained in the revised footer (i.e., the saved location of the document is changed or the document name or author is changed)? Since the current footer uses standard word fields--they automatically change to reflect the current saved location, etc.---even if the file saved location is modified from its original location during a subsequent edit process. I need to ensure that the code solution to create a new footer is always accurate just like the MS Word fields update automatically with the current footer using the standard Word fields. The automatic update of the fields in the new footer to reflect subsequent changes maybe my most difficult challenge with a VBA solution. THANKS.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Custom Footer (Word 2002)

    There are no string manipulation functions for use in fields, so I think you'll have to use VBA.

    You could use the following macro:

    Sub SetFooter()
    Dim strPath As String
    Dim strFooter As String
    Dim arrParts
    Dim i As Integer
    Dim n As Integer
    strPath = ActiveDocument.FullName
    arrParts = Split(strPath, "")
    n = UBound(arrParts)
    strFooter = arrParts(n - 3) & "" & _
    Right(arrParts(n - 2), 8) & "" & _
    Right(arrParts(n - 1), 4) & "" & _
    arrParts(n) & "" & _
    UCase(ActiveDocument.BuiltInDocumentProperties(wdP ropertyAuthor)) & "" & _
    LCase(Application.UserInitials)
    ActiveDocument.Sections(1).Footers(wdHeaderFooterP rimary).Range.Text = _
    vbTab & strFooter
    Erase arrParts
    End Sub

    and call it from the Document_Open event in the ThisDocument module:

    Private Sub Document_Open()
    SetFooter
    End Sub

    The footer will automatically be updated when the document is opened, but only if the user enables macros. If you want the footer to be updated when the user saves the document under another name or in another location from within Word, you'd have to hijack the FileSaveAs command.

  3. #3
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Canton, Ohio, USA
    Posts
    270
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Custom Footer (Word 2002)

    Hans,
    Thank you for your sample code. Yes, the users do "enable macros" when opening word files as I have several simple print macros in the template file---so that won't be a problem. There are many people that "create" these word files..so I need the "most" mechanized solution that can be achieved via vba.

    After testing your code, what I have found out is that I will need to have the footer update when the document is saved. The reason for this is that the word file is filled with the customer information, then saved, then printed. These typists would now need to enter the customer information, save it, close the file, re-open the same file and then print it to get the newly revised footer. So it looks like I will need to "hijack" the FileSaveAs command...have no clue how to do this. Would you recommend that I create a new button on the toolbar that will save the file AND update the footer or can the code be arranged in a way that it fires whenever the typist click "save" or "save as" from the File menu? Thanks again.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Custom Footer (Word 2002)

    You could use these two macros:

    Sub FileSave()
    If Right(ActiveDocument.Name, 4) = ".doc" Then
    ActiveDocument.Save
    Else
    FileSaveAs
    End If
    End Sub

    Sub FileSaveAs()
    If Dialogs(wdDialogFileSaveAs).Show = True Then
    SetFooter
    End If
    End Sub

    They hijack File | Save and File | Save As.

  5. #5
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Canton, Ohio, USA
    Posts
    270
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Custom Footer (Word 2002)

    Hans,
    I have one further question and also want to confirm my understanding on post # 568045. Is there any way to get the footer to be aligned with the left margin the same as it is when using the standard MS word fields in the footer? The macro generated footer starts about 2 inches from the left margin. I have tried experimenting with the vbTab command but that doesn't seem to be the reason the vba footer does not start at the left margin.

    Also, on post 568045, the code actually "appends" the function of the "save" and "save as" function versus a "hijack" of the intended function, correct? These commands still function as normal but additionally create the footer via vba prior to performing their normal function...am I understanding this correctly? Is it possible via code to actually "hijack" the save or save as button [or any other standard function] so that it doesn't "save" or "save as" anymore but actually performs a different function? I can think of no reason to do this, but I just want to understand whether VBA is limited to "appending a function" or can actually "replace" the intended function in a MS software application. THANKS.

    It is important to me that I just don't come to the lounge for a quick code solution without also attempting to learn the code you or other moderators provide whether it this lounge or the EXCEL lounge...My heart is generally in the EXCEL lounge and I read all the posts daily as a way to learn--I also notice how much of your time you give to helping others learn about code and MS Software....I am truly thankful for your willingness to share your knowledge with others....I am also amazed at your creative ability to solve problems/issues and your patience with those that need the help but don't always know how to ask...take a bow you are the GREATEST!

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Custom Footer (Word 2002)

    1) I'd try omitting the vbTab altogether - it was intended to center the text in the footer. So:

    ActiveDocument.Sections(1).Footers(wdHeaderFooterP rimary).Range.Text = _
    strFooter

    2) If you create a macro with the same name as a built-in command, Word will execute the macro instead of the built-in command. It is completely up to you as programmer to decide what the macro will do. In most cases, you will invoke the standard action together with some extra code, but you can also disable a built-in command this way. If you're in a devilish mood, you could even have File | Open make selected text bold, and make Edit | Copy close the active document. <img src=/S/bananas.gif border=0 alt=bananas width=33 height=35>

    If you're interested, you can download a file listing all built-in commands from Word commands, and their descriptions, default shortcuts and menu assignments. The comments are amusing.

    Thanks for your kind words, by the way!

  7. #7
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Canton, Ohio, USA
    Posts
    270
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Custom Footer (Word 2002)

    Hans,
    Thanks. Eliminating the vbTab did the trick...after I checked what I was trying..in error, I also had eliminated the _ after the equal sign--this is not good, trust me. To ensure this new footer is universally used, can I add this code to each typists' normal.dot file? We also have several other templates for an invoice and shipping label...I believe these template files would also need to have the code inserted into them as when a template is opened it [the new templace] supercedes or over writes the normal.dot template that is opened at the start-up of MS Word...is this correct? THANKS.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Custom Footer (Word 2002)

    If you put the FileSave and FileSaveAs macros in a module in Normal.dot or in a global template (add-in), they will replace the built-in commands, even in a document based on another template. Normal.dot and global templates stay loaded throughout the entire Word session.

  9. #9
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Canton, Ohio, USA
    Posts
    270
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Custom Footer (Word 2002)

    Hans,
    Everything has worked fine with this solution; however, the end users have made an additional request that I have not been able to code. They have requested that I print "Page #" centered on the next line following the revised path macro you created in this thread. Additionally, they do NOT want the page number to appear on the first page.

    So on a 3 page document , they want the path only in the footer on page 1 [left aligned as it currently is], but the path (still left aligned on the first line of the footer) and Page 2 and Page 3 centered on the line following the path in the footer for pages 2 and 3. I noticed that the Page number dialog box has a check box on whether to show the page number on the first page...and I tried the Word macro recorder but I still can't get the desired result, I haven't been able to successfully write an "IF" statement to keep the page number from appearing on page 1. Nor have I been able to discern the code to "center" the Page # on the line following the file path. I can only get my "page #'s" to be left aligned which I am guessing is the default. Also, my left aligned page numbers are "over-writing" the file path you created..I can't seem to get the page number to start on the second line of the footer---I must need a carriage return in the code or something. Thanks.

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Custom Footer (Word 2002)

    Try this:

    Sub SetFooter()
    Dim strPath As String
    Dim strFooter As String
    Dim arrParts
    Dim i As Integer
    Dim n As Integer
    Dim rng As Range
    strPath = ActiveDocument.FullName
    arrParts = Split(strPath, "")
    n = UBound(arrParts)
    strFooter = arrParts(n - 3) & "" & _
    Right(arrParts(n - 2), 8) & "" & _
    Right(arrParts(n - 1), 4) & "" & _
    arrParts(n) & "" & _
    UCase(ActiveDocument.BuiltInDocumentProperties(wdP ropertyAuthor)) & "" & _
    LCase(Application.UserInitials)
    With ActiveDocument.Sections(1)
    ' Specify a different footer for the first page
    .PageSetup.DifferentFirstPageHeaderFooter = True
    ' Set the footer for the first page
    .Footers(wdHeaderFooterFirstPage).Range.Text = _
    strFooter
    ' Set the footer for subsequent pages
    .Footers(wdHeaderFooterPrimary).Range.Text = _
    strFooter & vbCr & vbTab & "Page "
    Set rng = .Footers(wdHeaderFooterPrimary).Range
    rng.Collapse Direction:=wdCollapseEnd
    rng.Fields.Add rng, wdFieldPage
    End With
    Erase arrParts
    Set rng = Nothing
    End Sub

  11. #11
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Canton, Ohio, USA
    Posts
    270
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Custom Footer (Word 2002)

    Hans,
    Thanks for the code..it has worked wonderfully. Like many projects, I guess I am into phase 2. Earlier in this post you stated that Word field strings cannot be manipulated. Can Word fields be created much like EXCEL permits user defined functions? The reason I am asking is I would like to create a field, named say KWGD path, and the field is populated by the code in the macro that establishes the file path in the footer. Is this possible? If so, I would need to be able to add this new user defined field to the normal.dot so that all of my users could insert this field anywhere into the document. I am so much hoping such a field can be created. The answer will determine my next steps. THANK YOU.

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Custom Footer (Word 2002)

    You can set a custom document property or a document variable in code. The difference is that the user can view and edit custom document properties in the Custom tab of File | Properties, while document variables can only be edited in code.
    The user can refer to a document property anywhere in the document by inserting a DocProperty field, and to a document variable by inserting a DocVariable field. Here is code to set a custom document property.

    Sub SetValue()
    Dim strPath As String
    Dim strValue As String
    Dim arrParts
    Dim i As Integer
    Dim n As Integer
    On Error GoTo ErrHandler
    strPath = ActiveDocument.FullName
    arrParts = Split(strPath, "")
    n = UBound(arrParts)
    strValue = arrParts(n - 3) & "" & _
    Right(arrParts(n - 2), 8) & "" & _
    Right(arrParts(n - 1), 4) & "" & _
    arrParts(n) & "" & _
    UCase(ActiveDocument.BuiltInDocumentProperties(wdP ropertyAuthor)) & "" & _
    LCase(Application.UserInitials)
    ActiveDocument.CustomDocumentProperties("KWGD") = strValue

    ExitHandler:
    Erase arrParts
    Exit Sub

    ErrHandler:
    ActiveDocument.CustomDocumentProperties.Add Name:="KGWD", _
    Type:=msoPropertyTypeString, Value:=strValue, LinkToContent:=False
    Resume ExitHandler
    End Sub

  13. #13
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Canton, Ohio, USA
    Posts
    270
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Custom Footer (Word 2002)

    Hans,
    Thank you for this code...I have been experimenting with it and have the following observations. First, if I run the code twice [i.e. for example, the path needs to be updated to reflect the fact that the document was saved in a different place or using a different document name], I get a Run time error -2147467256 (80004005) Automation Error, Unspecified Error. If I delete the KWGD custom document property and then re-run the code--no error message. So I am stumped as to how to capture the "new" file path if a document is subsequently re-named through a file save_as action. Can the code be "smart enough" to discern if the KWGD custom document property already exists and if so, delete it prior to re-creating it to avoid the run time error?

    Second question is that the document must be saved prior to running the code or a "blank" record is returned as the property value. This makes sense as there is no path or document name until the document is saved. This made me wonder if I could associate your new code to post 568099 where you "high-jacked" the file save and save_as functions. Again, the macro would need to discern if a KWGD property field already existed prior to creating one as noted in my first paragraph to avoid the runtime error.

    The last issue I observed is when the KWGD custom document property is placed in the body of a document or in the header / footer it is a "one" time shot as far as accuracy. For example, I created the kwgd custom document property using your code, and then inserted this field into the document body. If I then follow the steps above, delete the KWGD custom document property, save the document with a new name and then re-run the macro...the field value does reflect the "new" file path / filename, etc. However, the kwgd custom document property already inserted in the document still shows the previous path and file name. I need to be able to create and use a document field or document variable that once placed in the document [ either in the body or header / footer ] will change to reflect a new path / filename, etc. as the user saves the document.

    I hope I have not made this post too long or too confusing; but I really try to leverage your time by not leaving out any of the critical details or thought processes. Also, if this is too complicated and I really need a professional consultant, please let me know as I do not want to over step the lounge's mission statement as to "helping" but not "consulting". I hope all is well with you and your family. Thank you.

  14. #14
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Custom Footer (Word 2002)

    Some code is not designed to be run twice. <img src=/S/wink.gif border=0 alt=wink width=15 height=15>

    It is common for Word VBA to generate an error when trying to add a new item to a collection with a duplicate key (in this case, the name of the custom property). Similarly, if you try to check whether the property is there by accessing it, if it's missing you will get an error. If you expect the code to run when the property already exists, you need to account for these errors. One way is as follows, which is an example I have handy and not related to your particular custom property. It looks at every property to find the target and, if it doesn't exist, creates it:

    <code>Public Function ReadWFP() As String
    ' Return value of WFP document property or, if it doesn't exist,
    ' create it with the value 00
    Dim docprop As DocumentProperty, custProps As DocumentProperties
    Set custProps = ActiveDocument.CustomDocumentProperties
    For Each docprop In custProps
    If docprop.Name = "WFP" Then
    ' The property exists! Validate string
    Select Case docprop.Value
    Case "00", "01", "10", "11" 'valid
    ' No action; extract value after Select
    Case Else
    ' This is no good; reset to 00
    docprop.Value = "00"
    End Select
    ReadWFP = docprop.Value
    GoTo cleanUp
    End If
    Next
    ' The property needs to be created
    custProps.Add Name:="WFP", LinkToContent:=False, _
    Type:=msoPropertyTypeString, Value:="00"
    ReadWFP = "00"

    ' Clean up objects
    cleanUp:
    If Not docprop Is Nothing Then
    Set docprop = Nothing
    End If
    Set custProps = Nothing
    End Function</code>

    (That's old code; I think I'm a bit more efficient these days. <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15> )

    Another approach is to use error handling. Try to create the property, trap and inspect any errors, and then proceed from there.

  15. #15
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Canton, Ohio, USA
    Posts
    270
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Custom Footer (Word 2002)

    Thanks for the return post...can you also shed some light on my third paragraph? Is what I have observed, that once a custom document property field is placed in the document it is "frozen" with its value fixed at the time of insertion? If this is the case, I will need to seek other alternatives. I really need a "dynamic" field or variable to resolve my problem, sorry I mean challenge. THANKS.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •