New Section Footer via Automation

A

AG

I am using automation from Access 2000 to create a new Word doc (2000-2003).
The doc consists of several sections with tables and footers. I can generate
and fill all data, text, etc with no problem on my machine and generate a
completed doc as desired.
However, on client's machine, the separate section footers do not behave as
desired.
Instead of creating a new footer, the previous footer is being modified with
the content intended for the new footer.

Here is a sniplet of code that I am using to add the new footer.
Yes, all objects are properly defined/instantiated. No errors are generated.

Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdSectionBreakNextPage
mappWord.Visible = True
Selection.EndKey Unit:=wdStory '9/20/2007
If docWord.ActiveWindow.View.SplitSpecial <> wdPaneNone Then
docWord.ActiveWindow.Panes(2).Close
End If
If docWord.ActiveWindow.ActivePane.View.Type = wdNormalView _
Or docWord.ActiveWindow.ActivePane.View.Type = wdOutlineView Then
docWord.ActiveWindow.ActivePane.View.Type = wdPrintView
End If

docWord.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
docWord.ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageFooter
End If
Selection.HeaderFooter.LinkToPrevious = False
On Error Resume Next
'I think the problem is related to this line. Client was generating error
'command not avaliable..' here
while I was not. Yet, I had similar footer problem without this line.
docWord.ActiveWindow.ActivePane.View.NextHeaderFooter
On Error GoTo ErrLine
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.HeaderFooter.PageNumbers.Add PageNumberAlignment:= _
wdAlignPageNumberRight, FirstPage:=True
Selection.MoveLeft Unit:=wdCharacter, Count:=1
'do some formatting, type text etc. for footer

I am guessing that it is related to some option setting on client's machine.

Any help would be appreciated.
 
T

Tony Strazzeri

I am using automation from Access 2000 to create a new Word doc (2000-2003).
The doc consists of several sections with tables and footers. I can generate
and fill all data, text, etc with no problem on my machine and generate a
completed doc as desired.
However, on client's machine, the separate section footers do not behave as
desired.
Instead of creating a new footer, the previous footer is being modified with
the content intended for the new footer.

Here is a sniplet of code that I am using to add the new footer.
Yes, all objects are properly defined/instantiated. No errors are generated.

Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdSectionBreakNextPage
mappWord.Visible = True
Selection.EndKey Unit:=wdStory '9/20/2007
If docWord.ActiveWindow.View.SplitSpecial <> wdPaneNone Then
docWord.ActiveWindow.Panes(2).Close
End If
If docWord.ActiveWindow.ActivePane.View.Type = wdNormalView _
Or docWord.ActiveWindow.ActivePane.View.Type = wdOutlineView Then
docWord.ActiveWindow.ActivePane.View.Type = wdPrintView
End If

docWord.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
docWord.ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageFooter
End If
Selection.HeaderFooter.LinkToPrevious = False
On Error Resume Next
'I think the problem is related to this line. Client was generating error
'command not avaliable..' here
while I was not. Yet, I had similar footer problem without this line.
docWord.ActiveWindow.ActivePane.View.NextHeaderFooter
On Error GoTo ErrLine
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.HeaderFooter.PageNumbers.Add PageNumberAlignment:= _
wdAlignPageNumberRight, FirstPage:=True
Selection.MoveLeft Unit:=wdCharacter, Count:=1
'do some formatting, type text etc. for footer

I am guessing that it is related to some option setting on client's machine.

Any help would be appreciated.
The command not available at thayt point suggests that the selection
has come out of the footer/header and dropped back to the Main
document story.

Also have a look at the starting view that you use v your clients. ie
are you both running the macro from Normal view or Print Layout view.

Hope this helps

Cheers
TonyS.
 
J

Jay Freedman

I hate to be the bearer of bad news, but that code is horrible, the
sort of stuff you get out of the macro recorder because it doesn't
know any better. (See
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm for a
critique of that garbage grinder.)

Instead of writing a macro that pushes the Selection around and works
as if an automaton was pressing keys, use the objects that VBA makes
available. Something like this:

Sub NewSectionFooter()
Dim docWord As Word.Document
Dim wSection As Word.Section
Dim wRange As Word.Range

Set docWord = ActiveDocument ' or = Word.Documents.Add
Set wRange = docWord.Range
wRange.Collapse wdCollapseEnd
wRange.InsertBreak Type:=wdSectionBreakNextPage

' since the break is inserted before wRange,
' wRange is contained in the new section, so
' the new section is wRange.Sections(1)
Set wSection = wRange.Sections(1)
With wSection.Footers(wdHeaderFooterPrimary)
.LinkToPrevious = False
.Range.Text = "some text here" ' replaces whatever was there
.PageNumbers.Add PageNumberAlignment:= _
wdAlignPageNumberRight, FirstPage:=True
End With
End Sub

Please ask questions if there's any part of that macro that's unclear
to you.
 
A

AG

Thanks Tony,

I will rewrite the section where the view is checked. As Jay pointed out it
comes from recorder code and I did not look at it close enough. It only
checks for normal or outline view, but maybe this user has a different
'default' view.
 
A

AG

Thanks Jay,

Your points are well taken. Yes, I admit the code came from the recorder
with just a bit of tweaking. Not my usual style, but I do very little Word
VBA and find the documentation woefully lacking. My usual is VB, Access VBA
and .NET.
Good article also, which I do agree with. Although, I do tend to leave in a
lot of the garbage, just so I can later go back and tweak a few other
properties without having to spend a lot of time figuring out how to access
them.
I will try your method tomorrow.
 
A

AG

Jay,

Just wanted to say thanks. Your approach solved the problem, along with
another that popped up.
 
J

Jay Freedman

You're welcome! That's why I'm here.

Jay
Jay,

Just wanted to say thanks. Your approach solved the problem, along
with another that popped up.


AG
Email: discussATadhdataDOTcom
 

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