unlink Footer/Header in VBA

A

Associates

Hi,

I need your help desperately. I've been trying to get this to work since
yesterday but to no avail. I was trying to insert Header/Footer using VBA in
word 03. This will involve section breaks insertion into the document.

My problem is i think i haven't really put the following code in the right
place.
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious

Different Sections will have different Header and Footer in the document.

Here is my code of inserting a section of "Bibliography" into the document.
This might be placed in the middle of the document.

Sub InsertBibliography()
'Apply Header for previous section
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
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="Header 1"
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=vbTab & vbTab & "Date Issue"
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=16
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

Selection.InsertBreak Type:=wdSectionBreakNextPage
'Selection.InsertBreak Type:=wdPageBreak
Selection.TypeText Text:="Bibliography"
Selection.MoveLeft Unit:=wdCharacter, Count:=12, Extend:=wdExtend
Selection.Font.Color = wdColorGreen
Selection.Font.Bold = wdToggle
Selection.Font.Size = 16
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
Selection.Font.Size = 12
Selection.Font.Color = wdColorAutomatic

'Apply Header for Bibliography
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
'Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious
Selection.TypeText Text:="Company Name"
Selection.MoveLeft Unit:=wdCharacter, Count:=11, Extend:=wdExtend
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="clientname"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=vbTab & vbTab & "Report Line1"
Selection.MoveLeft Unit:=wdCharacter, Count:=12, Extend:=wdExtend
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="reportline1"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
Selection.TypeText Text:=vbTab & vbTab & "Report Line2"
Selection.MoveLeft Unit:=wdCharacter, Count:=12, Extend:=wdExtend
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="reportline2"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

'Apply Footer
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
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.TypeText Text:=vbTab & "Page S"
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage

Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious

With Selection.HeaderFooter.PageNumbers
.NumberStyle = wdPageNumberStyleArabic
.HeadingLevelForChapter = 0
.IncludeChapterNumber = False
.ChapterPageSeparator = wdSeparatorHyphen
.RestartNumberingAtSection = True
.StartingNumber = 1
End With

Selection.MoveUp Unit:=wdLine, Count:=3
Selection.MoveRight Unit:=wdCharacter, Count:=7
Selection.TypeBackspace

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

End Sub

Bibliography Section will have its own Page format in the Footer namely Page
S1, Page S2, and so on. Whereas for the content of the doc, Page 1, 2, etc.
is used.

Greatly appreciate if anyone might be able to give me your advise here.

Thank you in advance
 
S

Shauna Kelly

Hi Associates

Can you back up here a few steps and tell us:

(a) What are you trying to achieve here? Explain it in words you'd use
to your manager's manager.

(b) How does the user decide to insert the Bibliography? For example, do
you want the user to click on a button on a toolbar, or is this code
part of something larger?

(c) Will users insert a bibliography into all different kinds of
documents, or just some specific kinds of documents?

(d) Where in the document is the bibliography inserted? For example, is
it always at the very end of the document?

Shauna

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
A

Associates

Hi Shauna,

Sorry for not providing you with sufficient info.

(a) What are you trying to achieve here?
The aim here is to set up a standard document template that would enable our
staff to create a report for our company. At the moment, many reports are
different in terms of format. There are lots of inconsistencies throughout
all the reports made by different staff. So we thought, by having a proper
template made ready for staff to use, we could resolve this issue.

In the template (.dot), we plan on making lots of macros placed in the menu
that would enable user to do the job they desire. For example, we have macros
called "insert a section", "insert picture", "insert table", and so on. I
know these would override the features provided by word 03. But these macros
would shorten the steps needed for the staff to say "insert a section or sub
section" for example.

At the moment, i was creating a macro that would insert a bibliography into
the document. By clicking the button "insert bibliography", it would
automatically create a new section break and put the word "bibliography" as
the heading with appropriate header and footer. Of course, this will depend
on the location of the insertion point of the cursor as the bibliography
section will be created in the next page.

So far, I'm struggling to make macro to insert the bibliography section as
well as its footer and header to the document (the code for the macro was
posted earlier). Each of the sections (for example bibliography, glossary,
etc) have their own different format of header and footer. So this involves
lots of "unlink and link to the previous" as far as the header/footer is
concerned.
(c) Will users insert a bibliography into all different kinds of
documents, or just some specific kinds of documents?
My idea was everytime someone wants to make up a document report for their
client. He/she can run this template and work on the report.

All the macros are available listed in the customised menu specifically for
our own use.

Hopefully i explained it in words that can make things a lot clearer for you.

