Log in

View Full Version : How to solve: Different first page header, but logo from second page places.



babsiee
03-12-2013, 09:27 AM
Hi all!

I have made a macro. It helps me to add images in the headers and text in the footers. With a different first page. It also changes the marges of the page.

It works fine when I use it on an empty document. But when I use it on a document with text already in it, it goes wrong.

Then it adds the logo, that needs to be in the header of the second page, underneath the logo of the first page!

Any ideas how to solve this?

Thanks!
Babette

Sub MacroB()

'

' MacroB Macro

'


' Custom form page parameters
Const FPageWid = 7: Const FPageHgt = 2#
Const FMargLft = 1: Const FMargRgt = 0.78
Const FMargTop = 1.77: Const FMargBot = 0.78

' Word page dimensions
Const WPageWid = 8.5: Const WPageHgt = 11

' Printer no-print area
Const PNoPrTop = 0.25: Const PNoPrBot = 0.25
Const PNoPrLft = 0.25: Const PNoPrRgt = 0.25

Dim WMargTop, WMargBot, WMargLft, WMargRgt ' Word margins
WMargTop = FMargTop
WMargBot = FMargBot
WMargLft = FMargLft
WMargRgt = FMargRgt

Dim oDoc As Document
Dim oVars As Variables
Set oDoc = ActiveDocument
Set oVars = oDoc.Variables

With oVars
.Add("WMargTop").Value = WMargTop
.Add("WMargBot").Value = WMargBot
.Add("WMargLft").Value = WMargLft
.Add("WMargRgt").Value = WMargRgt
End With

With oDoc.PageSetup
.TopMargin = InchesToPoints(oVars("WMargTop").Value)
.BottomMargin = InchesToPoints(oVars("WMargBot").Value)
.LeftMargin = InchesToPoints(oVars("WMargLft").Value)
.RightMargin = InchesToPoints(oVars("WMargRgt").Value)
End With

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With Dialogs(wdDialogInsertPicture)
.Name = "http://www.broesj.com/Kop4.jpg"
.FloatOverText = False
.Execute
End With

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.TypeText Text:="here is text"
Selection.MoveLeft Unit:=wdCharacter, Count:=192, Extend:=wdExtend
Selection.Font.Name = "Verdana"
Selection.Font.Size = 6
ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True

Dim asection As Section

Set asection = Selection.Sections(1)

With asection

With .PageSetup

End With





'tekst header pagina 1

ActiveWindow.ActivePane.View.SeekView = wdHeaderFooterFirstPage 'HEADER PAGINA 1
With Dialogs(wdDialogInsertPicture)
.Name = "http://www.broesj.com/Kop3.jpg"
.FloatOverText = False
.Execute
End With





'footer lettertype enzo
.Footers(wdHeaderFooterFirstPage).Range.Text = "here is text"

ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range.Select


Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Name = "Verdana"
Selection.Font.Size = 6




ActiveWindow.ActivePane.View.Type = wdPageView

End With
End Sub

Doug Robbins
03-12-2013, 03:03 PM
You should use the Range object rather than selection with code such as:

Dim rng As Range
With ActiveDocument.Sections(1)
.PageSetup.DifferentFirstPageHeaderFooter = True
Set rng = .Headers(wdHeaderFooterPrimary).Range
rng.InlineShapes.AddPicture "http://www.broesj.com/Kop4.jpg"
Set rng = .Footers(wdHeaderFooterPrimary).Range
rng.Text = "here is text"
rng.Font.Name = "Verdana"
rng.Font.Size = 6
Set rng = .Headers(wdHeaderFooterFistPage).Range
rng.InlineShapes.AddPicture "http://www.broesj.com/Kop4.jpg"
Set rng = .Footers(wdHeaderFooterFirstPage).Range
rng.Text = "here is text"
rng.Font.Name = "Verdana"
rng.Font.Size = 6
End With


I could not test the above as http://www.broesj.com/Kop4.jpg does not appear to return a picture.

fumei
03-13-2013, 09:25 PM
And use Styles!

fumei
03-13-2013, 09:26 PM
And could you explain a bit more what you ae wanting to do. What is happening with the Variables.