Share via

VBA for finding all words beginning with § and transfering them in Excel

Anonymous
2014-05-30T11:55:39+00:00

My aim is to

(1)   find in the Word document all words beginning with §-character (it can appear in the beginning only), and fill them in a string array,

(2)   transfer this array for the next treatment into Excel app.

Being expert in Excel VBA, I’m lost in Word part of the task.

TIA

Petr

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. Suzanne S Barnhill 277.2K Reputation points MVP Volunteer Moderator
    2014-05-30T12:30:27+00:00

    I have no VBA expertise in Word or Excel, but if a series of paragraphs or rows, one word in each, would do what you want, then you can use a simple Find operation:

    1. Press Ctrl+F to open the Find dialog.
    2. Check the box for "Highlight all items found in" and select "Main Document."
    3. Click More to expand the dialog.
    4. Check the box for "Use wildcards."
    5. Paste this search string into the "Find what" box: §[A-Za-z]{1,}>
    6. Click Find All. All the words will be selected.
    7. Click on the title bar of the document to return focus to it without deselecting the found words.
    8. Press Ctrl+C to copy the selected words.
    9. Press Ctrl+V to paste the words into a new blank document (one per paragraph) or into Excel (one per row).

    Note: If the terms to be found contain numbers, the search string should be §[A-Za-z0-9]{1,}>

    1 person found this answer helpful.
    0 comments No comments

Answer accepted by question author

  1. Anonymous
    2014-05-30T13:47:37+00:00

    From Excel VBA, the following function will list the Words in a named document. You can call the function as in the Test Macro and process as you wish.

    Function GetWords(strDocFullName As String) As String

    Dim wdApp As Object

    Dim wdDoc As Object

    Dim oRng As Object

    Dim bStarted As Boolean

    Dim strWordList As String

        On Error Resume Next

        Set wdApp = GetObject(, "Word.Application")

        If Err Then

            Set wdApp = CreateObject("Word.Application")

            bStarted = True

        End If

        On Error GoTo 0

        Set wdDoc = wdApp.Documents.Open(strDocFullName)

        Set oRng = wdDoc.Range

        strWordList = ""

        With oRng.Find

            Do While .Execute(FindText:="§*>", MatchWildcards:=True)

                strWordList = strWordList & "|" & oRng

            Loop

        End With

        strWordList = Right(strWordList, Len(strWordList) - 1)

        wdDoc.Close 0

        If bStarted Then wdApp.Quit

        GetWords = strWordList

    End Function

    Sub Test()

        MsgBox GetWords("D:\Path\docname.docx")

    End Sub

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Suzanne S Barnhill 277.2K Reputation points MVP Volunteer Moderator
    2014-05-31T12:46:32+00:00

    Graham is the expert on using wildcards. I was vaguely thinking * was not available when using wildcards; otherwise I would have used it, too. Graham's string is definitely more elegant.

    0 comments No comments
  2. Anonymous
    2014-05-31T10:34:03+00:00

    The search string I suggested was not necessarily better, it just worked for this job. In general the * wildcard is a blunt instrument that is better avoided - see http://www.gmayor.com/replace_using_wildcards.htm

    As for the error, you will get an error if Word is not running when you call GetObject (i.e you are getting the currently running object) and thus the macro allows you to use the Word that is open, or open it if not.

    Unlike Excel, you can open a document in Word that is already open so there is no need to error trap that.

    0 comments No comments
  3. Anonymous
    2014-05-31T07:28:11+00:00

    Suzanne,

    This is a repeating job, and it cannot be rendered without macro. Though, your access discloses a clever possibility of copying all the selected words at once. The copy into the Excel worksheet is arranged into a column, exactly what needed. Only the search string “§*>” according to Graham gives better results.

    Graham,

    The macro is, after all, quite understandable and works perfectly. Many thanks. Just to enhance my education: under which condition can an error in GetObject arise?

    Petr

    0 comments No comments