Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. Sub extractUnderlinedWords()
  2. Dim thisDoc As Word.Document
  3. Application.ScreenUpdating = False
  4. Dim appExcel As Object, oxlWbk As Object
  5. Dim FN As String
  6. Dim aRange As Range
  7. Dim intRowCount As Integer
  8. intRowCount = 1
  9.  
  10. Set thisDoc = ActiveDocument
  11. Set aRange = thisDoc.Range
  12.  
  13. Set appExcel = CreateObject("Excel.application")
  14. FN = "C:Users[blah]UnderlinedWords.xlsx"
  15.  
  16. If FileExists(FN) Then
  17. Set oxlWbk = appExcel.workbooks.Open(fileName:=FN).Sheets("Sheet1")
  18. End If
  19.  
  20. With aRange.Find
  21. Do
  22. .Font.Underline = True
  23. .Execute
  24. If .Found Then
  25. ' aRange.Expand Unit:=wdSentence
  26. ' aRange.Select
  27. If Len(aRange) > 1 Then
  28. If Not aRange.InRange(thisDoc.TablesOfContents(1).Range) Then
  29. aRange.MoveEndWhile cset:=Chr(13), Count:=wdBackward
  30. ' aRange.Copy
  31. oxlWbk.Cells(intRowCount, 1).Value = aRange.Text
  32. aRange.Collapse wdCollapseEnd
  33. Debug.Print "Page: " & aRange.Information(wdActiveEndAdjustedPageNumber)
  34. ' aRange.Select
  35. If oxlWbk Is Nothing Then
  36. intRowCount = 1
  37. End If
  38. 'oxlWbk.Cells(intRowCount, 1).Value = aRange.Text
  39. ' oxlWbk.Paste
  40. intRowCount = intRowCount + 1
  41. End If
  42. End If
  43. End If
  44. Loop While .Found
  45. End With
  46. If Not oxlWbk Is Nothing Then
  47. appExcel.workbooks(1).Close True
  48. appExcel.Quit
  49. Set oxlWbk = Nothing
  50. Set appExcel = Nothing
  51. End If
  52. Set aRange = Nothing
  53. Application.ScreenUpdating = True
  54. MsgBox ("Done!")
  55. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement