Search for a string in word document using VBA

Hi,
I am new to VBA. My requirement is that VBA code should find the path in the word document., some of the paths are present in the word document.
search criteria should start with \\\\ and end with .doc. Could anyone please help me out on how to provide the search criteria and is there any predefined methods present.
Thanks in advance.
Thanks,.

Apologies, the line
Do While .Execute(FindText:=.Text = "\\\\*.doc", MatchWildCards:=True)
should read
Do While .Execute(FindText:="\\\\*.doc", MatchWildCards:=True)
It will however only work for the document body. If the string is in a different story range you will have to loop through those story ranges e.g.
Sub FindIt()
Dim wdApp As Object
Dim wdDoc As Object
Dim oRng As Object
Dim bStarted As Boolean
Dim i As Integer
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If Err Then
        Set wdApp = CreateObject("Word.Application")
        bStarted = True
    End If
    On Error GoTo 0
    ActiveWorkbook.Save
    Set wdDoc = wdApp.Documents.Open("C:\Path\DocumentName.docx")
    i = 1
    For Each oRng In wdDoc.StoryRanges
        With oRng.Find
            .MatchWildcards = True
            Do While .Execute(FindText:="\\\\*.doc")
                Range("A" & i) = oRng.Text
                oRng.Collapse 0
                i = i + 1
            Loop
        End With
        If oRng.StoryType <> wdMainTextStory Then
            While Not (oRng.NextStoryRange Is Nothing)
                Set oRng = oRng.NextStoryRange
                With oRng.Find
                    .MatchWildcards = True
                    Do While .Execute(FindText:="\\\\*.doc")
                        Range("A" & i) = oRng.Text
                        oRng.Collapse 0
                        i = i + 1
                    Loop
                End With
            Wend
        End If
    Next oRng
    wdDoc.Close 0
    If bStarted Then wdApp.Quit
    Set oRng = Nothing
    Set wdDoc = Nothing
    Set wdApp = Nothing
End Sub
Graham Mayor - Word MVP
www.gmayor.com

