Discussion:
Word Header changes in size though nothing was changed in it
(too old to reply)
elderfubeca
2008-07-31 14:16:02 UTC
Permalink
I've got a VB.NET application that does find/replace in documents. If I do a
find/replace in the header using the following code, the header size seems to
augment visually--though the actual header size doesn't change. It almost
appears to not close properly or something. And then I can't get the header
to size back to its original size no matter what I do. This same code seems
to work in a Macro, so it seems to be an Interop issue.

WordDoc.Application.ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageHeader
WordDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Find.Execute(FindText:="BOB", Forward:=True, ReplaceWith:="Bob", Replace:=wdReplaceAll)
WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

Any ideas as to what I'm doing wrong?

Thanks
Doug Robbins - Word MVP
2008-07-31 19:52:30 UTC
Permalink
Try using:

Dim myrange As Range
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
Set myrange = .Range
myrange.Start = myrange.Start + InStr(myrange, "BOB") - 1
myrange.End = myrange.Start + 3
myrange.Text = "Bob"
End With

Though for your situation (VB.Net), it would probably be

Dim myrange As Range
With WordDoc.Sections(1).Headers(wdHeaderFooterPrimary)
Set myrange = .Range
myrange.Start = myrange.Start + InStr(myrange, "BOB") - 1
myrange.End = myrange.Start + 3
myrange.Text = "Bob"
End With
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
Post by elderfubeca
I've got a VB.NET application that does find/replace in documents. If I do a
find/replace in the header using the following code, the header size seems to
augment visually--though the actual header size doesn't change. It almost
appears to not close properly or something. And then I can't get the header
to size back to its original size no matter what I do. This same code seems
to work in a Macro, so it seems to be an Interop issue.
WordDoc.Application.ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageHeader
WordDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Find.Execute(FindText:="BOB",
Forward:=True, ReplaceWith:="Bob", Replace:=wdReplaceAll)
WordDoc.Application.ActiveWindow.ActivePane.View.SeekView =
wdSeekMainDocument
Any ideas as to what I'm doing wrong?
Thanks
elderfubeca
2008-07-31 21:16:01 UTC
Permalink
That still doesn't work. Maybe if you setup a document the same margins and
such that I've got you'll see the same problem.

Margins:
Top: 0.3''
Left: 1.35''
Bottom: 1''
Right: 1.25''
Gutter: 0''

Headers and Footers:
Header: 0.5''
Footer: 0.1''

Now there is nothing in the header. But I noticed that the header seems to
expand when you run even the following line of code in a Macro:

ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = ""

Then the header doesn't seem to want to collapse down after that when I run
my VB.NET code, even running the code you provided. It seems to act strange
anytime that I touch the Range object of the header.

Thanks,

