none
Go to beginning of first column on the page RRS feed

  • Question

  • Hello all Experts,

    I have a multiple column document, how can I get the selection to the beginning of the first column on the current page using vba?

    • Edited by moishy Friday, April 27, 2012 6:21 AM Clarification
    Thursday, April 26, 2012 11:07 PM

Answers

  • I have a multiple column document, how can I get the selection to the beginning of the first column on the current page using vba?

    See if this code sample helps at all...

    Sub GoToFirstCol_NewspaperCols()
        Dim currPageNr As Long
        Dim rngPage As word.Range
        Dim rngSecCols As word.Range
        Dim sec As word.Section
        
        currPageNr = Selection.Information(wdActiveEndPageNumber)
        Set rngPage = ActiveDocument.Bookmarks("\Page").Range
        For Each sec In rngPage.Sections
            If sec.PageSetup.TextColumns.Count > 1 Then
                Set rngSecCols = sec.Range
                If Asc(rngSecCols.Characters(1)) = 12 Then
                    rngSecCols.MoveStart wdCharacter, 1
                End If
                rngSecCols.Collapse wdCollapseStart
                'Debug.Print rngSecCols.Words(1)
                If rngSecCols.Information(wdActiveEndPageNumber) = currPageNr Then
                    rngSecCols.Select
                Else
                    Selection.GoTo What:=wdGoToPage, Count:=currPageNr
                End If
                Exit For
            End If
        Next
    End Sub


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by moishy Sunday, April 29, 2012 11:57 AM
    Sunday, April 29, 2012 6:18 AM
    Moderator

All replies

  • See if it helps.

    Sub MyWord()

        With Selection
            .GoToPrevious (wdGoToPage)
            .GoToNext (wdGoToPage)
        End With

    End Sub

    Friday, April 27, 2012 8:07 AM
  • Hi Moishy

    Are we talking about newspaper-type columns or table columns?

    Which version of Word?


    Cindy Meister, VSTO/Word MVP

    Friday, April 27, 2012 8:08 AM
    Moderator
  • Hi Moishy

    Are we talking about newspaper-type columns or table columns?

    Which version of Word?


    Cindy Meister, VSTO/Word MVP

    1. newspaper-type columns.

    2. Currently using 2010 (but prefer something that isn't version dependable).

    Friday, April 27, 2012 8:40 AM
  • See if it helps.

    Sub MyWord()

        With Selection
            .GoToPrevious (wdGoToPage)
            .GoToNext (wdGoToPage)
        End With

    End Sub


    Works well, with one caveat; if the page begins with one column it goes to the next page even though there are multiple columns later on in that page. It should jump to the beginning of the first of the multiple columns (i.e. not the beginning of the page). Sorry I wasn't clear initially.
    Friday, April 27, 2012 8:45 AM
  • See if it solves.

    Sub MyWord()

        Dim i As Long
       
        i = Selection.Information(wdActiveEndPageNumber)
       
            With Selection
                .GoTo what:=wdGoToSection
                If i <> Selection.Information(wdActiveEndPageNumber) Then
                    .GoTo what:=wdGoToPage, which:=wdGoToNext
                End If
            End With

    End Sub

    Friday, April 27, 2012 9:55 AM
  • See if it solves.

    Sub MyWord()

        Dim i As Long
       
        i = Selection.Information(wdActiveEndPageNumber)
       
            With Selection
                .GoTo what:=wdGoToSection
                If i <> Selection.Information(wdActiveEndPageNumber) Then
                    .GoTo what:=wdGoToPage, which:=wdGoToNext
                End If
            End With

    End Sub


    It only works if the cursor is in the multiple column section, otherwise it just moves down one line.
    Friday, April 27, 2012 12:03 PM
  • I am a bit late due to deep night.

    Sub MyWord()

        Dim lPageNum As Long, lSecNum As Long
           
        With Selection
            'Store the present Section and Page number
           
            lSecNum = .Information(wdActiveEndSectionNumber)
            lPageNum = .Information(wdActiveEndPageNumber)
           
            'First goto start of current page
            .GoTo what:=wdGoToPage, which:=wdGoToAbsolute, Count:=lPageNum
           
            'If it is not in current section then goback to start
            ' of current section
            lSecNum = lSecNum - .Information(wdActiveEndSectionNumber)
           
            If lSecNum Then
                .GoTo what:=wdGoToSection, which:=wdGoToRelative, Count:=lSecNum
            End If
        End With
    End Sub

    Saturday, April 28, 2012 6:14 AM
  • Will this work if there are continuous section breaks in the document?
    Saturday, April 28, 2012 10:32 PM
  • I have a multiple column document, how can I get the selection to the beginning of the first column on the current page using vba?

    See if this code sample helps at all...

    Sub GoToFirstCol_NewspaperCols()
        Dim currPageNr As Long
        Dim rngPage As word.Range
        Dim rngSecCols As word.Range
        Dim sec As word.Section
        
        currPageNr = Selection.Information(wdActiveEndPageNumber)
        Set rngPage = ActiveDocument.Bookmarks("\Page").Range
        For Each sec In rngPage.Sections
            If sec.PageSetup.TextColumns.Count > 1 Then
                Set rngSecCols = sec.Range
                If Asc(rngSecCols.Characters(1)) = 12 Then
                    rngSecCols.MoveStart wdCharacter, 1
                End If
                rngSecCols.Collapse wdCollapseStart
                'Debug.Print rngSecCols.Words(1)
                If rngSecCols.Information(wdActiveEndPageNumber) = currPageNr Then
                    rngSecCols.Select
                Else
                    Selection.GoTo What:=wdGoToPage, Count:=currPageNr
                End If
                Exit For
            End If
        Next
    End Sub


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by moishy Sunday, April 29, 2012 11:57 AM
    Sunday, April 29, 2012 6:18 AM
    Moderator
  • I have a multiple column document, how can I get the selection to the beginning of the first column on the current page using vba?

    See if this code sample helps at all...

    Sub GoToFirstCol_NewspaperCols()
        Dim currPageNr As Long
        Dim rngPage As word.Range
        Dim rngSecCols As word.Range
        Dim sec As word.Section
        
        currPageNr = Selection.Information(wdActiveEndPageNumber)
        Set rngPage = ActiveDocument.Bookmarks("\Page").Range
        For Each sec In rngPage.Sections
            If sec.PageSetup.TextColumns.Count > 1 Then
                Set rngSecCols = sec.Range
                If Asc(rngSecCols.Characters(1)) = 12 Then
                    rngSecCols.MoveStart wdCharacter, 1
                End If
                rngSecCols.Collapse wdCollapseStart
                'Debug.Print rngSecCols.Words(1)
                If rngSecCols.Information(wdActiveEndPageNumber) = currPageNr Then
                    rngSecCols.Select
                Else
                    Selection.GoTo What:=wdGoToPage, Count:=currPageNr
                End If
                Exit For
            End If
        Next
    End Sub


    Cindy Meister, VSTO/Word MVP


    Thanks Cindy Works like a treat!!!
    Sunday, April 29, 2012 11:57 AM