I'm creating a button in an excel sheet that generates a report in Word. So far, so good except I'm trying to have a header in the document that looks like this:
but it ends up just looking like this:
where it just comes up as this weird extra window at the bottom of my document and it says page 3, when there is only two pages of the document (or there only should be two pages) my code is:
'Header
With Wordapp
.activeDocument.Bookmarks("Header").Select
.Selection.TypeText Text:="CFD Analysis of Antenna EPA "
.Selection.MoveRight Count:=1
.Selection.TypeText Text:=today
.Selection.MoveRight Count:=1
.Selection.TypeText Text:=antennamanu & " " & modnum
End With
The behavior shown in the screen shot of the question is due to use the Select
method. This separate Header pane, in the Draft view, comes from the days of Word 2.0, where WYSIWYG wasn't available for headers, footers, comments and similar things.
Instead of using Select
, working directly with Range
objects will avoid the problem. The code in the question can be altered along these lines:
'Header
Dim rngHeader as Word.Range 'or Object if this is late-binding
With Wordapp
Set rngHeader = .ActiveDocument.Bookmarks("Header").Range
rngHeader.Text ="CFD Analysis of Antenna EPA " & vbTab & _
today & vbCr & antennamanu & " " & modnum
End With
In the original code, the document apparently has a TAB and a Paragraph character. So Selection.MoveRight
is moving to the tab stop, and then to the next paragraph. The above code writes those as it goes, so these characters should be removed from the report template being used.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.