PDA

View Full Version : [SOLVED:] Select text and move to a header - how to do that in VBA



wdg1
01-21-2019, 04:27 AM
I want to select 3 lines of text (the 3 last lines in a document), then move this lines to the header of the document.

My macro does not work.
Here is the code.



Selection.EndKey Unit:=wdStory
Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.Cut
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.MoveLeft Unit:=wdCharacter, Count:=1
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

macropod
01-21-2019, 02:44 PM
There is nothing in your recorded code to paste the content from the clipboard into the header once you've accessed it. Insert:

Selection.Paste
before:

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

gmayor
01-21-2019, 09:56 PM
It probably makes more sense to use ranges e.g. as follows, then you don't have to open and close the header.
Your selection from the end of the document only selects 2 'lines'.


Sub Macro1()
Dim oRng As Range
Dim oHeader As HeaderFooter
Selection.EndKey Unit:=wdStory
Selection.MoveUp Unit:=wdLine, Count:=3, Extend:=wdExtend
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Set oRng = Selection.Range
Set oHeader = oRng.Sections(1).Headers(wdHeaderFooterPrimary)
oHeader.Range.FormattedText = oRng.FormattedText
oRng.Text = ""
Set oRng = Nothing
Set oHeader = Nothing
End Sub

Note 'lines' are a vague concept in Word as they are volatile and created by text flow. They don't actually exist. If however you mean paragraphs rather than lines then

Sub Macro2()
Dim oRng As Range
Dim oHeader As HeaderFooter
Set oRng = ActiveDocument.Range
oRng.Collapse 0
oRng.MoveStart wdParagraph, -3
Set oHeader = oRng.Sections(1).Headers(wdHeaderFooterPrimary)
oHeader.Range.FormattedText = oRng.FormattedText
oRng.Text = ""
Set oRng = Nothing
Set oHeader = Nothing
End Sub

macropod
01-21-2019, 10:09 PM
It probably makes more sense to use ranges e.g. as follows, then you don't have to open and close the header.
Hi Graham,
I thought about that, too, but decided not to go down that path as we don't really know which of the three possible header objects exists on the page concerned, and determining that just so a range object can be used is a lot more work.

wdg1
01-22-2019, 01:15 AM
There is nothing in your recorded code to paste the content from the clipboard into the header once you've accessed it. Insert:

Selection.Paste
before:

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument



Yes, there was no txt selected to the clipboard.
This works perfect now. Thank you very much for your kind help.
Ward

wdg1
01-22-2019, 01:22 AM
Thank you very much, Graham.
Both the VBA codes with range or paragraphs works perfect.
Thank you very much for your kind help.
Ward