+ Reply to Thread
Results 1 to 8 of 8

Select Text Found in Word Document via Code Running in Excel

  1. #1
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Select Text Found in Word Document via Code Running in Excel

    Hi all.

    I'm currently having problems being able to select the text after finding it in a Word document. I'm running the code via Excel, and the following excerpt, for example, works fine:

    Please Login or Register  to view this content.
    (MyDoc is declared as Word.Document and is set via a WApp.Documents.Add procedure.)

    However, I cannot manage to get the syntax correct to actually select the occurrence of the found string within the word document, so as to format it as I wish.

    If I try, e.g.:

    Please Login or Register  to view this content.
    then this applies to the whole document, not just the selected text.

    And if I try:

    Please Login or Register  to view this content.
    then the selection still applies to the active cell in the Excel sheet, not the Word document.

    If it's important, then the lines immediately preceding these involve copying a section of text from the Excel document and pasting it into the Word document. The clipboard is not emptied.

    Really appreciate any help, as always.

    Cheers
    Last edited by XOR LX; 12-08-2014 at 07:02 AM.
    Click * below if this answer helped

    Advanced Excel Techniques: http://excelxor.com/

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,274

    Re: Select Text Found in Word Document via Code Running in Excel

    Try:
    Please Login or Register  to view this content.
    Or you could use a Bookmark in Word so that you can put the text there directly and then simply format the bookmark's Range.
    Remember what the dormouse said
    Feed your head

  3. #3
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Select Text Found in Word Document via Code Running in Excel

    Thanks. So I have to reference via .Application - ah!

    However, although I get no error, the found text is not formatted as requested. In fact, I can't find any occurrences of green text in the Word document, nor in the Excel document.

    Is the string of the .Text parameter immediately transferred to becoming the .Selection, then, if found? It certainly isn't "selected" (=highlighted) as part of the process of running the code. What am I missing?

    Re bookmarks, I've already used this for other parts of the code. This particular part, however, has to add some text in a position immediately after a variable-length piece of text has first been pasted in. I just used my example of colour-changing as an example: the basic gist is that I simply need to be able to make this search text (which is the final sentence in the pasted piece of text) the selection. Hopefully then I can work out how to move to a new line after that!

    Cheers

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,274

    Re: Select Text Found in Word Document via Code Running in Excel

    If you paste it in, you should be able to determine the location of the text from that, I would think, then format directly. (Mind you, Word is not my thing - you should probably post this in the Word programming section so someone like macropod sees it)

  5. #5
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Select Text Found in Word Document via Code Running in Excel

    Ok. Have done.

    Cheers

  6. #6
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Select Text Found in Word Document via Code Running in Excel

    Ok. I've manager to reference it correctly:

    Please Login or Register  to view this content.
    Now I just need to work out the code to go to the end of that search text and begin a new line!

    Cheers

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,274

    Re: Select Text Found in Word Document via Code Running in Excel

    I think it would be something like:
    Please Login or Register  to view this content.

  8. #8
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Select Text Found in Word Document via Code Running in Excel

    Excellent!

    That got me to the end of the selection. I can now add .InsertParagraphAfter and .InsertAfter, etc. and I think I'm on my way again!

    Many thanks. Will mark as solved.

    Cheers!

+ 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. Replies: 0
    Last Post: 09-04-2014, 03:32 AM
  2. Replies: 0
    Last Post: 06-11-2013, 05:14 PM
  3. Need VBA code to select entire content of word document - excel related
    By derryt in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-09-2012, 07:33 AM
  4. excel vba code to export text to word protect document
    By rob bob in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-16-2010, 01:07 PM
  5. [SOLVED] How to select the first 5 paragraphs in a Word Document from Excel
    By quartz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-12-2005, 12: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