EF
Post by Doug Robbins - Word MVP
Dim myrange As Range
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
Set myrange = .Range
myrange.Start = myrange.Start + InStr(myrange, "BOB") - 1
myrange.End = myrange.Start + 3
myrange.Text = "Bob"
End With
Though for your situation (VB.Net), it would probably be
Dim myrange As Range
With WordDoc.Sections(1).Headers(wdHeaderFooterPrimary)
Set myrange = .Range
myrange.Start = myrange.Start + InStr(myrange, "BOB") - 1
myrange.End = myrange.Start + 3
myrange.Text = "Bob"
End With
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
Post by elderfubeca
I've got a VB.NET application that does find/replace in documents. If I do a
find/replace in the header using the following code, the header size seems to
augment visually--though the actual header size doesn't change. It almost
appears to not close properly or something. And then I can't get the header
to size back to its original size no matter what I do. This same code seems
to work in a Macro, so it seems to be an Interop issue.
WordDoc.Application.ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageHeader
WordDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Find.Execute(FindText:="BOB",
Forward:=True, ReplaceWith:="Bob", Replace:=wdReplaceAll)
WordDoc.Application.ActiveWindow.ActivePane.View.SeekView =
wdSeekMainDocument
Any ideas as to what I'm doing wrong?
Thanks
Doug Robbins - Word MVP
2008-07-31 22:47:37 UTC
Permalink
Running that code will insert an empty paragraph in the header which may
have been completely empty before hand. As a result, the header area will
expand as required to accommodate that paragraph.
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
Post by elderfubeca
That still doesn't work. Maybe if you setup a document the same margins and
such that I've got you'll see the same problem.
Top: 0.3''
Left: 1.35''
Bottom: 1''
Right: 1.25''
Gutter: 0''
Header: 0.5''
Footer: 0.1''
Now there is nothing in the header. But I noticed that the header seems to
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = ""
Then the header doesn't seem to want to collapse down after that when I run
my VB.NET code, even running the code you provided. It seems to act strange
anytime that I touch the Range object of the header.
Thanks,
EF
Post by Doug Robbins - Word MVP
Dim myrange As Range
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
Set myrange = .Range
myrange.Start = myrange.Start + InStr(myrange, "BOB") - 1
myrange.End = myrange.Start + 3
myrange.Text = "Bob"
End With
Though for your situation (VB.Net), it would probably be
Dim myrange As Range
With WordDoc.Sections(1).Headers(wdHeaderFooterPrimary)
Set myrange = .Range
myrange.Start = myrange.Start + InStr(myrange, "BOB") - 1
myrange.End = myrange.Start + 3
myrange.Text = "Bob"
End With
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
Post by elderfubeca
I've got a VB.NET application that does find/replace in documents. If
I
do a
find/replace in the header using the following code, the header size
seems
to
augment visually--though the actual header size doesn't change. It almost
appears to not close properly or something. And then I can't get the header
to size back to its original size no matter what I do. This same code seems
to work in a Macro, so it seems to be an Interop issue.
WordDoc.Application.ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageHeader
WordDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Find.Execute(FindText:="BOB",
Forward:=True, ReplaceWith:="Bob", Replace:=wdReplaceAll)
WordDoc.Application.ActiveWindow.ActivePane.View.SeekView =
wdSeekMainDocument
Any ideas as to what I'm doing wrong?
Thanks
elderfubeca
2008-08-01 12:18:00 UTC
Permalink
I guess that's what I'm trying to avoid.

The same thing happens when I run this line of your code:

Set myrange = .Range

I don't even have to change the Range object, just touch it, and the header
paragraph expands. Is there a way that I can validate wether the header has
a paragraph without touching the Range property?

Thanks!
Post by Doug Robbins - Word MVP
Running that code will insert an empty paragraph in the header which may
have been completely empty before hand. As a result, the header area will
expand as required to accommodate that paragraph.
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
Post by elderfubeca
That still doesn't work. Maybe if you setup a document the same margins and
such that I've got you'll see the same problem.
Top: 0.3''
Left: 1.35''
Bottom: 1''
Right: 1.25''
Gutter: 0''
Header: 0.5''
Footer: 0.1''
Now there is nothing in the header. But I noticed that the header seems to
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = ""
Then the header doesn't seem to want to collapse down after that when I run
my VB.NET code, even running the code you provided. It seems to act strange
anytime that I touch the Range object of the header.
Thanks,
EF
Post by Doug Robbins - Word MVP
Dim myrange As Range
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
Set myrange = .Range
myrange.Start = myrange.Start + InStr(myrange, "BOB") - 1
myrange.End = myrange.Start + 3
myrange.Text = "Bob"
End With
Though for your situation (VB.Net), it would probably be
Dim myrange As Range
With WordDoc.Sections(1).Headers(wdHeaderFooterPrimary)
Set myrange = .Range
myrange.Start = myrange.Start + InStr(myrange, "BOB") - 1
myrange.End = myrange.Start + 3
myrange.Text = "Bob"
End With
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
Post by elderfubeca
I've got a VB.NET application that does find/replace in documents. If
I
do a
find/replace in the header using the following code, the header size
seems
to
augment visually--though the actual header size doesn't change. It almost
appears to not close properly or something. And then I can't get the header
to size back to its original size no matter what I do. This same code seems
to work in a Macro, so it seems to be an Interop issue.
WordDoc.Application.ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageHeader
WordDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Find.Execute(FindText:="BOB",
Forward:=True, ReplaceWith:="Bob", Replace:=wdReplaceAll)
WordDoc.Application.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Any ideas as to what I'm doing wrong?
Thanks
Loading...