custom word footer

  • 10 years ago

     

    Hi folks, It's been awhile since I've been on.

    I'm trying to create a custom footer in MS Word for students logging on to the network.

    The plan is to edit the normal.dot template so everytime a new document is created, it automatically places their name in the footer.

    I've created the following function to get their full name which works well:

    Function getEnvironVar(EnvVar) As String  'Written by Barry Fensom 23/11/2006

    Dim EnvString
    Index = 1
    getEnvironVar = ""
    Do
        EnvString = Environ(Index)
       
        If Left(EnvString, Len(EnvVar)) = EnvVar Then
        getEnvironVar = Mid(EnvString, Len(EnvVar) + 2)
        End If
        Index = Index + 1
    Loop Until EnvString = ""









    End Function

    The code below returns an error if it is placed in the document_new event.

    Is there some way to use fields or something that gets it's data from my getenvironvar function? Thanks.

     

    Private Sub Document_New()

    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
        Selection.TypeText Text:=getEnvironVar("FULLNAME")
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

    End Sub

  • 10 years ago

    Hi,

    The below code will work for installing a footer if inserted in the normal template noting that the function getEnvironVar didn't work on my PC so I had to replace it with a regular variable and it worked just fine.

    Private Sub Document_New()

        ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
        Selection.TypeText Text:=getEnvironVar("FULLNAME")
        ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

    End Sub

     

    Hope this works for you.

  • 10 years ago

    Hi, Thanks for that, it works. Only problem I came across, was that the user needs to click File, and New Document for it to work, so I decided to script the opening of Ms Word using VBS as follows, then created a shortcut to the vbs file, and changed the shortcut icon to the MS Word icon. Now we can tell which student belongs to which printout.

     (hmm... from vba to vbs, maybe this should be moved to the vbs forum. lol)

     

    Set WshShell = CreateObject("WScript.Shell")

    set oEnv=WshShell.Environment("User")
    strFullName = oEnv.item("FULLNAME")


     

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.Documents.Add()

    objDoc.Sections(1).Footers(1).Range.Text = strFullName
    objDoc.Sections(1).Footers(1).Range.ParagraphFormat.Alignment = 1

     

     

    with some modifications for powerpoint:

     

    Set WshShell = CreateObject("WScript.Shell")

    set oEnv=WshShell.Environment("User")
    strFullName = oEnv.item("FULLNAME")



    Set ppApp = CreateObject("Powerpoint.Application")

    ppApp.Visible = True
    Set ppt = ppApp.Presentations.Add

     

    ppt.SlideMaster.HeadersFooters.Footer.Text = strFullName

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!' but 'That's funny...'” - Isaac Asimov