I'm happy and open to any other bright ideas you or anyone might like to
share. But this is the actual problem we're facing and trying to resolve it.
And if we can pull this off - it would hopefully reduce time spent on
checking for inconsistencies or errors found.

Thank you in advance
 
S

Shauna Kelly

Hi "Associates"

Thanks for the explanation. That helps a lot. Here is what I would
suggest:

1. Create a new .dot file. Do that by doing File > New and choosing to
create a template. Use the user interface to create the styles and page
format (eg paper size, margins) that you need in the template.

2. In the template, create an AutoText for each of the things that you
want people to add to the document. From what you say, you'll need an
AutoText for:
- a table
- a bibliography

You probably don't want to include a section break after the table
AutoText, but you can include a section break after the Bibliography
AutoText. The section break will include the headers and footers you
need.

If you want to make an AutoText of a table, select the table using Table
Select > Table. Just make that the AutoText. Don't include any
extraneous paragraphs before or after it (unless, of course, you
suddenly decide you

More info at: Using AutoText
http://www.word.mvps.org/FAQs/Customization/AutoText.htm


3. Use Tools > Customize and make sure that the "Save in" box refers to
your template.

On the Toolbars tab, create a new toolbar. Keep the Customize dialog
open.

On the Commands tab, in the Categories list, choose Insert. In the
Commands list find the button that says "From file" and has a picture
icon. Drag that to your menu. Right-click this new button on your menu
and change the caption to whatever suits. This one button will now
invoke the Insert > Picture > From File command. You won't need to write
any code to manage getting the image file and path name, coping if the
user presses Esc half way through the process, detecting the kind of
image etc etc.

Back in the Customize dialog, on the Commands tab, in the Categories
list, click New Menu. In the Commands list, you'll see New Menu with an
arrow. Drag that to your new toolbar. Right-click it and give it a new
caption (eg "Insert...").

Now, back in the Customize dialog, on the Commands tab, in the
Categories list, click AutoText. In the Commands list you'll now see the
AutoTexts you created. Drag each of them on to your new menu.




So, you now have a menu with a button that will allow people to insert a
picture, and a menu that will allow people to insert the content you
stored in AutoTexts.

You probably don't need any of the code you currently have. You'll need
to fiddle around with section breaks and the headers/footers but
depending on your needs, you might not need any code at all.


Hope this helps. Write back if you need more details.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
A

Associates

Hi Shauna,

Thank you for your support. I'm really glad you offered another way of
getting around my problem.

I can create a very simple autotext like inserting "Bibliography" into a
page. However, i was unable to create an autotext where it would have a
Header, a footer and Bibliography all in one. So far, i could only create
several autotexts, one for "Bibliography", another for "Header for
Bibliography" and another for "Footer for Bibliography". I wish i could learn
to put these all into one autotext. So when user clicks on the button "Insert
Bibliography" on the toolbar menu, it'd put in the header, bibliography and
footer to the document.

I read the article you advised me to read about autotext. It said items need
to be selected in the entry when creating autotext entry. I could only select
the word "Bibliography", not the Header and the footer as they are greyed
out. Hope you're still with me. Hence when creating the autotext entry, i can
only see the word "Bibliography" without the header and the footer.

Also, i noticed that i'm still having problem with the numbering in the
Footer. Even if the autotext can insert the Header, Bibliography and Footer
(all in one page) into the document, i still have to manually unlink it to
the previous and make it start from 1 as bibliography has a different footer
formatting from the rest of the pages in the document.

sorry if i have made it so complicated for you to understand. Please let me
know if have trouble understanding my problem.

Thank you in advance

For example:
I have a few pages at the start, their footer have the following
Page 1
Page 2
....
Page 7
---- section break ----
Page S1
Page S2

After deleting the section break, it stuffs up all the preceding numbering
system in the footer as follows
Page S1
Page S2
....
Page S8
Page S9

I think i need some codes so that it would preserve whatever the preceding
pages were as follow
Page 1
Page 2
....
Page 9

I guess, T
 
S

Shauna Kelly

Hi

Headers and footers are properties of a section. The section break at
the *end* of a section stores the properties of that section. So make
sure you can see the section breaks (see
http://www.word.mvps.org/FAQs/Formatting/NonPrintChars.htm).

(As an aside, all documents have at least one section. If a document has
only one section, you don't see a section break at the end of the
document. The final paragraph mark stores the relevant section
properties.)

Now, insert your text. Insert a section break after it. Click in the
text and format the section as you choose. Select both the text *and*
the section break and create an AutoText from that. When you insert that
AutoText it will bring the section break (and its properties) with it.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
A

Associates

Hi Shauna,

Thank you for getting back to me.

Yes, it works. I can insert the bibliography section with the section break
without any problems. I also learnt the fact that the header/footer is part
of the section break. That's great.

But, the only problem i have now is if i insert the bibliography, say
somewhere in the middle of the document, it seems to have stuffed up the
pagenation system.

For example Footer in previous pages looks like as follow (note this is
before the insertion of bibliography)
company name Page 1
company name Page 2
....
company name Page 10

== then, after inserting the bibliography, the footer of the bibliography
looks as follows
bibliography Page BI11
bibliography Page BI12

What i really wish to happen is to be able to unlink to previous so that
it'd look like
bibliography Page BI1
bibliography Page BI2
and so on

Can we still do it with autotext? or does it have to be done via VBA code?
if i do need to code, could you recommend me a good book that i can learn all
about the VBA coding for making macros for word document? because so far i
have been using the "record macro" to get the VBA code but this doesn't work
on a certain number of things like for example

everytime there is a change to the section whether adding new, or modify
text of the section heading or add a new page, i need to make sure that the
table of contents gets updated. I tried to find the code by using "record
macro" but to no avail. Could you please help me on this one too?

Thank you in advance
 
S

Shauna Kelly

Hi

Issue 1: Re-starting page numbers.

It looks like you've already unlinked those footers (if you had not,
they would be the same as in the previous section). When you create your
Bibliography autotext, use the Header and Footer toolbar to set the page
number to start at 1 (and not continue from the previous section). Then,
select the text and the section break as before, and create your
AutoText.

In any case, it's probably time for another read of the following:
How to control the page numbering in a Word document
http://www.word.mvps.org/FAQs/Numbering/PageNumbering.htm


Issue 2: Updating the table of contents.
everytime there is a change to the section whether adding new, or
modify
text of the section heading or add a new page, i need to make sure
that the
table of contents gets updated.

That's not possible--it's not just now Word's fields work. Word will
update the fields if you do print preview, or if you choose to update
the fields (eg, ctrl-a to select the whole document, then F9 to update
the fields).


Issue 3: Sources for information on programming in Word

Start with everything at http://word.mvps.org/index.html. Then, read
posts in these newsgroups frequently. There are some book
recommendations here: http://word.mvps.org/Tutorials/Books.htm


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
L

Lisa JW

Hi Shauna

I've got a similar problem to the post you've been replying to. I've been working on a macro which I have as a button on a custom toolbar which when pressed should:

1. Insert a new section at the point of the cursor
2. unlinks the header and footer in the new section
from the previous section
3. Changes the page setup of the new section to landscape and alters the margins.

I found your suggestion of creating an Autotext entry to unlink header/footer as an alternative great. I created my new section, made the changes I wanted to that then selected the section break between this section and the last section to create my autotext and then added it as a button on the toolbar.

When I insert it, it creates the new section but applies the changes to the header/footer and changes the page setup to landscape to the previous section, not the new one.

What am I doing wrong?
 
J

Jonathan West

Hi Shauna

I've got a similar problem to the post you've been replying to. I've been
working on a macro which I have as a button on a custom toolbar which when
pressed should:

1. Insert a new section at the point of the cursor
2. unlinks the header and footer in the new section
from the previous section
3. Changes the page setup of the new section to landscape and alters the
margins.

I found your suggestion of creating an Autotext entry to unlink
header/footer as an alternative great. I created my new section, made the
changes I wanted to that then selected the section break between this
section and the last section to create my autotext and then added it as a
button on the toolbar.

When I insert it, it creates the new section but applies the changes to
the header/footer and changes the page setup to landscape to the previous
section, not the new one.

What am I doing wrong?

You're doing nothing wrong, it is just that section formatting is defined by
the section break at the end of the section, not the section break at the
start. So, if you insert a "formatted" section break, it sets the page
layout and headers & footers of the section in front of the break.

The "section break" defining the section formatting of the last section in
the document is the final undeletable paragraph mark.
 
L

Lisa JW

Hi Shauna

I forgot to add I tried creating the autotext by selecting the last paragraph mark in my new section and that didn't appear to do anything.

Any help would be greatly appreciated

Thanks
Lisa
 
L

Lisa JW

Hi Shauna

I forgot to add I tried creating the autotext by selecting the last paragraph mark in my new section and that didn't appear to do anything not even create a new section.

Any help would be greatly appreciated

Thanks
Lisa
 

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