wdHeaderFooterPrimary wdHeaderFooterPrimary

S

Senad Isanovic

Need to know how to refer to first page in the document (not just to first
section) no matter if is different first page or not. Below I have a range
that refers to footer in the first section but I need the just the first
page. Thanks

Set myrange =
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
myrange.Collapse WdCollapseDirection.wdCollapseEnd

Set myfooter = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
AddTxtbox sRef, myrange, myfooter, top
 
J

Jezebel

Dim pRange As Word.Range

on error resume next
If ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter Then
Set pRange = ActiveDocument.StoryRanges(wdFirstPageFooterStory)
Else
Set pRange = ActiveDocument.StoryRanges(wdPrimaryFooterStory)
End If
on error goto 0

You need the error-handler in case there's no footer at all.
 
S

Senad Isanovic

Great! And if I want to do in the similay way for a HeaderFooter variable,
how can that be done? The reason that I'm asking is that I need to pass both
myrange and myfooter to AddTxtbox procedure. That procedure adds a textbox
in the footer on the first page on the document. This works fine if the
first section only has one page but if the first section of the document
contains several pages then the textbox is added on all pages if the first
section which is not what I want. Thanks!

Dim myfooter As HeaderFooter
If ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter Then
Set myrange = ActiveDocument.StoryRanges(wdFirstPageFooterStory)
Set myfooter =
ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage)
Else
Set myrange = ActiveDocument.StoryRanges(wdPrimaryFooterStory)
Set myfooter =
ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage)
myfooter.LinkToPrevious = False
End If

myrange.Collapse WdCollapseDirection.wdCollapseEnd
AddTxtbox sRef, myrange, myfooter, top
 
J

Jezebel

This has to do with how your document is set up, rather than your code. If
the first section of your document has more than one page, and is not set up
with a different first page footer, then adding something to the footer will
add it to every page of that section. That's the way headers and footers
work. If you need the object on the first page only, you need to set
DifferentFirstPageHeaderFooter to TRUE.
 
S

Senad Isanovic

Ok, I understand. My issue is that I need to add a textbox with unique
document number in the footer of the first page of the document, no matter
if the document has different first page or not, and no matter if you have
several pages in the first section of the doc. Changing the page setup to
different first page (if is not different first page already) is not a
proper solution because if an existing footer is there on the first page it
may disappear which is not what I want.. So any other solution? Mayby using
the code below and trying to add a textbox, is not the right way to do
this.. Thank you, /Senad
 
J

Jean-Guy Marcil

Senad Isanovic was telling us:
Senad Isanovic nous racontait que :
Ok, I understand. My issue is that I need to add a textbox with unique
document number in the footer of the first page of the document, no
matter if the document has different first page or not, and no matter
if you have several pages in the first section of the doc. Changing
the page setup to different first page (if is not different first
page already) is not a proper solution because if an existing footer
is there on the first page it may disappear which is not what I
want.. So any other solution? Mayby using the code below and trying
to add a textbox, is not the right way to do this.. Thank you, /Senad


Does the text have to be in the footer?
Why can't you just add it tot he bottom of the first page directly?

If not, you can detect the type of footer and code for the addition of a
different first page footer like this (In pseudo code):

Detect if first section has a different first page footer
If ActiveDocument.Sections(1).PageSetup.DifferentFirstPageHeaderFooter
Then
MsgBox "First section has a different first page footer."
Else
MsgBox "First section does not have a different first page footer."
End If
If so, add your textbox > End
If not,
Add the diffrent first page header/footer to the first section
Copy the content of the primary footer to the first page footer
With ActiveDocument.Sections(1)
With .Footers
.Item(wdHeaderFooterFirstPage).Range.FormattedText = _
.Item(wdHeaderFooterPrimary).Range.FormattedText
'Remove superfluous ¶
.Item(wdHeaderFooterFirstPage).Range.Paragraphs.Last.Range.Delete
End With
End With
Repeat for the header
Add your textbox to the footer > End

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

Senad Isanovic

Many Thanks for a very good answer.





No it does not have to in the footer, I can add it at the bottom of the
first page or in the left margin of the first page. The only thing that is
important is that it is somewhere on the first page and that the user can
not easily delete this unique document number. That was the original reason
to why I 'm adding a textbox in the footer. I tried also to add a little
table with only one cell on the first page and then to read/write the unique
number to that cell in my table but the document can contain one or several
other tables too and I don't know how to refer to my little table.

Can I name the table to something unique instead of using the table index
number?

ActiveDocument.Tables(1).Rows(1).Cells(1).Range.Text = "INT98878-v1"
 
J

Jean-Guy Marcil

Senad Isanovic was telling us:
Senad Isanovic nous racontait que :
Many Thanks for a very good answer.






No it does not have to in the footer, I can add it at the bottom of
the first page or in the left margin of the first page. The only
thing that is important is that it is somewhere on the first page and
that the user can not easily delete this unique document number. That
was the original reason to why I 'm adding a textbox in the footer. I

Then it is probably best to use the footer to avoid accidental deletion.
tried also to add a little table with only one cell on the first page
and then to read/write the unique number to that cell in my table but
the document can contain one or several other tables too and I don't
know how to refer to my little table.
Can I name the table to something unique instead of using the table
index number?

ActiveDocument.Tables(1).Rows(1).Cells(1).Range.Text = "INT98878-v1"

No, you cannot name tables, but, you can assign a bookmark to a table, as in
this sample code that assumes that you have already made sure that section
1 in the document has a different first page footer:

'_______________________________________
Dim tblID As Table
Dim rgeFooter As Range
Const strTableName As String = "Table_ID"

'Set the range in the footer that already has a diffrent first page footer
Set rgeFooter = ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage)
_
.Range.Paragraphs(1).Range
rgeFooter.Collapse wdCollapseStart

'Add a table in that footer and assign bookmark
Set tblID = rgeFooter.Tables.Add(rgeFooter, 1, 1)
'set width
tblID.Columns(1).Width = InchesToPoints(1.25)
ActiveDocument.Bookmarks.Add strTableName, tblID.Range

'To use the table in the bookmark
With ActiveDocument.Bookmarks(strTableName).Range.Tables(1)
.Cell(1, 1).Range.Text = "INT98878-v1"
End With
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top