Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-05-2016, 03:57 PM
nancy nancy is offline Selecting certain text in word Windows XP Selecting certain text in word Office 2013
Novice
Selecting certain text in word
 
Join Date: Apr 2016
Posts: 3
nancy is on a distinguished road
Default 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!
Reply With Quote
  #2  
Old 04-05-2016, 07:21 PM
nancy nancy is offline Selecting certain text in word Windows XP Selecting certain text in word Office 2013
Novice
Selecting certain text in word
 
Join Date: Apr 2016
Posts: 3
nancy is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 04-05-2016, 07:47 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: 21,956
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

Quote:
Originally Posted by nancy View Post
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.
This is really confusing. A paragraph break is what is created by pressing the Enter key, so I don't understand why you're differentiating between 'paragraph symbol' and 'enter'.

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]
Reply With Quote
  #4  
Old 04-06-2016, 06:30 AM
nancy nancy is offline Selecting certain text in word Windows XP Selecting certain text in word Office 2013
Novice
Selecting certain text in word
 
Join Date: Apr 2016
Posts: 3
nancy is on a distinguished road
Default

Sure! Here is a sample. There is text that follows, which I have deleted.
Attached Files
File Type: docx sample.docx (25.5 KB, 11 views)
Reply With Quote
  #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: 21,956
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
Reply

Thread Tools
Display Modes


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 12:03 PM.


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