How to unlink headers/footers correctly without using ActiveWindow

E

ESH

Hey

As a start I ran a macro with application visible.
From the macro recorder I learned to use the following statement
to unlink header/footer:

Selection.InsertBreak Type:=wdSectionBreakNextPage

' Cut link between herder/footer on page x and page x+1
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.LinkToPrevious = False
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

BUT it didn't work when I changed to run the application invisible :-(

In an earlier Q in this NG I was told that the ActiveWindow object only
worked correctly when application was visible.
I have tried to rewrite the script to the following:

Selection.InsertBreak Type:=wdSectionBreakNextPage

' Cut link between herder/footer on page x and page x+1
With ActiveDocument.Sections(ActiveDocument.Sections.Count)
.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
End With

As far as I can see it does not work.
When I manually enters the header on the new page (doubleclicking on the
new header), it is STILL linked to previous page.

What am I doing wrong?

Ebbe
 
J

Jonathan West

I would recommend that if you want the macro to run correctly when
invisible, you avoid as far as possible the use of the following objects

- ActiveDocument
- Selection
- any Window object, including ActiveWindow

For the most part, you can avoid using the ActiveDocument, as follows. When
opening a document, using the following syntax

Dim oDoc As Document
Set oDoc = Documents.Open(FileName:=C:\test.doc")

After that, whenever you are in the habit of using ActiveDocument, you can
use oDoc instead.

Once you have a reference to oDoc, you can do the following to define a
range

Dim oRange as Range
Set oRange = oDoc.Range(0, 0)

Then you can do almost anything with oRange that you can do with Selection.
In addition, you can define as many Range objects as you want or need, and
use them all simultaneously. You can only have one Selection.

With regard to breaking the link between sections for the header and footer,
you don't need to use the Window objects or the Selection at all for this.
There are several ways to approach this but probably the simplest is as
follows. Assume that oRange is pointing to somewhere within the section
whose header and footer you want to unlink, proceed as follows

With oRange.Sections(1)
.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
End With

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
E

ESH

Hey

I have decided NOT to run the application invisible.
It involves too many changes :-(

Instead I run the macros minimized.
Doing this, I have one little problem:

When I open a new document with:
Documents.Open FileName:=BaseDocumentFullName, ReadOnly:=True
ActiveWindow.WindowState = wdWindowStateMinimize
The document pops up for a secund before it is minimized.

Are there any way I can do a "open minimized" to avoid this short pop-up?

Ebbe
 
J

Jonathan West

Hey

I have decided NOT to run the application invisible.
It involves too many changes :-(

Instead I run the macros minimized.
Doing this, I have one little problem:

When I open a new document with:
Documents.Open FileName:=BaseDocumentFullName, ReadOnly:=True
ActiveWindow.WindowState = wdWindowStateMinimize
The document pops up for a secund before it is minimized.

Are there any way I can do a "open minimized" to avoid this short pop-up?

Ebbe

Hi Ebbe

Try this

Application.Screenupdating = False
Documents.Open FileName:=BaseDocumentFullName, ReadOnly:=True
ActiveWindow.WindowState = wdWindowStateMinimize

Application.Screenupdating = True


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
E

ESH

Thank you Jonathan.

It didn't prevent the window to pop up, but it shortened the time it was
visible essentially.

Ebbe
 

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