Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Word VBA Inserting Section Break for Landscape Page

Posted on 2009-05-15
3
Medium Priority
?
1,280 Views
Last Modified: 2013-11-25
I'm only new to VBA.  I am trying to insert a Landscape Page into my Word document by VBA code.  It inserts the page fine but I've noticed that it is applying the style "footer landscape" style to the page before it (that is before the section break) as well .  Does anyone know what I've done wrong and how to isolate the footer change to just the new section??  I'm hoping for a very quick reply.
Sub InsertLandscape()
 
    Application.ScreenUpdating = False
 
    ActiveDocument.Range(Start:=Selection.Start, End:=Selection.Start). _
        InsertBreak Type:=wdSectionBreakNextPage
    Selection.Start = Selection.Start + 1
    
    
    
    With ActiveDocument.Range(Start:=Selection.Start, End:=ActiveDocument. _
        Content.End).PageSetup
        .TextColumns.SetCount NumColumns:=1
        .LineNumbering.Active = False
        .Orientation = wdOrientLandscape
        .TopMargin = CentimetersToPoints(0.9)
        .BottomMargin = CentimetersToPoints(1.7)
        .LeftMargin = CentimetersToPoints(2)
        .RightMargin = CentimetersToPoints(2)
        .HeaderDistance = CentimetersToPoints(0.3)
        .FooterDistance = CentimetersToPoints(0.6)
        .DifferentFirstPageHeaderFooter = False
    End With
  
    
    ActiveDocument.AttachedTemplate.AutoTextEntries("FooterLandscape").Insert _
    Where:=ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range, RichText:=True
    
    ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Style = "Footer Landscape"
   
  
 
    Application.ScreenUpdating = True
 
End Sub

Open in new window

0
Comment
Question by:Fi69
[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
  • 2
3 Comments
 
LVL 17

Assisted Solution

by:wobbled
wobbled earned 400 total points
ID: 24411517
I think your problem is with this line

 Where:=ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range, RichText:=True


you have hard coded in the section number so it will always place this in the section 1.  You need to get the section number of the section you have just added.
0
 
LVL 19

Accepted Solution

by:
dlc110161 earned 1600 total points
ID: 24413446
I agree with wobbled

Below is the modification I would do so that footer changes will be added to the newly created section.

Dawn Bleuel
Word MVP
Sub InsertLandscape()
Dim sCount As Integer
sCount = ActiveDocument.Sections.Count
 
    Application.ScreenUpdating = False
 
    ActiveDocument.Range(Start:=Selection.Start, End:=Selection.Start). _
        InsertBreak Type:=wdSectionBreakNextPage
    Selection.Start = Selection.Start + 1
    
sCount = ActiveDocument.Sections.Count
    
    With ActiveDocument.Range(Start:=Selection.Start, End:=ActiveDocument. _
        Content.End).PageSetup
        .TextColumns.SetCount NumColumns:=1
        .LineNumbering.Active = False
        .Orientation = wdOrientLandscape
        .TopMargin = CentimetersToPoints(0.9)
        .BottomMargin = CentimetersToPoints(1.7)
        .LeftMargin = CentimetersToPoints(2)
        .RightMargin = CentimetersToPoints(2)
        .HeaderDistance = CentimetersToPoints(0.3)
        .FooterDistance = CentimetersToPoints(0.6)
        .DifferentFirstPageHeaderFooter = False
    End With
    
    ActiveDocument.AttachedTemplate.AutoTextEntries("FooterLandscape").Insert _
    Where:=ActiveDocument.Sections(sCount).Footers(wdHeaderFooterPrimary).Range, RichText:=True
    
    ActiveDocument.Sections(sCount).Footers(wdHeaderFooterPrimary).Range.Style = "Footer Landscape"
 
    Application.ScreenUpdating = True
 
End Sub

Open in new window

0
 
LVL 19

Assisted Solution

by:dlc110161
dlc110161 earned 1600 total points
ID: 24413466
Ooops, delete the first sCount = ActiveDocument.Sections.Count statement. In order to get the proper section count you need the count AFTER the section has been created.

Dawn Bleuel
Word MVP
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
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 where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

647 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