PDA

View Full Version : Insert/Format Landscape Section



AndrewPerl
04-10-2006, 02:19 PM
I am trying to write a macro that will insert and format a landscape section into a document. The insertion will take place at the current cursor location. I have a message box asking the user if the cursor is at the proer insertion point... and that's where things stop working.

Here is the logic I am trying to use (I am sure I am getting hung up on the syntax of some commands):

1. Get the current section number
2. Insert a next page break
3. Insert a paragraph
4. Insert another next page break
5. Go to the section number obtained in step 1, increment by 1.
6. Format the section with page margin and orientation as needed.

I get some bizarre results with my little code snippets. Any help would be greatly appreciated.

Andrew Perl
Kansas City

fumei
04-10-2006, 03:02 PM
With Selection
.InsertBreak Type:=wdSectionBreakNextPage
.TypeParagraph
.InsertBreak Type:=wdSectionBreakNextPage
.GoTo What:=wdGoToSection, Which:=wdGoToPrevious, _
Count:=1, Name:=""
With .PageSetup
.Orientation = wdOrientLandscape
.TopMargin = InchesToPoints(1.2)
.BottomMargin = InchesToPoints(1.2)
.LeftMargin = InchesToPoints(0.7)
.RightMargin = InchesToPoints(0.8)
End With
End With
1. do not need the Section number, just go back one section
2. I assume "next page break" should be "next SECTION break".

AndrewPerl
04-12-2006, 12:35 PM
I added the code you provided and added a user prompt for the user to ensure that the cursor is at the proper insertion point. This works fine. I then went ahead and added code to format the headers and footers in the new section. This is where I get confused. I am able to successfully set the LinkToPrevious property to false. I then try to manipulate the text in the header (and footer eventually).. with no luck.

The new section inherits the header and footers text from the previous section. This is OK. I just need to adjust some tabs stops and justification in the current header and footer. Here is the code I have so far...



Sub InsertLandscapeSection()
'
' InsertLandscapeSection Macro
' Macro created 4/7/2006
' Declare Page setup options as variables

'Display Msgbox to ask author if the cursor is at the insertion point
CursorMsg = "Is the cursor placed at the insertion point of the new landscape section?"
Response = MsgBox(CursorMsg, vbYesNo, "Confirmation")
CurrentSection = ActiveDocument.Range(0, Selection.Sections(1).Range.End).Sections.Count
NewSection = CurrentSection + 1
If Response = vbYes Then 'Author chose Yes
'Insert new section
With Selection
.TypeParagraph
.InsertBreak Type:=wdSectionBreakNextPage
.TypeParagraph
.TypeParagraph
.InsertBreak Type:=wdSectionBreakNextPage
'Go to the section we just created
.GoTo What:=wdGoToSection, Which:=wdGoToPrevious, Count:=1, Name:=""
'Format the section we just created
With .PageSetup
.Orientation = wdOrientLandscape
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(0.8)
.RightMargin = InchesToPoints(0.8)
End With
'Reformat the headers; break the same as previous link before doing any formatting
With ActiveDocument.Sections(NewSection).Headers(wdHeaderFooterPrimary)
.LinkToPrevious = False
'.Alignment = wdAlignParagraphRight
End With
'Reformat the footers; break the same as previous link before doing any formatting
With ActiveDocument.Sections(NewSection).Footers(wdHeaderFooterPrimary)
.LinkToPrevious = False
End With

End With
Else 'Author chose No
'Exit sub
End If

End Sub


I am still having difficulty grasping the concept of Selection and Range. I guess I will have to somehow select the text in the header and then apply my changes. Would I nest a With Selection statement in the Header/Footer section of the code?

Thanks,

Andrew Perl
Kansas City

AndrewPerl
04-19-2006, 01:42 PM
Attached is the final working code sample that I developed. The magic moment arrived when I realized that you had to switch the view type to accomplish anything in a header/footer....



Sub InsertLandscapeSection()
' InsertLandscapeSection Macro
' Macro created 4/7/2006 by Andrew Perl
' Only for use with SES-related templates (do not use for CMTCGUI-based documents)
'Display Msgbox to ask author if the cursor is at the insertion point
CursorMsg = "Is the cursor placed at the insertion point of the new landscape section?"
Response = MsgBox(CursorMsg, vbYesNo, "Confirmation")
CurrentSection = ActiveDocument.Range(0, Selection.Sections(1).Range.End).Sections.Count
NewSection = CurrentSection + 1
'Author selected Yes in message box
If Response = vbYes Then
'Insert new section
With Selection
.TypeParagraph
.TypeParagraph
.InsertBreak Type:=wdSectionBreakNextPage
.TypeParagraph
.TypeParagraph
.InsertBreak Type:=wdSectionBreakNextPage

'Go to the section we just created
.GoTo What:=wdGoToSection, Which:=wdGoToPrevious, Count:=1, Name:=""

'Format the section we just created
With .PageSetup
.Orientation = wdOrientLandscape
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(0.8)
.RightMargin = InchesToPoints(0.8)
End With

'Break the same as previous link in the current section
With ActiveDocument.Sections(NewSection).Headers(wdHeaderFooterPrimary)
.LinkToPrevious = False
End With

With ActiveDocument.Sections(NewSection).Footers(wdHeaderFooterPrimary)
.LinkToPrevious = False
End With

'Break the same as previous link in the section after the new landscape section;
'Otherwise the headers and footers following the new section
'Will be improperly formatted
With ActiveDocument.Sections(NewSection + 1).Headers(wdHeaderFooterPrimary)
.LinkToPrevious = False
End With

With ActiveDocument.Sections(NewSection + 1).Footers(wdHeaderFooterPrimary)
.LinkToPrevious = False
End With

'Reformat Landscape Headers
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
With Selection.Paragraphs.TabStops
.ClearAll
.Add Position:=InchesToPoints(9.4), Alignment:=wdAlignTabRight
End With

'Reformat the Landscape Footers
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
With Selection.Paragraphs.TabStops
.ClearAll
.Add Position:=InchesToPoints(0), Alignment:=wdAlignTabLeft
.Add Position:=InchesToPoints(4.7), Alignment:=wdAlignTabCenter
.Add Position:=InchesToPoints(9.4), Alignment:=wdAlignTabRight
End With

'Reset view pane to standard view
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

End With

'Author selected NO in the message box
Else
'Exit sub (do nothing)
End If
End Sub


Somewhat painful to develop! I learned quite a bit and this macro will save my team a lot of time a hassle over the next year.

Andrew Perl

fumei
04-19-2006, 02:12 PM
The magic moment arrived when I realized that you had to switch the view type to accomplish anything in a header/footer....This is NOT correct. You do not have to change View type to action in the header/footer.

The reason you have to with your code, is that you are putting your code within a With statement. The With refers to the Selection...and that is why you have to use View.

If you used a Range (or a HeaderFooter object) , then you can action headers/footer directly.