+ Reply to Thread
Results 1 to 9 of 9

VBA to Push Excel Table to Word

  1. #1
    Registered User
    Join Date
    04-15-2014
    Location
    UK
    MS-Off Ver
    Office 2016
    Posts
    16

    VBA to Push Excel Table to Word

    Hello, I have the following code that runs from within Excel to copy and paste a table in to Word between 2 bookmarks. I have to do this from Excel to Word as the company security protocols block running VBA in Word files.

    As the code stands it works and I'm sure there are ways in which is can be improved but the issue I have is that it doesn't work for everyone and in theory we all have the same build of corporate laptops. I'm at a loss as to what might be causing the issue. At first some did not have the "Microsoft Word 16.0 Object Library" enable in Excel and this fixed 1 or 2, but some are still getting;

    Run time error 4068 - Value out of Range

    If debug is clicked it highlights the following line

    wdDoc.Range(rngStart.End + 2, rngEnd.Start - 2).Delete


    Now I've watch someone do this and I know they are using the right Word file with the bookmarks in, so very confused.

    Many thanks for any tips or guidance as to where to look next, as I still think it a PC issue, due to the code working on other PC's



    The full code

    Please Login or Register  to view this content.
    Last edited by AliGW; 11-04-2024 at 04:12 AM. Reason: Code tags added - please review the forum guidelines.

  2. #2
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,425

    Re: VBA to Push Excel Table to Word

    AI says:

    The "Run time error 4068 - Value out of Range" in VBA for Word can be caused by several factors. Here are a few possible reasons:

    1. Invalid index values: If the values rngStart.End + 2 or rngEnd.Start - 2 exceed the document’s range, this can cause the error. Ensure these values are within the document’s bounds.
    2. Bookmarks out of range: If the bookmarks rngStart and rngEnd are set such that their end positions are outside the document’s range, this can also lead to the error. Check that the bookmarks are correctly positioned.
    3. Overlapping ranges: If rngStart.End + 2 is greater than rngEnd.Start - 2, the range to be deleted will be invalid. Make sure the ranges do not overlap in a way that causes a negative length.
    4. Empty bookmarks: If the bookmarks rngStart or rngEnd are empty (contain no text), operations on their end positions might be invalid.

    Artik

  3. #3
    Registered User
    Join Date
    04-15-2014
    Location
    UK
    MS-Off Ver
    Office 2016
    Posts
    16

    Re: VBA to Push Excel Table to Word

    Artik,

    Many thank for coming back to me on this. I will check the above, but also know the user having issue are working with the same file as me, as I've passed it to them via email and watched them save them and then open then on their own PC's. But I haven't actually check the word document on their PC's to make sure nothing is being changed or removed.

    Regards

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2412 (Windows 11 24H2 64-bit)
    Posts
    88,706

    Re: VBA to Push Excel Table to Word

    @Artik

    Administrative Note:

    Sorry, but your post does not comply with Rule #9 of our Forum Rules:

    The use of AI tools (e.g. chatGPT, BARD, GPT4 etc) to create forum answers is not permitted. If a user is believed to have used such tools to provide a forum answer, sanctions may be imposed.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  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,776

    Re: VBA to Push Excel Table to Word

    Instead of bookmarking two ranges and trying to past a table between them, you should simply bookmark the range you want to paste the table into.

    The following code assumes you want to paste the Excel table into a bookmarked Word range named "RAID":
    Please Login or Register  to view this content.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    Registered User
    Join Date
    04-15-2014
    Location
    UK
    MS-Off Ver
    Office 2016
    Posts
    16

    Re: VBA to Push Excel Table to Word

    Hi Paul,

    Thats for the alternative option, I will take a look at that. Although, the reason I have it pasting between 2 bookmarks is that I want them to still be available after the paste, as there might be reason to run the macro again after updates to the RAID. Just wondering if a single bookmark will remain after the paste?

    But will give it a go.
    Richard

  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,776

    Re: VBA to Push Excel Table to Word

    Quote Originally Posted by richard.venn View Post
    the reason I have it pasting between 2 bookmarks is that I want them to still be available after the paste, as there might be reason to run the macro again after updates to the RAID. Just wondering if a single bookmark will remain after the paste?
    If you look at the code (or even just the document after running it), you will see that the bookmark is still there after the code finishes.

  8. #8
    Registered User
    Join Date
    04-15-2014
    Location
    UK
    MS-Off Ver
    Office 2016
    Posts
    16

    Re: VBA to Push Excel Table to Word

    Paul,

    Thanks for the confirmation, I will certainly give that a try, when back in the office this week.

  9. #9
    Registered User
    Join Date
    04-15-2014
    Location
    UK
    MS-Off Ver
    Office 2016
    Posts
    16

    Re: VBA to Push Excel Table to Word

    Hi Paul,

    Apologies for the delay, but again thanks for this, makes things a little better to work with.

    Richard

+ 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. Excel Push Data to Word Templates in Same Folder and Save
    By displacedtex in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-30-2022, 12:32 AM
  2. Extract Word header table data to excel (closed word document)
    By Yakov on Excel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-14-2017, 02:12 PM
  3. Have table push the lines below?
    By horsefish01 in forum Excel General
    Replies: 2
    Last Post: 10-25-2015, 04:32 PM
  4. [SOLVED] Push cell value from Excel to bookmarks in Word
    By Tor Jan Idland in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-30-2014, 02:39 AM
  5. Convert Word to Excel based upon Word Table of Contents
    By jcappuccino7 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-06-2013, 01:25 PM
  6. Populate an Excel Form from a table on push button click
    By munzer1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-23-2007, 09:55 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