Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #5  
Old 04-06-2016, 04:33 PM
macropod's Avatar
macropod macropod is offline Selecting certain text in word Windows 7 64bit Selecting certain text in word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try adding the following macro to the document you want the output to go to. It includes a folder browser, so all you need do is select the folder to process and a table containing the 'to' & 'cc' data will be created at the end of your document.
Code:
Sub HarvestDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
Dim StrTxt As String, Rng As Range
Set Rng = ActiveDocument.Range.Characters.Last
strDocNm = ActiveDocument.FullName
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With ActiveDocument 'wdDoc
      With .Range
        With .Find
          .ClearFormatting
          .Text = "Memorandum to^l*From^l"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindContinue
          .Format = False
          .MatchWildcards = True
          .Execute
        End With
        If .Find.Found = True Then
          StrTxt = .Text
          Do While InStr(StrTxt, vbCr & vbCr) > 0
            StrTxt = Replace(StrTxt, vbCr & vbCr, vbCr)
          Loop
          StrTxt = Replace(Replace(.Text, "Memorandum to" & Chr(11), ""), vbCr & "From" & Chr(11), "")
          StrTxt = Replace(Replace(StrTxt, vbCr, vbTab), "cc" & Chr(11), "")
          Do While Right(StrTxt, 1) = vbTab
            StrTxt = Left(StrTxt, Len(StrTxt) - 1)
          Loop
          Rng.InsertAfter StrTxt & vbCr
        End If
      End With
      .Close SaveChanges:=True
    End With
  End If
  strFile = Dir()
Wend
Rng.ConvertToTable vbTab
Set wdDoc = Nothing: Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting certain text in word Word macro for selecting text and putting it in footnotes mdhg Word VBA 20 03-06-2024 08:07 AM
Selecting and moving text boxes identified by specific text. Chayes Word VBA 8 02-22-2016 07:01 AM
Selecting certain text in word Selecting text between two key phrases Chayes Word VBA 6 06-24-2012 06:54 PM
Selecting styled text Caroline Word 5 02-15-2011 12:55 PM
Selecting a Text Box gajesh Word 0 09-02-2009 11:45 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:35 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft