Solved

Word VBA code to insert a document footer in all sections of a document

Posted on 2006-07-13
3
428 Views
Last Modified: 2008-02-01
I'm trying to write some VBA to automate the process of stripping out the first 13  characters of the document name and insert that result as a right-justified footer in all sections of my document.  Everything works EXCEPT I can get the for each loop to work for all the other sections of the document.  Here is my code.   I know it's something to do with the footer properties that I'm missing that casuing it not to work in all sections.  Any help would be greatly appreiciated!

oboefajitaman

Sub stripdocumentname()
 
    Dim namelcase As String
    Dim sec As Section
     
    namelcase = LCase(Mid(ActiveDocument.Name, 13))
                       
        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
       
        For Each sec In ActiveDocument.Sections
            ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = False
            ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
            Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
            Selection.InsertBefore (namelcase)
            Selection.Font.Size = 7
            ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
        Next sec

End Sub
0
Comment
Question by:oboefajitaman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 17103291
Hi oboefajitaman,

Within the loop, you never actually refer to any particular sections.  You'll need to fix that.

Regards,

Patrick
0
 

Author Comment

by:oboefajitaman
ID: 17103320
OK,  I'm just learning VBA and will need a little push here :)

How do I refer to all sections?
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 17104353
It's not clear exactly what your problem is, but I find it easier work on the document directly, instead of through the user interface (changing view, activating windows and using the selection).

This demonstrates:

Sub stripdocumentname()
 
    Dim namelcase As String
    Dim sec As Section
    Dim para As Paragraph
    Dim ftr As HeaderFooter
   
    namelcase = LCase(Mid(ActiveDocument.Name, 13))
                       
    For Each sec In ActiveDocument.Sections
        For Each ftr In sec.Footers 'Primary, First page and even page
            If ftr.Exists Then
                Set para = ftr.Range.Paragraphs(1)
                para.Alignment = wdAlignParagraphRight
                para.Range.InsertBefore namelcase
                para.Range.Font.Size = 7
            End If
        Next ftr
    Next sec

End Sub
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question