Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleOpen Word files and add footers giving their file names
KeywordsOffice, Word, footer
CategoriesOffice
 
Open a Word server object. Use it to open the files and insert the footers.

Note that this project contains a reference to the Word object library. You may need to use a different reference for your version of Word.

 
Private m_WordServer As Word.Application

' Add the file's footer.
Private Sub ProcessFile(ByVal file_name As String)
Debug.Print file_name

    ' Open the file.
    m_WordServer.Documents.Open FileName:=file_name, _
        ConfirmConversions:=False, _
        ReadOnly:=False, AddToRecentFiles:=False, _
            PasswordDocument:="", _
        PasswordTemplate:="", Revert:=False, _
            WritePasswordDocument:="", _
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto

    ' Insert the footer.
    If m_WordServer.ActiveWindow.View.SplitSpecial <> _
        wdPaneNone Then
        m_WordServer.ActiveWindow.Panes(2).Close
    End If
    If m_WordServer.ActiveWindow.ActivePane.View.Type = _
        wdNormalView Or _
       m_WordServer.ActiveWindow.ActivePane.View.Type = _
           wdOutlineView _
    Then
        m_WordServer.ActiveWindow.ActivePane.View.Type = _
            wdPrintView
    End If
    m_WordServer.ActiveWindow.ActivePane.View.SeekView = _
        wdSeekCurrentPageHeader
    If m_WordServer.Selection.HeaderFooter.IsHeader = True _
        Then
        m_WordServer.ActiveWindow.ActivePane.View.SeekView _
            = wdSeekCurrentPageFooter
    Else
        m_WordServer.ActiveWindow.ActivePane.View.SeekView _
            = wdSeekCurrentPageHeader
    End If
    m_WordServer.Selection.EndKey Unit:=wdStory, _
        Extend:=wdExtend
    m_WordServer.Selection.TypeText Text:="left" & vbTab & _
        file_name & vbTab & "right"
    m_WordServer.ActiveWindow.ActivePane.View.SeekView = _
        wdSeekMainDocument

    ' Save and close the file.
    m_WordServer.ActiveDocument.Save
    m_WordServer.ActiveDocument.Close
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated