Guest User

Untitled

a guest
Jan 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. Sub FindMe()
  2.  
  3. Dim fle As Range
  4. Dim i As Long
  5. Dim k As Long
  6. Dim line As Long
  7. Dim strline As String
  8. Dim strsearch As String
  9. Dim loc As Range
  10. Dim sht As Worksheet
  11. Dim lngPos As Long
  12. Dim lngCount As Long
  13. Dim wdApp As Object
  14. Dim wdDoc As Object
  15. Dim oRng As Object
  16. Dim osld As Object
  17. Dim oshp As Object
  18. Dim pptApp As Object
  19. Dim pptdoc As Object
  20.  
  21. Sheet3.Range("A4:B999999").ClearContents
  22. Application.ScreenUpdating = False
  23. Application.DisplayAlerts = False
  24. If Not IsEmpty(Sheet3.Range("B1").Value) Then
  25. strsearch = Sheet3.Range("B1").Value
  26. i = 0
  27. k = 4
  28. lngCount = 0
  29. For Each fle In Sheet2.Range("A:A")
  30. If InStr(1, fle.Value, ".txt") > 0 Then '.txt extension
  31. Open fle.Value For Input As #1
  32. Do While Not EOF(1)
  33. Line Input #1, strline
  34. lngPos = 1
  35. Do
  36. lngPos = InStr(lngPos, strline, strsearch, vbTextCompare)
  37. If lngPos > 0 Then
  38. lngCount = lngCount + 1
  39. lngPos = lngPos + Len(strsearch)
  40. End If
  41. Loop Until lngPos = 0
  42. Loop
  43. If lngCount <> 0 Then
  44. Sheet3.Cells(k, 1).Value = lngCount
  45. Sheet3.Cells(k, 2).Value = fle.Value
  46. k = k + 1
  47. lngCount = 0
  48. End If
  49. Close #1
  50.  
  51. ElseIf InStr(1, fle.Value, ".xls") > 0 Or InStr(1, fle.Value, ".csv") Then '.xls, .xlsx, .xlsm, .csv extentions
  52. Workbooks.Open Filename:=fle.Value, ReadOnly:=True, UpdateLinks:=False
  53. For Each sht In ActiveWorkbook.Worksheets
  54. With sht
  55. Set loc = .Cells.Find(What:=strsearch)
  56. If Not loc Is Nothing Then
  57. FirstAddress = loc.Address
  58. Do
  59. i = i + 1
  60. Set loc = .Cells.FindNext(loc)
  61. Loop While Not loc Is Nothing And loc.Address <> FirstAddress
  62. End If
  63. End With
  64. Next sht
  65. ActiveWorkbook.Close False
  66. If i <> 0 Then
  67. Sheet3.Cells(k, 1).Value = i
  68. Sheet3.Cells(k, 2).Value = fle.Value
  69. k = k + 1
  70. i = 0
  71. End If
  72.  
  73. ElseIf InStr(1, fle.Value, ".doc") > 0 Or InStr(1, fle.Value, ".pdf") > 0 Then '.doc, .docx extentions
  74. Set wdApp = CreateObject("word.Application")
  75. Set wdDoc = wdApp.documents.Open(fle.Value, ReadOnly:=True)
  76. Set oRng = wdDoc.Range
  77. With oRng.Find
  78. Do While .Execute(FindText:=strsearch, MatchCase:=False)
  79. i = i + 1
  80. Loop
  81. End With
  82. wdDoc.Close 0
  83. Set oRng = Nothing
  84. Set wdDoc = Nothing
  85. Set wdApp = Nothing
  86. If i <> 0 Then
  87. Sheet3.Cells(k, 1).Value = i
  88. Sheet3.Cells(k, 2).Value = fle.Value
  89. k = k + 1
  90. i = 0
  91. End If
  92.  
  93. ElseIf InStr(1, fle.Value, ".ppt") > 0 Then '.ppt, .pptx, .pptm extentions
  94. Set pptApp = CreateObject("powerpoint.Application")
  95. Set pptdoc = pptApp.presentations.Open(fle.Value, ReadOnly:=True)
  96. For Each osld In pptdoc.slides
  97. For Each oshp In osld.Shapes
  98. If oshp.HasTextFrame Then
  99. If oshp.TextFrame.HasText Then
  100. Set otext = oshp.TextFrame.TextRange
  101. Set foundText = otext.Find(findwhat:=strsearch)
  102. Do While Not (foundText Is Nothing)
  103. lngCount = lngCount + 1
  104. With foundText
  105. Set foundText = otext.Find(findwhat:=strsearch, After:=.Start + .Length - 1)
  106. End With
  107. Loop
  108. End If
  109. End If
  110. Next oshp
  111. Next osld
  112. pptdoc.Close
  113. Set pptdoc = Nothing
  114. Set pptApp = Nothing
  115. Set otext = Nothing
  116. Set foundText = Nothing
  117. If lngCount <> 0 Then
  118. Sheet3.Cells(k, 1).Value = lngCount
  119. Sheet3.Cells(k, 2).Value = fle.Value
  120. k = k + 1
  121. lngCount = 0
  122. End If
  123.  
  124. End If
  125. Next fle
  126. Else:
  127. MsgBox "Enter text in cell 'B1' before searching."
  128. End If
  129. Application.DisplayAlerts = True
  130. Application.ScreenUpdating = True
  131.  
  132. End Sub
Add Comment
Please, Sign In to add comment