Word Macro: Find Key Word(s) and Extract Sentence to Excel












0















I'm a Proposal Manager and use the below macro to search a Word file for the word "shall" and extract the sentence containing shall to Excel. It works but I can't figure out how to edit the code so it can search for more than one word in the order they appear in the file.



Example:
1. Search for "shall" or "must".
2. It shouldn't search for "shall" and then look for "must". It should search for "shall" or "must" then "shall" or "must".
3. If a paragraph has four sentences, and the first sentence contains "shall", the second contains "shall", the third contains "must", and the fourth contains "shall", the macro should extract to Excel in that order.



Sub FindWordCopySentence()
Dim appExcel As Object
Dim objSheet As Object
Dim aRange As Range
Dim intRowCount As Integer
intRowCount = 1
Set aRange = ActiveDocument.Range
With aRange.Find
Do
.Text = "shall" ' the word I am looking for
.Execute
If .Found Then
aRange.Expand Unit:=wdSentence
aRange.Copy
aRange.Collapse wdCollapseEnd
If objSheet Is Nothing Then
Set appExcel = CreateObject("Excel.Application")
'Change the file path to match the location of your test.xls
Set objSheet = appExcel.workbooks.Open("C:Temptest.xlsx").Sheets("Sheet1")
intRowCount = 1
End If
objSheet.Cells(intRowCount, 1).Select
objSheet.Paste
intRowCount = intRowCount + 1
End If
Loop While .Found
End With
If Not objSheet Is Nothing Then
appExcel.workbooks(1).Close True
appExcel.Quit
Set objSheet = Nothing
Set appExcel = Nothing
End If
Set aRange = Nothing
End Sub









share|improve this question





























    0















    I'm a Proposal Manager and use the below macro to search a Word file for the word "shall" and extract the sentence containing shall to Excel. It works but I can't figure out how to edit the code so it can search for more than one word in the order they appear in the file.



    Example:
    1. Search for "shall" or "must".
    2. It shouldn't search for "shall" and then look for "must". It should search for "shall" or "must" then "shall" or "must".
    3. If a paragraph has four sentences, and the first sentence contains "shall", the second contains "shall", the third contains "must", and the fourth contains "shall", the macro should extract to Excel in that order.



    Sub FindWordCopySentence()
    Dim appExcel As Object
    Dim objSheet As Object
    Dim aRange As Range
    Dim intRowCount As Integer
    intRowCount = 1
    Set aRange = ActiveDocument.Range
    With aRange.Find
    Do
    .Text = "shall" ' the word I am looking for
    .Execute
    If .Found Then
    aRange.Expand Unit:=wdSentence
    aRange.Copy
    aRange.Collapse wdCollapseEnd
    If objSheet Is Nothing Then
    Set appExcel = CreateObject("Excel.Application")
    'Change the file path to match the location of your test.xls
    Set objSheet = appExcel.workbooks.Open("C:Temptest.xlsx").Sheets("Sheet1")
    intRowCount = 1
    End If
    objSheet.Cells(intRowCount, 1).Select
    objSheet.Paste
    intRowCount = intRowCount + 1
    End If
    Loop While .Found
    End With
    If Not objSheet Is Nothing Then
    appExcel.workbooks(1).Close True
    appExcel.Quit
    Set objSheet = Nothing
    Set appExcel = Nothing
    End If
    Set aRange = Nothing
    End Sub









    share|improve this question



























      0












      0








      0








      I'm a Proposal Manager and use the below macro to search a Word file for the word "shall" and extract the sentence containing shall to Excel. It works but I can't figure out how to edit the code so it can search for more than one word in the order they appear in the file.



      Example:
      1. Search for "shall" or "must".
      2. It shouldn't search for "shall" and then look for "must". It should search for "shall" or "must" then "shall" or "must".
      3. If a paragraph has four sentences, and the first sentence contains "shall", the second contains "shall", the third contains "must", and the fourth contains "shall", the macro should extract to Excel in that order.



      Sub FindWordCopySentence()
      Dim appExcel As Object
      Dim objSheet As Object
      Dim aRange As Range
      Dim intRowCount As Integer
      intRowCount = 1
      Set aRange = ActiveDocument.Range
      With aRange.Find
      Do
      .Text = "shall" ' the word I am looking for
      .Execute
      If .Found Then
      aRange.Expand Unit:=wdSentence
      aRange.Copy
      aRange.Collapse wdCollapseEnd
      If objSheet Is Nothing Then
      Set appExcel = CreateObject("Excel.Application")
      'Change the file path to match the location of your test.xls
      Set objSheet = appExcel.workbooks.Open("C:Temptest.xlsx").Sheets("Sheet1")
      intRowCount = 1
      End If
      objSheet.Cells(intRowCount, 1).Select
      objSheet.Paste
      intRowCount = intRowCount + 1
      End If
      Loop While .Found
      End With
      If Not objSheet Is Nothing Then
      appExcel.workbooks(1).Close True
      appExcel.Quit
      Set objSheet = Nothing
      Set appExcel = Nothing
      End If
      Set aRange = Nothing
      End Sub









      share|improve this question
















      I'm a Proposal Manager and use the below macro to search a Word file for the word "shall" and extract the sentence containing shall to Excel. It works but I can't figure out how to edit the code so it can search for more than one word in the order they appear in the file.



      Example:
      1. Search for "shall" or "must".
      2. It shouldn't search for "shall" and then look for "must". It should search for "shall" or "must" then "shall" or "must".
      3. If a paragraph has four sentences, and the first sentence contains "shall", the second contains "shall", the third contains "must", and the fourth contains "shall", the macro should extract to Excel in that order.



      Sub FindWordCopySentence()
      Dim appExcel As Object
      Dim objSheet As Object
      Dim aRange As Range
      Dim intRowCount As Integer
      intRowCount = 1
      Set aRange = ActiveDocument.Range
      With aRange.Find
      Do
      .Text = "shall" ' the word I am looking for
      .Execute
      If .Found Then
      aRange.Expand Unit:=wdSentence
      aRange.Copy
      aRange.Collapse wdCollapseEnd
      If objSheet Is Nothing Then
      Set appExcel = CreateObject("Excel.Application")
      'Change the file path to match the location of your test.xls
      Set objSheet = appExcel.workbooks.Open("C:Temptest.xlsx").Sheets("Sheet1")
      intRowCount = 1
      End If
      objSheet.Cells(intRowCount, 1).Select
      objSheet.Paste
      intRowCount = intRowCount + 1
      End If
      Loop While .Found
      End With
      If Not objSheet Is Nothing Then
      appExcel.workbooks(1).Close True
      appExcel.Quit
      Set objSheet = Nothing
      Set appExcel = Nothing
      End If
      Set aRange = Nothing
      End Sub






      excel vba ms-word






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 15:53









      dwirony

      4,34731334




      4,34731334










      asked Nov 15 '18 at 15:36









      ShaneShane

      1




      1
























          2 Answers
          2






          active

          oldest

          votes


















          0














          A fairly basic problem you'll likely encounter is that VBA has no idea what a grammatical sentence is. For example, consider the following:



          Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.



          For you and me, that would count as one sentence; for VBA it counts as 5 sentences. Accordingly, the following macro simply captures the whole of the paragraphs concerned. Much of the code is concerned with establishing that the workbook and worksheet exist; I haven't included error-checking as to whether the file might already be opened, though.



          Sub Demo()
          'Note: This code requires a VBA reference to the Excel object library
          Dim xlApp As New Excel.Application, xlWkBk As Excel.Workbook
          Dim xlSht As Excel.Worksheet, StrWkBkNm As String, StrWkSht As String
          Dim lRow As Long, Para As Paragraph
          StrWkBkNm = "C:Temptest.xlsx": StrWkSht = "Sheet1"
          If Dir(StrWkBkNm) = "" Then
          MsgBox "Cannot find the designated workbook: " & StrWkBkNm, vbExclamation
          Exit Sub
          End If
          With xlApp
          .Visible = True
          ' The file is available, so open it.
          Set xlWkBk = .Workbooks.Open(FileName:=StrWkBkNm, ReadOnly:=False, AddToMru:=False)
          If xlWkBk Is Nothing Then
          MsgBox "Cannot open:" & vbCr & StrWkBkNm, vbExclamation
          .Quit
          Exit Sub
          End If
          ' Process the workbook.
          With xlWkBk
          'Ensure the worksheet exists
          If SheetExists(StrWkSht) = True Then
          Set xlSht = .Worksheets(StrWkSht)
          With xlSht
          ' Find the last-used row in column A.
          lRow = .Cells(.Rows.Count, 1).End(-4162).Row ' -4162 = xlUp
          End With
          For Each Para In ActiveDocument.Paragraphs
          With Para
          If (InStr(.Range.Text, "shall") > 0) Or (InStr(.Range.Text, "shall") > 0) Then
          lRow = lRow + 1
          xlSht.Range("A" & lRow).Value = .Range.Text
          End If
          End With
          Next
          Else
          MsgBox "Cannot find the designated worksheet: " & StrWkSht, vbExclamation
          .Close False
          xlApp.Quit
          End If
          End With
          End With
          ' Release Excel object memory
          Set xlSht = Nothing: Set xlWkBk = Nothing: Set xlApp = Nothing
          End Sub





          share|improve this answer
























          • This is close to what I need but one big reason for the macro is to extract each sentence with "shall" to its own row in Excel. The code I provided does that. The thing I can't get it to do is search for more than one word. Like "shall" and "must". This isn't the right code but something like ".Text = "shall" or "must".

            – Shane
            Nov 16 '18 at 20:00











          • As I already said, though, VBA has no idea what a grammatical sentence is - it really doesn't know where a sentence starts or ends. If you put 'shall' and 'must' into the example I posted, what VBA would extract in each case using its 'sentences' property would be only part of the grammatical sentence. For example, suppose you insert 'Shall ' at the start of the example I gave. VBA's sentence property would return just "Shall Mr.", which isn't very useful. That you've gotten away with it so far only reflects the fact you've only tried it with simple sentences lacking even abbreviations.

            – macropod
            Nov 16 '18 at 21:06



















          0














          One way to approach this is to:



          (1) Use Word's search/replace to wrap the words of interest (shall, will) with tags, e.g., shall, will. and can be anything you don't expect to be in the Word source document;



          (2) Use a modified version of your FindWordCopySentence to find the tagged words, then copy the corresponding sentences to Excel; then



          (3) Use Word's search/replace to clean-up (remove the tags). Or you could just close the Word doc without saving.



          Here's the code with some comments to explain the details:



          Option Explicit
          Const START_TAG As String = "$$SWSTART_"
          Const END_TAG As String = "_SWEND$$"


          Sub AddTagsToShallWords()
          ' SHALL_WORDS is a |-delimited string of the words you want to replace
          ' The "[Ss]" means that the first letter can be upper or lower case (same for [Ww])
          ' This is designed to be extendible, e.g. you could add "must" by appending |[Mm]ust
          Const SHALL_WORDS = "[Ss]hall|[Ww]ill"
          Dim v As Variant
          Dim I As Long
          Dim s As String
          Dim aRange As Range
          Dim sFindText As String
          Dim sReplaceText As String

          ' Create shall words to an array
          v = Split(SHALL_WORDS, "|")

          ' Replace each shall word with its tagged version
          For I = 0 To UBound(v)
          s = CStr(v(I))
          Set aRange = ActiveDocument.Range

          ' Create the FindText arg, e.g. "(<[Ss]hall>)"
          ' The parentheses create a "group" that we use to build the replacement text
          ' The <> are used to mark the beginning and end of words
          ' to prevent FindText="will" from matching "swill", "goodwill", etc.
          sFindText = "(<" & s & ">)"

          ' Create the ReplaceText arg. "1" is the found text. Wrap it in the tags.
          sReplaceText = START_TAG & "1" & END_TAG
          With aRange.Find
          .MatchWildcards = True
          .Execute FindText:=sFindText, ReplaceWith:=sReplaceText, Replace:=wdReplaceAll
          End With
          Next I
          Set aRange = Nothing
          End Sub



          Sub RemoveTags()
          Dim aRange As Range
          Dim sFindText As String
          Dim sReplaceText As String

          Set aRange = ActiveDocument.Range
          sFindText = START_TAG & "(*)" & END_TAG
          sReplaceText = "1"
          With aRange.Find
          .MatchWildcards = True
          .Execute FindText:=sFindText, ReplaceWith:=sReplaceText, Replace:=wdReplaceAll
          End With
          Set aRange = Nothing
          End Sub



          Sub FindWordCopySentence()
          Dim appExcel As Object
          Dim objSheet As Object
          Dim aRange As Range
          Dim intRowCount As Integer
          Dim s As String
          intRowCount = 1
          Set aRange = ActiveDocument.Range
          With aRange.Find
          .MatchWildcards = True
          Do
          .Text = START_TAG & "*" & END_TAG ' the word I am looking for
          .Execute
          If .Found Then
          aRange.Expand Unit:=wdSentence
          s = aRange.Text
          s = Replace(s, START_TAG, "")
          s = Replace(s, END_TAG, "")
          aRange.Collapse wdCollapseEnd
          If objSheet Is Nothing Then
          Set appExcel = CreateObject("Excel.Application")
          'Change the file path to match the location of your test.xls
          Set objSheet = appExcel.workbooks.Open("C:Temptest.xlsx").Sheets("Sheet1")
          intRowCount = 1
          End If
          objSheet.Cells(intRowCount, 1).Formula = s
          intRowCount = intRowCount + 1
          End If
          Loop While .Found
          End With
          If Not objSheet Is Nothing Then
          appExcel.workbooks(1).Close True
          appExcel.Quit
          Set objSheet = Nothing
          Set appExcel = Nothing
          End If
          Set aRange = Nothing
          End Sub


          Hope that helps






          share|improve this answer
























          • Thanks. In FindWordCopySentence I got a run-time error '6' Overflow on "intRowCount = intRowCount + 1".

            – Shane
            Nov 16 '18 at 20:08











          • Rats. intRowCount is declared as an Integer so it overflows on exceeding 32,767. In my tests the code worked on a sample doc with 2 shalls and 1 will. One possibility (unlikely) is that you have more than 32,767 shalls and wills. If that's the case you can change intRowCount to Long. Another possibility is that for some reason Word's Find is looping through your document over and over. You can investigate by adding a temporary line of code If intRowCount > 10000 Then Exit Do in the do-loop. This should terminate the program before it overflows so you can inspect the Excel file.

            – xidgel
            Nov 16 '18 at 20:42











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322875%2fword-macro-find-key-words-and-extract-sentence-to-excel%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          A fairly basic problem you'll likely encounter is that VBA has no idea what a grammatical sentence is. For example, consider the following:



          Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.



          For you and me, that would count as one sentence; for VBA it counts as 5 sentences. Accordingly, the following macro simply captures the whole of the paragraphs concerned. Much of the code is concerned with establishing that the workbook and worksheet exist; I haven't included error-checking as to whether the file might already be opened, though.



          Sub Demo()
          'Note: This code requires a VBA reference to the Excel object library
          Dim xlApp As New Excel.Application, xlWkBk As Excel.Workbook
          Dim xlSht As Excel.Worksheet, StrWkBkNm As String, StrWkSht As String
          Dim lRow As Long, Para As Paragraph
          StrWkBkNm = "C:Temptest.xlsx": StrWkSht = "Sheet1"
          If Dir(StrWkBkNm) = "" Then
          MsgBox "Cannot find the designated workbook: " & StrWkBkNm, vbExclamation
          Exit Sub
          End If
          With xlApp
          .Visible = True
          ' The file is available, so open it.
          Set xlWkBk = .Workbooks.Open(FileName:=StrWkBkNm, ReadOnly:=False, AddToMru:=False)
          If xlWkBk Is Nothing Then
          MsgBox "Cannot open:" & vbCr & StrWkBkNm, vbExclamation
          .Quit
          Exit Sub
          End If
          ' Process the workbook.
          With xlWkBk
          'Ensure the worksheet exists
          If SheetExists(StrWkSht) = True Then
          Set xlSht = .Worksheets(StrWkSht)
          With xlSht
          ' Find the last-used row in column A.
          lRow = .Cells(.Rows.Count, 1).End(-4162).Row ' -4162 = xlUp
          End With
          For Each Para In ActiveDocument.Paragraphs
          With Para
          If (InStr(.Range.Text, "shall") > 0) Or (InStr(.Range.Text, "shall") > 0) Then
          lRow = lRow + 1
          xlSht.Range("A" & lRow).Value = .Range.Text
          End If
          End With
          Next
          Else
          MsgBox "Cannot find the designated worksheet: " & StrWkSht, vbExclamation
          .Close False
          xlApp.Quit
          End If
          End With
          End With
          ' Release Excel object memory
          Set xlSht = Nothing: Set xlWkBk = Nothing: Set xlApp = Nothing
          End Sub





          share|improve this answer
























          • This is close to what I need but one big reason for the macro is to extract each sentence with "shall" to its own row in Excel. The code I provided does that. The thing I can't get it to do is search for more than one word. Like "shall" and "must". This isn't the right code but something like ".Text = "shall" or "must".

            – Shane
            Nov 16 '18 at 20:00











          • As I already said, though, VBA has no idea what a grammatical sentence is - it really doesn't know where a sentence starts or ends. If you put 'shall' and 'must' into the example I posted, what VBA would extract in each case using its 'sentences' property would be only part of the grammatical sentence. For example, suppose you insert 'Shall ' at the start of the example I gave. VBA's sentence property would return just "Shall Mr.", which isn't very useful. That you've gotten away with it so far only reflects the fact you've only tried it with simple sentences lacking even abbreviations.

            – macropod
            Nov 16 '18 at 21:06
















          0














          A fairly basic problem you'll likely encounter is that VBA has no idea what a grammatical sentence is. For example, consider the following:



          Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.



          For you and me, that would count as one sentence; for VBA it counts as 5 sentences. Accordingly, the following macro simply captures the whole of the paragraphs concerned. Much of the code is concerned with establishing that the workbook and worksheet exist; I haven't included error-checking as to whether the file might already be opened, though.



          Sub Demo()
          'Note: This code requires a VBA reference to the Excel object library
          Dim xlApp As New Excel.Application, xlWkBk As Excel.Workbook
          Dim xlSht As Excel.Worksheet, StrWkBkNm As String, StrWkSht As String
          Dim lRow As Long, Para As Paragraph
          StrWkBkNm = "C:Temptest.xlsx": StrWkSht = "Sheet1"
          If Dir(StrWkBkNm) = "" Then
          MsgBox "Cannot find the designated workbook: " & StrWkBkNm, vbExclamation
          Exit Sub
          End If
          With xlApp
          .Visible = True
          ' The file is available, so open it.
          Set xlWkBk = .Workbooks.Open(FileName:=StrWkBkNm, ReadOnly:=False, AddToMru:=False)
          If xlWkBk Is Nothing Then
          MsgBox "Cannot open:" & vbCr & StrWkBkNm, vbExclamation
          .Quit
          Exit Sub
          End If
          ' Process the workbook.
          With xlWkBk
          'Ensure the worksheet exists
          If SheetExists(StrWkSht) = True Then
          Set xlSht = .Worksheets(StrWkSht)
          With xlSht
          ' Find the last-used row in column A.
          lRow = .Cells(.Rows.Count, 1).End(-4162).Row ' -4162 = xlUp
          End With
          For Each Para In ActiveDocument.Paragraphs
          With Para
          If (InStr(.Range.Text, "shall") > 0) Or (InStr(.Range.Text, "shall") > 0) Then
          lRow = lRow + 1
          xlSht.Range("A" & lRow).Value = .Range.Text
          End If
          End With
          Next
          Else
          MsgBox "Cannot find the designated worksheet: " & StrWkSht, vbExclamation
          .Close False
          xlApp.Quit
          End If
          End With
          End With
          ' Release Excel object memory
          Set xlSht = Nothing: Set xlWkBk = Nothing: Set xlApp = Nothing
          End Sub





          share|improve this answer
























          • This is close to what I need but one big reason for the macro is to extract each sentence with "shall" to its own row in Excel. The code I provided does that. The thing I can't get it to do is search for more than one word. Like "shall" and "must". This isn't the right code but something like ".Text = "shall" or "must".

            – Shane
            Nov 16 '18 at 20:00











          • As I already said, though, VBA has no idea what a grammatical sentence is - it really doesn't know where a sentence starts or ends. If you put 'shall' and 'must' into the example I posted, what VBA would extract in each case using its 'sentences' property would be only part of the grammatical sentence. For example, suppose you insert 'Shall ' at the start of the example I gave. VBA's sentence property would return just "Shall Mr.", which isn't very useful. That you've gotten away with it so far only reflects the fact you've only tried it with simple sentences lacking even abbreviations.

            – macropod
            Nov 16 '18 at 21:06














          0












          0








          0







          A fairly basic problem you'll likely encounter is that VBA has no idea what a grammatical sentence is. For example, consider the following:



          Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.



          For you and me, that would count as one sentence; for VBA it counts as 5 sentences. Accordingly, the following macro simply captures the whole of the paragraphs concerned. Much of the code is concerned with establishing that the workbook and worksheet exist; I haven't included error-checking as to whether the file might already be opened, though.



          Sub Demo()
          'Note: This code requires a VBA reference to the Excel object library
          Dim xlApp As New Excel.Application, xlWkBk As Excel.Workbook
          Dim xlSht As Excel.Worksheet, StrWkBkNm As String, StrWkSht As String
          Dim lRow As Long, Para As Paragraph
          StrWkBkNm = "C:Temptest.xlsx": StrWkSht = "Sheet1"
          If Dir(StrWkBkNm) = "" Then
          MsgBox "Cannot find the designated workbook: " & StrWkBkNm, vbExclamation
          Exit Sub
          End If
          With xlApp
          .Visible = True
          ' The file is available, so open it.
          Set xlWkBk = .Workbooks.Open(FileName:=StrWkBkNm, ReadOnly:=False, AddToMru:=False)
          If xlWkBk Is Nothing Then
          MsgBox "Cannot open:" & vbCr & StrWkBkNm, vbExclamation
          .Quit
          Exit Sub
          End If
          ' Process the workbook.
          With xlWkBk
          'Ensure the worksheet exists
          If SheetExists(StrWkSht) = True Then
          Set xlSht = .Worksheets(StrWkSht)
          With xlSht
          ' Find the last-used row in column A.
          lRow = .Cells(.Rows.Count, 1).End(-4162).Row ' -4162 = xlUp
          End With
          For Each Para In ActiveDocument.Paragraphs
          With Para
          If (InStr(.Range.Text, "shall") > 0) Or (InStr(.Range.Text, "shall") > 0) Then
          lRow = lRow + 1
          xlSht.Range("A" & lRow).Value = .Range.Text
          End If
          End With
          Next
          Else
          MsgBox "Cannot find the designated worksheet: " & StrWkSht, vbExclamation
          .Close False
          xlApp.Quit
          End If
          End With
          End With
          ' Release Excel object memory
          Set xlSht = Nothing: Set xlWkBk = Nothing: Set xlApp = Nothing
          End Sub





          share|improve this answer













          A fairly basic problem you'll likely encounter is that VBA has no idea what a grammatical sentence is. For example, consider the following:



          Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.



          For you and me, that would count as one sentence; for VBA it counts as 5 sentences. Accordingly, the following macro simply captures the whole of the paragraphs concerned. Much of the code is concerned with establishing that the workbook and worksheet exist; I haven't included error-checking as to whether the file might already be opened, though.



          Sub Demo()
          'Note: This code requires a VBA reference to the Excel object library
          Dim xlApp As New Excel.Application, xlWkBk As Excel.Workbook
          Dim xlSht As Excel.Worksheet, StrWkBkNm As String, StrWkSht As String
          Dim lRow As Long, Para As Paragraph
          StrWkBkNm = "C:Temptest.xlsx": StrWkSht = "Sheet1"
          If Dir(StrWkBkNm) = "" Then
          MsgBox "Cannot find the designated workbook: " & StrWkBkNm, vbExclamation
          Exit Sub
          End If
          With xlApp
          .Visible = True
          ' The file is available, so open it.
          Set xlWkBk = .Workbooks.Open(FileName:=StrWkBkNm, ReadOnly:=False, AddToMru:=False)
          If xlWkBk Is Nothing Then
          MsgBox "Cannot open:" & vbCr & StrWkBkNm, vbExclamation
          .Quit
          Exit Sub
          End If
          ' Process the workbook.
          With xlWkBk
          'Ensure the worksheet exists
          If SheetExists(StrWkSht) = True Then
          Set xlSht = .Worksheets(StrWkSht)
          With xlSht
          ' Find the last-used row in column A.
          lRow = .Cells(.Rows.Count, 1).End(-4162).Row ' -4162 = xlUp
          End With
          For Each Para In ActiveDocument.Paragraphs
          With Para
          If (InStr(.Range.Text, "shall") > 0) Or (InStr(.Range.Text, "shall") > 0) Then
          lRow = lRow + 1
          xlSht.Range("A" & lRow).Value = .Range.Text
          End If
          End With
          Next
          Else
          MsgBox "Cannot find the designated worksheet: " & StrWkSht, vbExclamation
          .Close False
          xlApp.Quit
          End If
          End With
          End With
          ' Release Excel object memory
          Set xlSht = Nothing: Set xlWkBk = Nothing: Set xlApp = Nothing
          End Sub






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 22:37









          macropodmacropod

          2,7912310




          2,7912310













          • This is close to what I need but one big reason for the macro is to extract each sentence with "shall" to its own row in Excel. The code I provided does that. The thing I can't get it to do is search for more than one word. Like "shall" and "must". This isn't the right code but something like ".Text = "shall" or "must".

            – Shane
            Nov 16 '18 at 20:00











          • As I already said, though, VBA has no idea what a grammatical sentence is - it really doesn't know where a sentence starts or ends. If you put 'shall' and 'must' into the example I posted, what VBA would extract in each case using its 'sentences' property would be only part of the grammatical sentence. For example, suppose you insert 'Shall ' at the start of the example I gave. VBA's sentence property would return just "Shall Mr.", which isn't very useful. That you've gotten away with it so far only reflects the fact you've only tried it with simple sentences lacking even abbreviations.

            – macropod
            Nov 16 '18 at 21:06



















          • This is close to what I need but one big reason for the macro is to extract each sentence with "shall" to its own row in Excel. The code I provided does that. The thing I can't get it to do is search for more than one word. Like "shall" and "must". This isn't the right code but something like ".Text = "shall" or "must".

            – Shane
            Nov 16 '18 at 20:00











          • As I already said, though, VBA has no idea what a grammatical sentence is - it really doesn't know where a sentence starts or ends. If you put 'shall' and 'must' into the example I posted, what VBA would extract in each case using its 'sentences' property would be only part of the grammatical sentence. For example, suppose you insert 'Shall ' at the start of the example I gave. VBA's sentence property would return just "Shall Mr.", which isn't very useful. That you've gotten away with it so far only reflects the fact you've only tried it with simple sentences lacking even abbreviations.

            – macropod
            Nov 16 '18 at 21:06

















          This is close to what I need but one big reason for the macro is to extract each sentence with "shall" to its own row in Excel. The code I provided does that. The thing I can't get it to do is search for more than one word. Like "shall" and "must". This isn't the right code but something like ".Text = "shall" or "must".

          – Shane
          Nov 16 '18 at 20:00





          This is close to what I need but one big reason for the macro is to extract each sentence with "shall" to its own row in Excel. The code I provided does that. The thing I can't get it to do is search for more than one word. Like "shall" and "must". This isn't the right code but something like ".Text = "shall" or "must".

          – Shane
          Nov 16 '18 at 20:00













          As I already said, though, VBA has no idea what a grammatical sentence is - it really doesn't know where a sentence starts or ends. If you put 'shall' and 'must' into the example I posted, what VBA would extract in each case using its 'sentences' property would be only part of the grammatical sentence. For example, suppose you insert 'Shall ' at the start of the example I gave. VBA's sentence property would return just "Shall Mr.", which isn't very useful. That you've gotten away with it so far only reflects the fact you've only tried it with simple sentences lacking even abbreviations.

          – macropod
          Nov 16 '18 at 21:06





          As I already said, though, VBA has no idea what a grammatical sentence is - it really doesn't know where a sentence starts or ends. If you put 'shall' and 'must' into the example I posted, what VBA would extract in each case using its 'sentences' property would be only part of the grammatical sentence. For example, suppose you insert 'Shall ' at the start of the example I gave. VBA's sentence property would return just "Shall Mr.", which isn't very useful. That you've gotten away with it so far only reflects the fact you've only tried it with simple sentences lacking even abbreviations.

          – macropod
          Nov 16 '18 at 21:06













          0














          One way to approach this is to:



          (1) Use Word's search/replace to wrap the words of interest (shall, will) with tags, e.g., shall, will. and can be anything you don't expect to be in the Word source document;



          (2) Use a modified version of your FindWordCopySentence to find the tagged words, then copy the corresponding sentences to Excel; then



          (3) Use Word's search/replace to clean-up (remove the tags). Or you could just close the Word doc without saving.



          Here's the code with some comments to explain the details:



          Option Explicit
          Const START_TAG As String = "$$SWSTART_"
          Const END_TAG As String = "_SWEND$$"


          Sub AddTagsToShallWords()
          ' SHALL_WORDS is a |-delimited string of the words you want to replace
          ' The "[Ss]" means that the first letter can be upper or lower case (same for [Ww])
          ' This is designed to be extendible, e.g. you could add "must" by appending |[Mm]ust
          Const SHALL_WORDS = "[Ss]hall|[Ww]ill"
          Dim v As Variant
          Dim I As Long
          Dim s As String
          Dim aRange As Range
          Dim sFindText As String
          Dim sReplaceText As String

          ' Create shall words to an array
          v = Split(SHALL_WORDS, "|")

          ' Replace each shall word with its tagged version
          For I = 0 To UBound(v)
          s = CStr(v(I))
          Set aRange = ActiveDocument.Range

          ' Create the FindText arg, e.g. "(<[Ss]hall>)"
          ' The parentheses create a "group" that we use to build the replacement text
          ' The <> are used to mark the beginning and end of words
          ' to prevent FindText="will" from matching "swill", "goodwill", etc.
          sFindText = "(<" & s & ">)"

          ' Create the ReplaceText arg. "1" is the found text. Wrap it in the tags.
          sReplaceText = START_TAG & "1" & END_TAG
          With aRange.Find
          .MatchWildcards = True
          .Execute FindText:=sFindText, ReplaceWith:=sReplaceText, Replace:=wdReplaceAll
          End With
          Next I
          Set aRange = Nothing
          End Sub



          Sub RemoveTags()
          Dim aRange As Range
          Dim sFindText As String
          Dim sReplaceText As String

          Set aRange = ActiveDocument.Range
          sFindText = START_TAG & "(*)" & END_TAG
          sReplaceText = "1"
          With aRange.Find
          .MatchWildcards = True
          .Execute FindText:=sFindText, ReplaceWith:=sReplaceText, Replace:=wdReplaceAll
          End With
          Set aRange = Nothing
          End Sub



          Sub FindWordCopySentence()
          Dim appExcel As Object
          Dim objSheet As Object
          Dim aRange As Range
          Dim intRowCount As Integer
          Dim s As String
          intRowCount = 1
          Set aRange = ActiveDocument.Range
          With aRange.Find
          .MatchWildcards = True
          Do
          .Text = START_TAG & "*" & END_TAG ' the word I am looking for
          .Execute
          If .Found Then
          aRange.Expand Unit:=wdSentence
          s = aRange.Text
          s = Replace(s, START_TAG, "")
          s = Replace(s, END_TAG, "")
          aRange.Collapse wdCollapseEnd
          If objSheet Is Nothing Then
          Set appExcel = CreateObject("Excel.Application")
          'Change the file path to match the location of your test.xls
          Set objSheet = appExcel.workbooks.Open("C:Temptest.xlsx").Sheets("Sheet1")
          intRowCount = 1
          End If
          objSheet.Cells(intRowCount, 1).Formula = s
          intRowCount = intRowCount + 1
          End If
          Loop While .Found
          End With
          If Not objSheet Is Nothing Then
          appExcel.workbooks(1).Close True
          appExcel.Quit
          Set objSheet = Nothing
          Set appExcel = Nothing
          End If
          Set aRange = Nothing
          End Sub


          Hope that helps






          share|improve this answer
























          • Thanks. In FindWordCopySentence I got a run-time error '6' Overflow on "intRowCount = intRowCount + 1".

            – Shane
            Nov 16 '18 at 20:08











          • Rats. intRowCount is declared as an Integer so it overflows on exceeding 32,767. In my tests the code worked on a sample doc with 2 shalls and 1 will. One possibility (unlikely) is that you have more than 32,767 shalls and wills. If that's the case you can change intRowCount to Long. Another possibility is that for some reason Word's Find is looping through your document over and over. You can investigate by adding a temporary line of code If intRowCount > 10000 Then Exit Do in the do-loop. This should terminate the program before it overflows so you can inspect the Excel file.

            – xidgel
            Nov 16 '18 at 20:42
















          0














          One way to approach this is to:



          (1) Use Word's search/replace to wrap the words of interest (shall, will) with tags, e.g., shall, will. and can be anything you don't expect to be in the Word source document;



          (2) Use a modified version of your FindWordCopySentence to find the tagged words, then copy the corresponding sentences to Excel; then



          (3) Use Word's search/replace to clean-up (remove the tags). Or you could just close the Word doc without saving.



          Here's the code with some comments to explain the details:



          Option Explicit
          Const START_TAG As String = "$$SWSTART_"
          Const END_TAG As String = "_SWEND$$"


          Sub AddTagsToShallWords()
          ' SHALL_WORDS is a |-delimited string of the words you want to replace
          ' The "[Ss]" means that the first letter can be upper or lower case (same for [Ww])
          ' This is designed to be extendible, e.g. you could add "must" by appending |[Mm]ust
          Const SHALL_WORDS = "[Ss]hall|[Ww]ill"
          Dim v As Variant
          Dim I As Long
          Dim s As String
          Dim aRange As Range
          Dim sFindText As String
          Dim sReplaceText As String

          ' Create shall words to an array
          v = Split(SHALL_WORDS, "|")

          ' Replace each shall word with its tagged version
          For I = 0 To UBound(v)
          s = CStr(v(I))
          Set aRange = ActiveDocument.Range

          ' Create the FindText arg, e.g. "(<[Ss]hall>)"
          ' The parentheses create a "group" that we use to build the replacement text
          ' The <> are used to mark the beginning and end of words
          ' to prevent FindText="will" from matching "swill", "goodwill", etc.
          sFindText = "(<" & s & ">)"

          ' Create the ReplaceText arg. "1" is the found text. Wrap it in the tags.
          sReplaceText = START_TAG & "1" & END_TAG
          With aRange.Find
          .MatchWildcards = True
          .Execute FindText:=sFindText, ReplaceWith:=sReplaceText, Replace:=wdReplaceAll
          End With
          Next I
          Set aRange = Nothing
          End Sub



          Sub RemoveTags()
          Dim aRange As Range
          Dim sFindText As String
          Dim sReplaceText As String

          Set aRange = ActiveDocument.Range
          sFindText = START_TAG & "(*)" & END_TAG
          sReplaceText = "1"
          With aRange.Find
          .MatchWildcards = True
          .Execute FindText:=sFindText, ReplaceWith:=sReplaceText, Replace:=wdReplaceAll
          End With
          Set aRange = Nothing
          End Sub



          Sub FindWordCopySentence()
          Dim appExcel As Object
          Dim objSheet As Object
          Dim aRange As Range
          Dim intRowCount As Integer
          Dim s As String
          intRowCount = 1
          Set aRange = ActiveDocument.Range
          With aRange.Find
          .MatchWildcards = True
          Do
          .Text = START_TAG & "*" & END_TAG ' the word I am looking for
          .Execute
          If .Found Then
          aRange.Expand Unit:=wdSentence
          s = aRange.Text
          s = Replace(s, START_TAG, "")
          s = Replace(s, END_TAG, "")
          aRange.Collapse wdCollapseEnd
          If objSheet Is Nothing Then
          Set appExcel = CreateObject("Excel.Application")
          'Change the file path to match the location of your test.xls
          Set objSheet = appExcel.workbooks.Open("C:Temptest.xlsx").Sheets("Sheet1")
          intRowCount = 1
          End If
          objSheet.Cells(intRowCount, 1).Formula = s
          intRowCount = intRowCount + 1
          End If
          Loop While .Found
          End With
          If Not objSheet Is Nothing Then
          appExcel.workbooks(1).Close True
          appExcel.Quit
          Set objSheet = Nothing
          Set appExcel = Nothing
          End If
          Set aRange = Nothing
          End Sub


          Hope that helps






          share|improve this answer
























          • Thanks. In FindWordCopySentence I got a run-time error '6' Overflow on "intRowCount = intRowCount + 1".

            – Shane
            Nov 16 '18 at 20:08











          • Rats. intRowCount is declared as an Integer so it overflows on exceeding 32,767. In my tests the code worked on a sample doc with 2 shalls and 1 will. One possibility (unlikely) is that you have more than 32,767 shalls and wills. If that's the case you can change intRowCount to Long. Another possibility is that for some reason Word's Find is looping through your document over and over. You can investigate by adding a temporary line of code If intRowCount > 10000 Then Exit Do in the do-loop. This should terminate the program before it overflows so you can inspect the Excel file.

            – xidgel
            Nov 16 '18 at 20:42














          0












          0








          0







          One way to approach this is to:



          (1) Use Word's search/replace to wrap the words of interest (shall, will) with tags, e.g., shall, will. and can be anything you don't expect to be in the Word source document;



          (2) Use a modified version of your FindWordCopySentence to find the tagged words, then copy the corresponding sentences to Excel; then



          (3) Use Word's search/replace to clean-up (remove the tags). Or you could just close the Word doc without saving.



          Here's the code with some comments to explain the details:



          Option Explicit
          Const START_TAG As String = "$$SWSTART_"
          Const END_TAG As String = "_SWEND$$"


          Sub AddTagsToShallWords()
          ' SHALL_WORDS is a |-delimited string of the words you want to replace
          ' The "[Ss]" means that the first letter can be upper or lower case (same for [Ww])
          ' This is designed to be extendible, e.g. you could add "must" by appending |[Mm]ust
          Const SHALL_WORDS = "[Ss]hall|[Ww]ill"
          Dim v As Variant
          Dim I As Long
          Dim s As String
          Dim aRange As Range
          Dim sFindText As String
          Dim sReplaceText As String

          ' Create shall words to an array
          v = Split(SHALL_WORDS, "|")

          ' Replace each shall word with its tagged version
          For I = 0 To UBound(v)
          s = CStr(v(I))
          Set aRange = ActiveDocument.Range

          ' Create the FindText arg, e.g. "(<[Ss]hall>)"
          ' The parentheses create a "group" that we use to build the replacement text
          ' The <> are used to mark the beginning and end of words
          ' to prevent FindText="will" from matching "swill", "goodwill", etc.
          sFindText = "(<" & s & ">)"

          ' Create the ReplaceText arg. "1" is the found text. Wrap it in the tags.
          sReplaceText = START_TAG & "1" & END_TAG
          With aRange.Find
          .MatchWildcards = True
          .Execute FindText:=sFindText, ReplaceWith:=sReplaceText, Replace:=wdReplaceAll
          End With
          Next I
          Set aRange = Nothing
          End Sub



          Sub RemoveTags()
          Dim aRange As Range
          Dim sFindText As String
          Dim sReplaceText As String

          Set aRange = ActiveDocument.Range
          sFindText = START_TAG & "(*)" & END_TAG
          sReplaceText = "1"
          With aRange.Find
          .MatchWildcards = True
          .Execute FindText:=sFindText, ReplaceWith:=sReplaceText, Replace:=wdReplaceAll
          End With
          Set aRange = Nothing
          End Sub



          Sub FindWordCopySentence()
          Dim appExcel As Object
          Dim objSheet As Object
          Dim aRange As Range
          Dim intRowCount As Integer
          Dim s As String
          intRowCount = 1
          Set aRange = ActiveDocument.Range
          With aRange.Find
          .MatchWildcards = True
          Do
          .Text = START_TAG & "*" & END_TAG ' the word I am looking for
          .Execute
          If .Found Then
          aRange.Expand Unit:=wdSentence
          s = aRange.Text
          s = Replace(s, START_TAG, "")
          s = Replace(s, END_TAG, "")
          aRange.Collapse wdCollapseEnd
          If objSheet Is Nothing Then
          Set appExcel = CreateObject("Excel.Application")
          'Change the file path to match the location of your test.xls
          Set objSheet = appExcel.workbooks.Open("C:Temptest.xlsx").Sheets("Sheet1")
          intRowCount = 1
          End If
          objSheet.Cells(intRowCount, 1).Formula = s
          intRowCount = intRowCount + 1
          End If
          Loop While .Found
          End With
          If Not objSheet Is Nothing Then
          appExcel.workbooks(1).Close True
          appExcel.Quit
          Set objSheet = Nothing
          Set appExcel = Nothing
          End If
          Set aRange = Nothing
          End Sub


          Hope that helps






          share|improve this answer













          One way to approach this is to:



          (1) Use Word's search/replace to wrap the words of interest (shall, will) with tags, e.g., shall, will. and can be anything you don't expect to be in the Word source document;



          (2) Use a modified version of your FindWordCopySentence to find the tagged words, then copy the corresponding sentences to Excel; then



          (3) Use Word's search/replace to clean-up (remove the tags). Or you could just close the Word doc without saving.



          Here's the code with some comments to explain the details:



          Option Explicit
          Const START_TAG As String = "$$SWSTART_"
          Const END_TAG As String = "_SWEND$$"


          Sub AddTagsToShallWords()
          ' SHALL_WORDS is a |-delimited string of the words you want to replace
          ' The "[Ss]" means that the first letter can be upper or lower case (same for [Ww])
          ' This is designed to be extendible, e.g. you could add "must" by appending |[Mm]ust
          Const SHALL_WORDS = "[Ss]hall|[Ww]ill"
          Dim v As Variant
          Dim I As Long
          Dim s As String
          Dim aRange As Range
          Dim sFindText As String
          Dim sReplaceText As String

          ' Create shall words to an array
          v = Split(SHALL_WORDS, "|")

          ' Replace each shall word with its tagged version
          For I = 0 To UBound(v)
          s = CStr(v(I))
          Set aRange = ActiveDocument.Range

          ' Create the FindText arg, e.g. "(<[Ss]hall>)"
          ' The parentheses create a "group" that we use to build the replacement text
          ' The <> are used to mark the beginning and end of words
          ' to prevent FindText="will" from matching "swill", "goodwill", etc.
          sFindText = "(<" & s & ">)"

          ' Create the ReplaceText arg. "1" is the found text. Wrap it in the tags.
          sReplaceText = START_TAG & "1" & END_TAG
          With aRange.Find
          .MatchWildcards = True
          .Execute FindText:=sFindText, ReplaceWith:=sReplaceText, Replace:=wdReplaceAll
          End With
          Next I
          Set aRange = Nothing
          End Sub



          Sub RemoveTags()
          Dim aRange As Range
          Dim sFindText As String
          Dim sReplaceText As String

          Set aRange = ActiveDocument.Range
          sFindText = START_TAG & "(*)" & END_TAG
          sReplaceText = "1"
          With aRange.Find
          .MatchWildcards = True
          .Execute FindText:=sFindText, ReplaceWith:=sReplaceText, Replace:=wdReplaceAll
          End With
          Set aRange = Nothing
          End Sub



          Sub FindWordCopySentence()
          Dim appExcel As Object
          Dim objSheet As Object
          Dim aRange As Range
          Dim intRowCount As Integer
          Dim s As String
          intRowCount = 1
          Set aRange = ActiveDocument.Range
          With aRange.Find
          .MatchWildcards = True
          Do
          .Text = START_TAG & "*" & END_TAG ' the word I am looking for
          .Execute
          If .Found Then
          aRange.Expand Unit:=wdSentence
          s = aRange.Text
          s = Replace(s, START_TAG, "")
          s = Replace(s, END_TAG, "")
          aRange.Collapse wdCollapseEnd
          If objSheet Is Nothing Then
          Set appExcel = CreateObject("Excel.Application")
          'Change the file path to match the location of your test.xls
          Set objSheet = appExcel.workbooks.Open("C:Temptest.xlsx").Sheets("Sheet1")
          intRowCount = 1
          End If
          objSheet.Cells(intRowCount, 1).Formula = s
          intRowCount = intRowCount + 1
          End If
          Loop While .Found
          End With
          If Not objSheet Is Nothing Then
          appExcel.workbooks(1).Close True
          appExcel.Quit
          Set objSheet = Nothing
          Set appExcel = Nothing
          End If
          Set aRange = Nothing
          End Sub


          Hope that helps







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 18:44









          xidgelxidgel

          2,3172717




          2,3172717













          • Thanks. In FindWordCopySentence I got a run-time error '6' Overflow on "intRowCount = intRowCount + 1".

            – Shane
            Nov 16 '18 at 20:08











          • Rats. intRowCount is declared as an Integer so it overflows on exceeding 32,767. In my tests the code worked on a sample doc with 2 shalls and 1 will. One possibility (unlikely) is that you have more than 32,767 shalls and wills. If that's the case you can change intRowCount to Long. Another possibility is that for some reason Word's Find is looping through your document over and over. You can investigate by adding a temporary line of code If intRowCount > 10000 Then Exit Do in the do-loop. This should terminate the program before it overflows so you can inspect the Excel file.

            – xidgel
            Nov 16 '18 at 20:42



















          • Thanks. In FindWordCopySentence I got a run-time error '6' Overflow on "intRowCount = intRowCount + 1".

            – Shane
            Nov 16 '18 at 20:08











          • Rats. intRowCount is declared as an Integer so it overflows on exceeding 32,767. In my tests the code worked on a sample doc with 2 shalls and 1 will. One possibility (unlikely) is that you have more than 32,767 shalls and wills. If that's the case you can change intRowCount to Long. Another possibility is that for some reason Word's Find is looping through your document over and over. You can investigate by adding a temporary line of code If intRowCount > 10000 Then Exit Do in the do-loop. This should terminate the program before it overflows so you can inspect the Excel file.

            – xidgel
            Nov 16 '18 at 20:42

















          Thanks. In FindWordCopySentence I got a run-time error '6' Overflow on "intRowCount = intRowCount + 1".

          – Shane
          Nov 16 '18 at 20:08





          Thanks. In FindWordCopySentence I got a run-time error '6' Overflow on "intRowCount = intRowCount + 1".

          – Shane
          Nov 16 '18 at 20:08













          Rats. intRowCount is declared as an Integer so it overflows on exceeding 32,767. In my tests the code worked on a sample doc with 2 shalls and 1 will. One possibility (unlikely) is that you have more than 32,767 shalls and wills. If that's the case you can change intRowCount to Long. Another possibility is that for some reason Word's Find is looping through your document over and over. You can investigate by adding a temporary line of code If intRowCount > 10000 Then Exit Do in the do-loop. This should terminate the program before it overflows so you can inspect the Excel file.

          – xidgel
          Nov 16 '18 at 20:42





          Rats. intRowCount is declared as an Integer so it overflows on exceeding 32,767. In my tests the code worked on a sample doc with 2 shalls and 1 will. One possibility (unlikely) is that you have more than 32,767 shalls and wills. If that's the case you can change intRowCount to Long. Another possibility is that for some reason Word's Find is looping through your document over and over. You can investigate by adding a temporary line of code If intRowCount > 10000 Then Exit Do in the do-loop. This should terminate the program before it overflows so you can inspect the Excel file.

          – xidgel
          Nov 16 '18 at 20:42


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322875%2fword-macro-find-key-words-and-extract-sentence-to-excel%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python