Results 1 to 1 of 1

Excel Use Word to Find Text and return the following 10 characters

Threaded View

  1. #1
    Registered User
    Join Date
    05-07-2013
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    2

    Excel Use Word to Find Text and return the following 10 characters

    Hi

    I have a Macro which will open a word document, find some text and then replace it.

    I would like to change it so that it finds the text and then simply record that text with the next 10 characters in a spreadsheet.

    I want to find all 0845 numbers in word documents on a network drive and find out what the rest of the telephone number is.

    This is my current code which is performing the find text count and then only making changes to the document if the text is found.
    Sub Open_Word_Document()
    
    'Opens a Word Document from Excel
    
    'set the variables
    Dim ws As Worksheet
    Dim objWord As Object
    Dim New_Folder As String
    Dim wdDoc As Object
    Dim y As Integer
    Dim n As Integer
    Dim Doc_Count As Integer
    Dim Filepath As String
    Const wdReplaceAll = 2 ' replace all won't work without this
    Dim from_text As String, to_text As String
    
    ' define the current worksheet
    Set ws = ActiveWorkbook.Sheets("Filepaths")
    ' define variables
    New_Folder = Range("New_Folder")
    from_text = Range("Text_Find")
    to_text = Range("Text_Replace")
    Doc_Count = Range("Doc_Count")
    
    ' open word
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    
    'start rountine and open the word document
    For n = 1 To Doc_Count
    Filepath = ws.Cells(1 + n, 2) ' filepath of the document we want to update
    Set wdDoc = objWord.Documents.Open(Filepath) ' open the word document
    
    ' remove editing restrictions and flag if document is password protected
    On Error Resume Next
        wdDoc.Unprotect  ' remove editing restrictions
            If Err > 0 Then ' loop through in case the document isn't protected
            Else
                wdDoc.Unprotect
            End If
            
        If Err = 5485 Then ' document is password protected
            ws.Cells(n + 1, 3) = "Can't change as document is password protected"
        Else ' perform the count of the find text - could have this as a separate routine
            y = 0 ' reset y to zero & perform word count
                    
            ' search document
            With wdDoc.Content.Find
                Do While .Execute(FindText:=from_text, Forward:=True, Format:=True, _
                    MatchWholeWord:=True) = True
                y = y + 1
                Loop
            End With
            
            ' search text boxes
            For Each myStoryRange In wdDoc.StoryRanges
                    With myStoryRange.Find
                        Do While .Execute(FindText:=from_text, Forward:=True, Format:=True, _
                            MatchWholeWord:=True) = True
                        y = y + 1
                        Loop
                    End With
                Do While Not (myStoryRange.NextStoryRange Is Nothing)
                    Set myStoryRange = myStoryRange.NextStoryRange
                    With myStoryRange.Find
                        Do While .Execute(FindText:=from_text, Forward:=True, Format:=True, _
                            MatchWholeWord:=True) = True
                        y = y + 1
                        Loop
                    End With
                Loop
            Next myStoryRange
    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer
    Last edited by 6StringJazzer; 11-11-2015 at 11:04 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Help with macro to find a specific word and replace the entire cell with the return word.
    By BG1983 in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 08-24-2015, 09:40 PM
  2. Using VBA to find a word in a line of text then return data to the right of it
    By DAN_AO in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-02-2015, 04:29 AM
  3. Replies: 3
    Last Post: 03-11-2015, 03:25 PM
  4. [SOLVED] Find word in text string and return word adjacent to the left
    By eadamquinn in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-06-2014, 03:22 PM
  5. Need to find a word in text and return corresponding value
    By yafirst in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-04-2014, 11:52 AM
  6. Limit characters in a word/text box in excel
    By Alex Martinez in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-29-2005, 01:05 AM
  7. [SOLVED] Find/Replace carriage return & line feed characters in Excel.
    By Mary Cullen in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-04-2005, 04:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1