Problem with pasting shape object into header

B

booger

I am automating Word 2000 from VB6 SP6. (I have extensive Excel VBA
experience, but I rarely work with the Word object model - it is just
different enough from the Excel object model to be very frustrating at
times.)

I have a shape object in the main body of my document that I am
pasting into a header like so:

Set grpTemp = .ActiveDocument.Shapes.Range(aShapes).Group
grpTemp.Select
.Selection.Cut
With .ActiveDocument.Sections(SectionNum).Headers(wdHeaderFooterPrimary)
.Range.Paste
End With

When SectionNum=1, then everything works fine. Next, I add a section
break(Section 2), and add some text. Here is how I add the section
break:

With AppWord.Selection
.InsertBreak wdSectionBreakNextPage
.EndOf wdStory
With AppWord.ActiveDocument.Sections(.Information _
(wdActiveEndSectionNumber)).Headers(wdHeaderFooterPrimary)
.LinkToPrevious = False
.Range.Delete
End With
End With

Note that immediately after adding the section break, I break the link
to the previous section's header and clear the header range. At this
point, I add another section break (Section 3) using the section break
code and I perform the pasting code again with SectionNum=3.

Here is where the problem is: even though I am pasting my shape object
into the header of section 3, the shape appears in the header for
section 1. Now, to make sure that I am doing everything OK, I inserted
a line of code (.Range.Text = "hello") just before the .Range.Paste
line to write some text to the header. The "hello" text appears in the
Section 3 header (as it should) but when I continue to the
..Range.Paste line, the shape object which is pasted from the clipboard
stubbornly appears in Section 1.

Any ideas? Thanks in advance for your assistance.

sincerely,
drew richards
 
J

Julie

Drew,

Your problem likely stems from the design of the header/footer shapes
collection design. Hopefully, someone who understands the shapes collection
better than I will jump in here. As I understand it, there is a single
shapes collection for all headers and footers. Therefore, programmatically
inserting shapes into headers and footers using the header range can produce
unexpected results.

Try using the seekview method. Here is a recorded macro that puts the shape
into the correct header.

Before running the macro, I had a text box in the clipboard, insertion point
in section 3, section 3 header unlinked from previous, no shapes in any
header. AFter the macro, the shape appeared only in the section 3 header.

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.Paste
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument


When developing an automated watermark/draft stamp application a few years
back, I found a different way to work around the problem. I cannot remember
exactly how I resolved it, but do remember it being very, very complicated
and I was close to giving up before finding the ticket. If seekview
doesn't work for you, post back and I will dig up that huge application to
try to figure out exactly how I worked around the problem.

Hopefully, someone else here has a better solution.

Hope this helps,
Julie
 
B

booger

Julie,

Thanks for your response. I have already tried the SeekView method.
Unfortunately, it does not work for me because I am using Automation
from VB6 to create my document (or story...). I want the process to be
transparent to the user and it seems that once I invoke the SeekView
method, the Word Application becomes visible which also slows down the
execution of the code.

I agree with your assessment that someone who is familiar with the
HeaderFooter and Shape object models could probably clear this up for
me. I have not been able to find any documentation of how these models
interact with each other. I have tried various combinations of
DifferentFirstPageHeaderFooter equal to True and False along with
various combinations of pasting my shape using the
wdHeaderFooterPrimary and wdHeaderFooterFirstPage constants. So far, I
have not been able to find the magic combinations.

Thanks again for your response.

drew
 
J

Julie

Booger,

If you search Google groups Microsoft.public.word* for "shapes collection
header footer" you will find some good reading about the header/footer
shapes collection.

Let me know. I did a cursory review of my code prior to replying to you and
did not see anywhere where I was moving the insertion point. I do know this
though, I was using the header range in conjunction with an autotext entry
(the shape was stored in an autotext entry - the macro inserts the autotext
entry into collapsed range representing the end of the header range, then
removes the unwanted paragraph mark that resulted from insertion of the
autotext entry).

Juile
 

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