+ Reply to Thread
Results 1 to 10 of 10

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.

    On the advice of romperstomper, I am (re-)posting this query in the Word forums as it appears that it may be more appropriate here. This is the link to the thread I've just begun:

    http://www.excelforum.com/excel-prog...-in-excel.html

    If anyone can assist, or needs any more information, please don't hesitate to ask.

    @Mods If you feel it is more appropriate to simply move the existing thread to this forum then please do so.

    Many thanks in advance.

    Regards
    Click * below if this answer helped

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

  2. #2
    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

    Link now solved, so marking this as solved also.

    Regards

  3. #3
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

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

    Please Login or Register  to view this content.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #4
    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 a lot, macropod.

    Could I ask you (or anyone else) a (slightly) related, follow-up question?

    I'm now attempting to paste an Excel table into the same Word document, though no matter what I try I cannot get it to perform the equivalent of Merge Table from the Paste Special options.

    If I record a macro when doing this manually, the code is given as:

    Please Login or Register  to view this content.
    though adding this to my code seems to have no effect whatsoever (the result is precisely the same as if I'd performed .PasteAndFormat (wdTableInsertAsRows)).

    I have tried many other options, including PasteExcelTable, other PasteAndFormats, though none of these are exactly what I require, and seem like unnecessary workarounds given that there seems to be a perfectly good option, albeit one which I can't seem to access via VBA.

    Am I missing something? Why doesn't the PasteAppendTable method work in my code, yet that is what the macro recorder gives?

    Many thanks in advance.

    Regards

  5. #5
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

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

    Without knowing what the source data look like or how you expect them to appear in Word, it's difficult to give specific advice. However, I suspect you're after something like:
    Please Login or Register  to view this content.

  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

    Thanks, but that's my point: I don't want the equivalent of wdTableInsertAsRows. I want the equivalent of the Merge Table option from the Paste Special options.

    As I said, if I record a macro when doing this manually, the code is given as Selection.PasteAppendTable.

    But, as I said, using this in my code does not give the same results as the Merge Table option from the Paste Special options. But how can this be possible? How can the macro recorder give one thing for this manual operation yet when I attempt to use that very same line in my code I get a completely different result?

    The source is a 6-row-by-6-column table in Excel; the destination is a 6-row-by-6-column table in Word, if that helps.

    But I'm now equally curious to know why I can't reproduce the effects of Merge Table via code, especially since the code given in the macro recorder does not seem to work in practice.

    Regards

  7. #7
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

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

    There are some things you can do through the GUI for which there is no equivalent VBA method. This, it seems, is one of them. For what you want to do, you'll need to loop through the cells and write each one individually.

  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

    Ah, I see.

    In that case do you know of a quick way in which I can do that, given that I will then first need to create the table in the word doc (it will need to have a dynamic number of rows, depending on the Excel table)?

    I'm fine with writing the code to create that table in word, which I've already tried as a workaround to this issue.

    What I couldn't then do was to loop through all the cells in that table, since I couldn't find the equivalent of an OFFSET function for working in Word tables. The top-left cell in the headers of the table (which are the only part of the table which exist in the template, prior to running the code which inserts as many rows below as are required) is saved as a Bookmark.

    Or is there no way of offsetting from a Bookmark? Will I just have to find out the Table.Cell reference for each cell and write out as many statements as there are cells?

    Thanks a lot for your help with this.

    Regards

  9. #9
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

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

    In that case, why don't you simply copy & paste the Excel range, then add a header row to the pasted table in Word, if that's what you need?

  10. #10
    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 feel a bit stupid now for not having thought of that, but that's a perfect solution to my problem!

    Many thanks!

    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. [SOLVED] Select Text Found in Word Document via Code Running in Excel
    By XOR LX in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-08-2014, 09:27 AM
  2. Replies: 0
    Last Post: 09-04-2014, 03:32 AM
  3. Replies: 0
    Last Post: 06-11-2013, 05:14 PM
  4. 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
  5. 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

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