Similar Messages

  • Search for a String within a document (Word, txt, doc) using JSP, JAVA

    Hi
    I have created a little application that uses combination of JSP and HTML. Users of this application can upload documents which are then stored on the server. I need to develop functionality where I can allows users to search for a string within a document. More precisely, user would type in some string in a text box and application will search all uploaded documents for that string and return the downloadable links to those documents that contains that string. I have never done this before. I was wondering if someone could get me started on this or point me to some thread where this idea is already discussed. Any Jave code exists for searching through documents??
    Thanks for your help
    Riz

    http://www.ibm.com/developerworks/java/library/j-text-searching.html
    http://en.wikipedia.org/wiki/Full_text_search
    Type these parameter in yahoo:+efficient text search
    you will need something like openoffice library to read microsoft word document.

  • How to Search for text in a word document and replace it

    Hey All ,
    I am able to open up a word document present locally using OLE2 Automation. Now i need to search for a text in that document
    and replace it with another text. How do i achieve this?
    And another issue is when i search for the particular pattern i need to store the pattern that comes after the pattern as i need it for further use before i replace it.
    Thanks And Warm Regards,
    Nischal

    Hey Chen,
    Well before i do the search and edit , I am actually opening a .mts file in word which is happening successfully.
    The issue that i am facing now is I am unable to save it as .doc file through the 'SaveAs' method.
    Here is my code. Can u please tell me what i need to do in order to correct this.
    After i do this conversion i intend to pass the data from the .doc file to an internal table and then do the required change.
    But First i need to save it to .doc type.
    REPORT  MAIN_PROGRAM_2.
    INCLUDE OLE2INCL.
    *Declaration Of OLE2_Object Variables.
    DATA: WORD            TYPE OLE2_OBJECT,
          DOCUMENTS       TYPE OLE2_OBJECT,
          ACT_DOCUMENTS   TYPE OLE2_OBJECT.
    *Declaration of variables
    PARAMETERS PATH       TYPE string.
    DATA:Relative_Path    TYPE string,
         Relative_Path_Save    TYPE string.
    *Declaration of an internal table
    CONCATENATE PATH '\Script.mts' INTO RELATIVE_PATH.
    *Object Creation in order for us to open word
    CREATE OBJECT WORD 'WORD.APPLICATION'.
    PERFORM ERROR_HANDLE.
    *Set visibility property of MS word.
    SET property of WORD 'VISIBLE' = 1.
    *Open A document
    CALL METHOD OF WORD 'Documents' = DOCUMENTS.
    PERFORM ERROR_HANDLE.
    *CALL METHOD OF DOCUMENTS 'ADD' = ACT_DOCUMENTS.
    CALL METHOD OF DOCUMENTS 'OPEN' EXPORTING #1 = RELATIVE_PATH.
    PERFORM ERROR_HANDLE.
    *GET PROPERTY OF WORD 'ActiveDocument' = ACT_DOCUMENTS.
    *PERFORM ERROR_HANDLE.
    **To save the script.mts to script .doc file in order to put it into an internal table
    CONCATENATE PATH '\Script.doc' INTO RELATIVE_PATH_SAVE.
    CALL METHOD OF WORD 'SaveAs' EXPORTING #1 = RELATIVE_PATH_SAVE.
    PERFORM ERROR_HANDLE.
    FREE OBJECT WORD.
    The Save part of the code is giving me an OLE error = 2
    Thanks And Kind Regards,
    Nischal
    Edited by: Nischal HP on Apr 28, 2011 1:53 PM

  • Is Safari Find function flexible? (Search for inner strings of characters?)

    I'm running 10.6.8 with Safari Version 5.1.7 (6534.57.2). I see that when you search a web page using the Safari Find function, you can only search for the beginnings of words, not for character strings within the words. Is it possible to expand the Find function's capability to search for inner strings of characters?

    use command F or the edit menus to make the page search box visible - click the magnify/arrow icon at the left side of it - choose "contains" or "starts with".

  • How do i search for a string in a txt file using java??

    How do i search for a string in a txt file using java??
    could you please help thanks
    J

    Regular expressinos work just fine, especially when
    searching for patterns. But they seem to be impying
    it's a specific group of characters they're looking
    for, and indexOf() is much faster than a regex.If he's reading from a file, the I/O time will likely swamp any performance hit that regex introduces. I think contains() (or indexOf() if he's not on 5.0 yet) is preferable to regex just because it's simpler. (And in the case of contains(), the name makes for a very clear, direct mapping between your intent and the code that realizes it.)

  • HI I'm having real trouble opening my word documents (using Office:Mac 2008)  Have tried repair disk options as I saw this advice on another blog, seemed to work for a while now its stared happening again.  Any help MUCH appreciated

    HI I'm having real trouble opening my word documents (using Office:Mac 2008)  Have tried repair disk options as I saw this advice on another blog, seemed to work for a while now its stared happening again.  Any help MUCH appreciated

    http://answers.microsoft.com/en-us/mac

  • How to continously read for a string or word in a file?

    Hi
    I have been trying for a while now to search for a word that is being written to a file.
    I was wondering if anyone can get me over this hump?
    What I am doing is reading from the serial port to a file (example file called test), and while this is being done I want to search for the word "passed" or the phrase "test passed"
    Once I fine this word or phrase, I can stop the test
    I cannot figure out how to search the file, I used the scan file VI and the search/ replace VI etc with no luck!
    Your suggestions is appreciated

    It's always best to post your code so others can see the work you've done.  Suggestions will be more effective that way.
    So your reading from a serial port to a file?  What do you mean by this?  Are you reading from the serial port and writing the data to a file?  It's the file you want to read, isn't it?  If I had your code I could answer my own questions  
    Why don't you just monitor the incoming data stream from the serial port for "passed."
    But to read from a file and search for a string, Smercurio is correct.  Match Pattern will do what you need (You can use this same function for ASCII serial data as well).
    Reese, (former CLAD, future CLD)
    Some people call me the Space Cowboy!
    Some call me the gangster of love.
    Some people call me MoReese!
    ...I'm right here baby, right here, right here, right here at home

  • I have a MacBook Pro and can't print pdf files or word documents using my HP Wireless Photosmart Printer.  It will print files from the internet.  Any suggestions or ideas will be greatly appreciated.  Thank you.

    I have a MacBook Pro and can't print pdf files or word documents using my HP Wireless Photosmart Printer.  It will print files from the internet.  Any suggestions or ideas will be greatly appreciated.  Thank you.

    Hello, SoonerAnesthetist. 
    Thank you for visiting Apple Support Communities.
    Here is an article that I would recommend going through when experiencing this issue.
    OS X Mavericks: Solve printing problems
    http://support.apple.com/kb/PH14142
    Cheers,
    Jason H.

  • How can I search for a string like a partial IP address in the global search

    When searching for a string like "10.255." the global search finds messages containing "255" or "10" and both.
    This happens no matter whether quotation is used or not.
    When quotation is used it probably will match on the given order as well, but beeing in the timezone '+0100' the string "10" is always found first (in the email headers...).
    Is there a way to tell the global search not to use '.' or any other character as whitespace?

    When searching for a string like "10.255." the global search finds messages containing "255" or "10" and both.
    This happens no matter whether quotation is used or not.
    When quotation is used it probably will match on the given order as well, but beeing in the timezone '+0100' the string "10" is always found first (in the email headers...).
    Is there a way to tell the global search not to use '.' or any other character as whitespace?

  • How to search for a string in ALL Function Modules source code

    Hello,
    I want to search for the string "HELLO" in all FUNCTION MODULES source code(FM, no reports/programs).
    Is this possible? How?
    Cheers,
    Andy

    hi,
    Execute RPR_ABAP_SOURCE_SCAN to search for a string in the code ... Press where-used-list button on the program which takes to the function group or the function module where it is used ..
    Regards,
    Santosh

  • Writing to word document using java

    hi,
    i want to retrieve the values from database and then write to .doc file. how can i write to a word document using java?
    thanks in advance

    Google for "java write to word file"
    and behold the magic results

  • Search for a String in a list of files

    Could anyone please send me a java sample code which searches for a string from a list of files and returns me the filename and the line in which the Search String was found ?
    Thanks in advance
    RR

    Simple solution would be -
    1. Traverse a directory for each file.
    2. Read each file using Buffered Reader.
    3. Use readLine() method to read a line.
    4. Use String.indexOf to see if the data which you are looking for is there in the line or not.
    5. If its there then store the line number and file name.
    6. When the loop will end you will have list of file name and line number where the data which you are looking for is present.

  • COM object model not functioning after encrypting the word document using office extention

         After encrypting A word document using office extention, it seems like the COM object model becomes unusable. I cannot not manipulate the encrypt document through COM interface, nor can I write macro VBScript to do some operation on the docuent.
         For example, I create a word Macro with some simple code.
    Sub hi()
        Dim i As Integer
        i = ThisDocument.Fields.Count
        MsgBox i   
    End Sub
         If the document is not encrypt, then we will see a msgbox which tells us the number of bookmarks in the current document.
         Now, encrypt the word document using the office extention, running the macro code will fail with error code " 80004005" and error message:Method 'Fields' of bject 'ThisDocument' failed.
    It is very important for us to manipulate word documents through COM interface in our system, I want to know why the COM interface does not work anymore after the document is encrypted.

    I take macro script as an example to demonstrate that word COM object model cannot be used after encripted. Actually you can reproduce the same problem when you manipulate word document through COM interface using any programming language.
    For example, you can create a text document  with the following VBscript:
    set a = createobject("Word.Application")
    set doc = a.Documents.Open("E:\temp\test.docx")
    msgbox doc.Fields.count
    doc.close
    a.quit
    Save the document with ".vbs" file extention and then run it by double clicking it. it will fail if the document is encrypted using the office extention. 

  • Search for a string in SAP Script

    Hello Experts,
    Is there a way to search for a string in all Z SAP scripts, like ABAP source scan?
    Regards,
    Hari.

    Try using function module "READ_FORM" (you will have to write a simple z-program to find all SAPscripts in your system then use this function module to search the SAPScript Forms for a particular string).
    All you have to do is supply the "FORM" name and it will return all of the Pages, Paragraphs, and Texts etc....
    More specifically, try looping at the "FORM_LINES" internal table result and you will see the SAPScript text there....
    Have fun    !
    Edited by: Christopher Twirbutt on Sep 16, 2009 12:43 AM

  • How do i search for a photo album when i use the search facility on my iPad or iPhone it will not find photo albums of a given name can anyone help

    how do i search for a photo album when i use the search facility on my iPad or iPhone it will not find photo albums of a given name can anyone help

    Photo albums are not searched by Spotlight Search.  Go to Settings > General > Spotlight Search to see and reorder the available search items.
    For more details, see page 12 of the iPad iOS 5.1 User Guide

Maybe you are looking for

  • How to get how many lines a formatted string has

    ih, is there a way to get by formula code the object's number of lines once it has been formatted by the format editor checking the the Can Grow check with 0 as maximum number of lines? The Can Grow check can offer you to enter the Maximum number of

  • Using '&' in a sql statement

    Hi, My question is how I can insert exeactly the value '&Job' to my table without being prompted by SQL*Plus for entering a value for Job? Thanks null

  • Are there any firmaware updates for time capsule?

    Are there any firmware updates for Time Capsule/Time machine? I just had a new hybrid (SSD and spinning disk) drive (750 GB) installed on my MacBook Pro and used Boot Kamp to partition it and run Windows xp. Now, my previously working Time Machine wo

  • Need to Keep Packages?

    Hi there, I just got a MBP this past week, and one of the things I'm interested in doing is cleaning out all the unnecessary files that are cluttering my disk space. I ran a program called Whatsize (very nice app, btw), and it shows that I've got som

  • Error when attempting to upgrade firmware: uprade are failed!

    firmware currently set from factory at firmware 1.0, and the failed message comes up a few seconds after selecting the .bin file on desktop and hitting the upgrade button. cant seem to figure out why, tried re-downloading the firmware zip package, bu