Results 1 to 15 of 15

Thread: [RESOLVED] Programmatically Updating Footer In Word Document

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Resolved [RESOLVED] Programmatically Updating Footer In Word Document

    I have a Word document that will be utilized as a Template.

    I have the following place holders as a Footer:

    Originator:
    Use Cases For:

    What I would like to do is prompt the user for his/name and, once entered, update the footer information so that after entering the name, the footer would read:

    Originator: Hack

    I would like to do the same thing for the second line as well.

    I guess my first question would be is this even possible?

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Programmatically Updating Footer In Word Document

    Hack:

    I don't claim to be a Pro in Word, but here is something I recorded and then munged with. It seems to work ... except that it doesn't close the Header/Footer view. You can figure that out. Just do whatever you need to do to set up the 2 tStr variables that get inserted into the Footer.
    Code:
    Sub Macro3()
        Dim tStr1 As String
        Dim tStr2 As String
        '
        tStr1 = "Hack"
        tStr2 = "This is a TEST"
        
        If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
            ActiveWindow.Panes(2).Close
        End If
        If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
            ActivePane.View.Type = wdOutlineView Then
            ActiveWindow.ActivePane.View.Type = wdPrintView
        End If
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        If Selection.HeaderFooter.IsHeader = True Then
            ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
        Else
            ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        End If
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = "Originator:"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.TypeText Text:=" " & tStr1
        With Selection.Find
            .Text = "Use Cases For:"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.TypeText Text:=" " & tStr2
    End Sub
    I hope this helps!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Programmatically Updating Footer In Word Document

    Hack
    Basically you will need a variable in the document that can contain each of these values. The easiest way I have found to do this in the past is to use the "Custom Document Properties" feature in Word.
    Goto File-->>Properties and select the custom tab. Here you can add any properties you need, e.g. Originator. The in your _Open event, you can check to see if that property has a value other than a single space (it has to have some value, space is a good default) and prompt for one if its missing.

    VB Code:
    1. Private Sub Document_Open()
    2. Dim MyProp As DocumentProperty
    3.  
    4.     For Each MyProp In ThisDocument.CustomDocumentProperties
    5.         With MyProp
    6.             If .Name = "Originator" Then
    7.                 If .Value = " " Then
    8.                     'get your value here,
    9.                     'updating the value property
    10.                     ' and update header/footer
    11.                 End If
    12.             End If
    13.         End With
    14.     Next MyProp
    15. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Updating Footer In Word Document

    Two questions:

    Once I get a value for Originator, how do I tell it to update the Footer with that value?

    Does Word VBA support the InputBox?

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Updating Footer In Word Document

    Quote Originally Posted by Hack
    Two questions:

    Once I get a value for Originator, how do I tell it to update the Footer with that value?

    Does Word VBA support the InputBox?
    Strike the InputBox question. I have that working and I have a name stored in a string.

    I just don't know what to do with it in order to make it show up in the footer as:

    Originator: Name_I_Typed

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Programmatically Updating Footer In Word Document

    Webtest has posted the code necessary for accessing the header or footer.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Programmatically Updating Footer In Word Document

    Incidentally, you can use document variables to store anything you need - just like a variable but stored in the document.

    VB Code:
    1. ActiveDocument.Variables.Add Name:="Value1", Value:="1"
    2. MsgBox ActiveDocument.Variables("Value1")

    I find them much easier to use than custom properties. Also, you can put them straight into a document using the DOCVARIABLE field. So I'd just insert a DOCVARIABLE field into your footer and then prompt the user for their name (or copy the string) into that document variable, then just update the fields. You could easily do the same for the second case too.


    zaza
    Last edited by zaza; Mar 22nd, 2006 at 01:35 PM.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  8. #8

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Updating Footer In Word Document

    Quote Originally Posted by RobDog888
    Webtest has posted the code necessary for accessing the header or footer.
    So he has, and it works.

    Ok...final question...how do I automatically close the Header/Footer view?

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Programmatically Updating Footer In Word Document

    Something like so.
    VB Code:
    1. ActiveWindow.ActivePane.View.Type = wdPrintView
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Programmatically Updating Footer In Word Document

    WUZZAMATTA Hack ... you don't like the code I dished up especially for you?
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  11. #11
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Programmatically Updating Footer In Word Document

    Record a macro while you close the Header/Footer view. I did nearly the whole code in my post in Record Macro! (That's why it looks so kludgy!)

    Edit: RobDog made it too easy for you!

    Thanks, RobDog!
    Last edited by Webtest; Mar 22nd, 2006 at 02:00 PM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Programmatically Updating Footer In Word Document

    And I plagerized it in my previous post
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Updating Footer In Word Document

    Quote Originally Posted by Webtest
    WUZZAMATTA Hack ... you don't like the code I dished up especially for you?
    I love your code, am using it.

  14. #14

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Programmatically Updating Footer In Word Document

    Quote Originally Posted by Webtest
    Edit: RobDog made it too easy for you!
    I'm sure I could have figured it out, but after all this time I felt that it was my turn to play noob.

    It actually felt kinda good.

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Programmatically Updating Footer In Word Document

    Glad I'm not the only one that gets to feel like a noob. Currently being a noob at ASP.NET

    Ps Hack - 22,222 posts
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width