+ Reply to Thread
Results 1 to 5 of 5

Store text as variable between two words in Word from Excel VBA

  1. #1
    Registered User
    Join Date
    06-27-2014
    Location
    Comillas, Spain
    MS-Off Ver
    MS Office 2013
    Posts
    11

    Store text as variable between two words in Word from Excel VBA

    Hi people,

    I am trying to do something that should be easy but for the life of me I cannot figure this out.

    I want to be able to find a text string in a Word document (from Excel VBA) between two specific words (these two words are always the same but the string may vary) and store that text string as a variable.

    After some research I found this post which I tried to adapt to my needs:

    http://www.ozgrid.com/forum/showthread.php?t=149126

    But when I try to execute this code (slightly adapted but no major changes) I get the following error:

    Run-time error '5560':

    The Find What text contains a Pattern Match expression which is not valid.

    Somehow the "*" Wildcard is stopping the code from working, but if I leave it out the next error I get at the next line of code is

    Run-time error '4120':

    Bad parameter

    You can see the code I am using below:

    Please Login or Register  to view this content.
    It might be heplful to mention that the starting word is "OTRA, " (notice the blank space at the end) and the ending word is "," (comma at the end of the 'target' text)

    I am completely lost

    Can someone point me in the right direction?

  2. #2
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Store text as variable between two words in Word from Excel VBA

    I ran it and it worked for me. Two things I did.
    1) Did you enable the Word Object Library? Go to Tools->References. Then put a check mark next to the 'Microsoft Word 14.0 Reference Library'. Note that the list is alphabetical, so you will need to scroll down quite a bit. Also, your text may be slightly different (e.g. 13.0 instead of 14.0), but it should be clear that it is Microsoft Word.

    2) This is probably not the source of your problem, but you need to add this code at the end or else Word is not properly terminated:
    Please Login or Register  to view this content.
    Pauley
    --------
    If I helped with your issue, I'd appreciate a rep bump (hit the '*' icon to the bottom left of this post).

  3. #3
    Registered User
    Join Date
    06-27-2014
    Location
    Comillas, Spain
    MS-Off Ver
    MS Office 2013
    Posts
    11

    Re: Store text as variable between two words in Word from Excel VBA

    Sample_file.xlsm(Nombre) Contrato.docx
    Quote Originally Posted by Pauleyb View Post
    I ran it and it worked for me. Two things I did.
    1) Did you enable the Word Object Library? Go to Tools->References. Then put a check mark next to the 'Microsoft Word 14.0 Reference Library'. Note that the list is alphabetical, so you will need to scroll down quite a bit. Also, your text may be slightly different (e.g. 13.0 instead of 14.0), but it should be clear that it is Microsoft Word.

    2) This is probably not the source of your problem, but you need to add this code at the end or else Word is not properly terminated:
    Please Login or Register  to view this content.
    Hi Pauleyb,

    Thanks for your response.

    I have activated the reference you mention (in my PC is Microsoft Office 15.0 Object Library), and when I compile the code, VBA doesn't find anything wrong with the code, but when I run it, it returns the same error I mentioned earlier.

    I know the code is incomplete, it is part of a greater code but I isolated it to find an answer quicker as the error appears only when I run it, thanks for pointing that out.

    I think it would be easier to sort out if I attach sample files with what I am trying to do.

    The area highlighted in yellow in the .docx file is the one I am trying to select (keep in mind it belongs to a bigger document that varies in length so counting characters will not suffice).

    Thanks again for your help

  4. #4
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Store text as variable between two words in Word from Excel VBA

    Note, it was the Microsoft Word library not the Microsoft Office. Try again.

    Also, the code in your file does not match the code above. Above, your EndWord is "," but in your file it is "(EN ADELANTE". A parenthesis is a special character in regular expressions. You need to change it to "\(EN ADELANTE". That is probably you are getting a pattern match error. Next time, include the code that is giving you a problem, since I did go and double check that a comma is not a special character in regular expressions.

  5. #5
    Registered User
    Join Date
    06-27-2014
    Location
    Comillas, Spain
    MS-Off Ver
    MS Office 2013
    Posts
    11

    Re: Store text as variable between two words in Word from Excel VBA

    Hi Pauleyb,

    THANKS A LOT!

    Thanks for noticing those two details! It now works flawless!

    I appreciate your patience since I am a novice in VBA.

    I did try different strings of text, at one point I did try it with a comma and I must have switched while creating the sample file, sorry about that.

    WELL-DESERVED REPUTATION ADDED

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How to find last filled Row number in excel and store it in variable to use later
    By psoft in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-02-2014, 01:03 PM
  2. text box input to store as a variable?
    By losmi8 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-06-2009, 01:25 PM
  3. Pull URL from IE to store as Variable in Excel
    By odo in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-19-2009, 04:33 PM
  4. Insert a word/text before a list of words
    By Owen19102003 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-24-2008, 01:54 PM
  5. How to store text to variable
    By jafery in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-29-2005, 05:18 AM

Tags for this Thread

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