#1
|
|||
|
|||
Selecting certain text in word
Hi!
The top of my word document looks like this: CONFIDENTIAL Memorandum to Johnny Damon cc Holly Thompson Tony Bennett From I have a bunch of similar word documents in the same folder. The formatting is: The CONFIDENTIAL is followed by a paragraph symbol, Memorandum to is followed by enter, Johnny Damon is followed by a paragraph symbol, cc is followed by enter, next how ever many names followed by paragraph symbols, then one more paragraph and From is followed by enter. I want the macro to select Johnny Damon and a separate command to select all the cc names, one by one and then I'm copying and pasting into another table. I'm fine with the copying and pasting and looping, but I don't know how to get my macro to know what to select. I tried a few things, like ActiveDocument.Sentences(3).Copy which gives me cc name I tried ActiveDocument.Paragraphs(2).Range.Copy and got Memorandum to Johnny Damon I'm running through a bunch of documents on a loop. The Johnny Damon will always be in the same spot, but again, there could be a different number of cc: and I want to pick them up one at a time and stop when there are no more. Thanks! |
#2
|
|||
|
|||
Basically, I don't know all the VBA on how to select certain spots in a word document, so even just a run down of some of the basic types of commands to get to different spots in a word doc, I can make it work. In researching, I'm seeing a lot of the Find coding, which might be what I end up doing, but I'm not familiar with it.
|
#3
|
||||
|
||||
Quote:
Without actually seeing the problem document, it can be difficult for anyone to develop a solution. Can you attach a document to a post with some representative data (delete/obfuscate anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
Sure! Here is a sample. There is text that follows, which I have deleted.
|
#5
|
||||
|
||||
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] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 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 |