Consulting

Results 1 to 6 of 6

Thread: Select text and move to a header - how to do that in VBA

  1. #1
    VBAX Regular
    Joined
    Oct 2018
    Location
    Antwerp
    Posts
    41
    Location

    Select text and move to a header - how to do that in VBA

    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
    Last edited by Aussiebear; 12-19-2022 at 12:25 PM. Reason: Added code tags to supplied code

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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

    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by gmayor View Post
    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.
    Last edited by macropod; 01-22-2019 at 02:19 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Regular
    Joined
    Oct 2018
    Location
    Antwerp
    Posts
    41
    Location

    Copy txt to hedaer in a word document

    Quote Originally Posted by macropod View Post
    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

  6. #6
    VBAX Regular
    Joined
    Oct 2018
    Location
    Antwerp
    Posts
    41
    Location
    Thank you very much, Graham.
    Both the VBA codes with range or paragraphs works perfect.
    Thank you very much for your kind help.
    Ward

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •