Select all in word vba

In this article I will explain how you can use VBA for word to select text.


Select Entire Document:

Using the code snippet below you can select the entire word document:

Selection.WholeStory

Result:

Word VBA Select All


Select The Current Line:

Using the code below you can select the current line:

Sub main()
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
End Sub

 Assume the cursor is somewhere in the middle of the line:

Word VBA Move Cursor To Start of Line
The first line moves the cursor to the start of the line:

Selection.HomeKey Unit:=wdLine

The next line move the cursor to the end of the line while selecting the text:

Selection.EndKey Unit:=wdLine, Extend:=wdExtend

Result:

Word VBA Select Line, Result


Select to End of Line:

The code below will only select till the end of the line:

Selection.EndKey Unit:=wdLine, Extend:=wdExtend

Result:

Word VBA Select Line, To End


Select to Start of Line:

The code below will select text up to the start of the line:

Selection.HomeKey Unit:=wdLine, Extend:=wdExtend

Result:

Word VBA Select Line, To Start


Select Certain Number of Characters:

Lets say we need to select only a certain number of characters from the current location of the cursor. The code below will select 5 characters to the right:

Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend

Result:

Word VBA Select 5 Charcter to right
The code below will select 10 characters to the left:

Selection.MoveLeft Unit:=wdCharacter, Count:=10, Extend:=wdExtend

Result:

Word VBA Select 10 Charcter to left


Select Text up to a Certain Character:

You might want to select text until you reach a certain character. For example a space character, “*”, “/”, …

The code below selects text until a space character is reached:

Sub test()
Dim flag As Boolean
flag = True
While flag = True
    Selection.MoveRight Unit:=wdCharacter, Count:=1, _
    Extend:=wdExtend
    'checks the last character to see if its a space
    If Strings.Right(Selection.Range.Text, 1) = " " Then
        'if it was a space the loop flag will end
        flag = False
    End If
Wend
End Sub

Word VBA, Select up to space
Result:

Word VBA, Select up to space, Result
The line below selects one additional character to the right:

Selection.MoveRight Unit:=wdCharacter, Count:=1, _
Extend:=wdExtend

 The if statement below checks to see if the last character selected is a space character:

If Strings.Right(Selection.Range.Text, 1) = " " Then
...
End If

For more information about the Strings.Right function please see the article below. Although the article was written for Excel, it can be extended to VBA for Word:

  • Excel VBA String Proccessing and Manipulation

See also:

  • Word VBA, Move Cursor to Start of Document
  • Word VBA, Move Cursor to End of Document
  • Word VBA, Move Cursor to End of Line
  • Word VBA, Move Cursor to Start of Line
  • Excel VBA String Processing and Manipulation
  • Word VBA Bookmarks
  • Word VBA, Read All Lines

If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website  www.software-solutions-online.com

I know you can select the whole document like so

oWord.oDoc.ActiveWindow.Selection.WholeStory

What would be the syntax to select the whole document minus the first page?

Context: Excel runs all the code and outputs ranges direct to word to build a report. Reports can be any number of pages and the amount of pages is not known. This code would run as the last piece so document would be built by this point.

asked Jul 29, 2015 at 10:30

99moorem's user avatar

4

The following code selects everything from the second page onward:

Dim startRan As Range

Selection.HomeKey unit:=wdStory
Set startRan = Selection.GoTo(What:=wdGoToBookmark, Name:="page")

oDoc.Range(startRan.End, startRan.End).Select
Selection.EndKey unit:=wdStory, Extend:=wdExtend

answered Jul 29, 2015 at 11:35

LocEngineer's user avatar

LocEngineerLocEngineer

2,8271 gold badge16 silver badges28 bronze badges

3

  • Remove From My Forums
  • Question

  • Hi and thanks so much for your time!!!

    I am trying to find all instances of bulleted lists within a large selection in a word document and assign them a style as per our company branding.

    I have gotten pretty close, the following Macro selects the first line in the first bulleted list in the selection and assigns it the style I require.

    I just need some help getting it to select all the bullets in the doc.

    Sub findbullets22() 
    findbullets22 Macro 
    Dim oPara As Word.Paragraph 
    With Selection 
    For Each oPara In .Paragraphs 
    If oPara.Range.ListFormat.ListType = _ 
    WdListType.wdListBullet Then 
    oPara.Range.Select 
    End If 
    Next 
    End With 
    Selection.Style = ActiveDocument.Styles("List Paragraph") 
    End Sub 

    Any help you could provide would be amazing — I am starting to go nuts LOL :)

    • Edited by

      Thursday, August 16, 2012 5:49 AM

    • Moved by
      Youen Zen
      Friday, August 17, 2012 2:32 AM
      To provide better support (From:Visual Basic General)

Answers

  • Well if you want to process ALL bulleted paragraphs (not just what you select), then try:

    Sub findbullets22()
    Dim oPara As Word.Paragraph

    For Each oPara In ActiveDocument.Paragraphs
       If oPara.Range.ListFormat.ListType = _
             WdListType.wdListBullet Then
          oPara.Style = «List Paragraph»
       End If
    Next
    End Sub

    You do not actually need to Select anything.


    Word MVP

    • Proposed as answer by
      fumei2
      Saturday, August 18, 2012 11:28 PM
    • Marked as answer by
      Quist Zhang
      Sunday, August 26, 2012 5:46 PM

  • Hi,

    In Word, there is no such settings or button to directly select all tables at once.

    Maybe some codes can be used for your requirement. I found the following article provide a code about select all tables in a document:

    https://www.extendoffice.com/documents/word/639-word-select-all-tables.html

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please
    make sure that you completely understand the risk before retrieving any suggestions from the above link.

    It indicates that we can use a VBA code to select all tables in the document :

    Step 1: Press “Alt-F11” to open the Microsoft Visual Basic for Application window;
    Step 2: Click Module on the Insert tab, copy and paste the following VBA code into the Module window;

    Sub selecttables()
     Dim mytable As Table
     Application.ScreenUpdating = False

     For Each mytable In ActiveDocument.Tables
     mytable.Range.Editors.Add wdEditorEveryone
     Next
     ActiveDocument.SelectAllEditableRanges (wdEditorEveryone)
     ActiveDocument.DeleteAllEditableRanges (wdEditorEveryone)
     Application.ScreenUpdating = True
     End Sub

    Step 3: Save the VBA file and then close the Module window;
    Step 4: Click Run Macro on the Run tab;
    Step 5: Select the saved VBA file and click Run button.

    If you still have any further issue about it, it is better to ask a question in

    Word for Developers forum for more help.


    Regards,

    Winnie Liang


    Please remember to
    mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact
    tnmff@microsoft.com.

    • Proposed as answer by

      Monday, October 10, 2016 2:50 AM

    • Marked as answer by
      Winnie LiangMicrosoft contingent staff
      Tuesday, October 11, 2016 10:02 AM

  • Hey all,

    Attempting to get the bottom portion of this code to work in Excel VBA, I took the language from Word VBA so not sure how well it translates

    ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument

    Receiving an error «Runtime error ‘438’: Object doesn’t support this property or method»

    Code:

    Sub CopyandRename()
        Dim str1 As String
        str1 = "Q:ICNew StructureIC ToolkitTemplates1 Plan Doc Template16 SourceIC Plan Doc Template v1.0.docx"
        PlanDocTemplate = Application.ActiveWorkbook.Path & "" & Range("A1").Value & ".docx"
        
        Call FileCopy(str1, PlanDocTemplate)
        
        strWorkbookName = ThisWorkbook.Path & "" & ThisWorkbook.Name
        Worksheets("Data").Activate
           
    'Opens New Plan Doc Template
    
        Set appWD = CreateObject("Word.Application")
        appWD.Visible = True
    
        appWD.Documents.Open Filename:=PlanDocTemplate
        
        ActiveDocument.MailMerge.OpenDataSource Name:=strWorkbookName, _
        Format:=wdMergeInfoFromExcelDDE, _
        ConfirmConversions:=True, _
        ReadOnly:=False, _
        LinkToSource:=True, _
        AddtoRecentFiles:=False, _
        PasswordDocument:="", _
        PasswordTemplate:="", _
        Revert:=False, _
        Connection:="Entire Spreadsheet", _
        SQLStatement:="SELECT * FROM `Data$`", _
        SQLStatement1:="", _
        SubType:=wdMergeSubTypeOther
        
        appWD.Visible = True
    
        appWD.Selection.WholeStory
        appWD.Selection.Fields.Update
        appWD.Selection.Fields.Unlink
        appWD.ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument
        appWD.ActiveDocument.Save
    
    End Sub

    The code is intended to change the newly merged Word Document into a regular word document.

    Any help is greatly appreciated, thanks,
    Rich

    Понравилась статья? Поделить с друзьями:
  • Select all in word 2012
  • Select all in one column excel
  • Select all in excel macro
  • Select all headings in word
  • Select all from word document