Excel VBA to cycle through Word bookmarks and insert Excel range value

  • I'm trying to create a process in an Excel addin to 1) open a Word document, 2) search the document for bookmarks, then 3) insert the value from an Excel range that is named the same as the Word bookmark.

    I'm hoping that I can get this to be done within Excel code and not any Word VBA. I also want this code to be pretty generic to be used with documents of varying numbers of bookmarks. So I would like to use variables and loops to the fullest extent possible.

    Here's what I have so far that isn't working for me:



    This is crashing on the .bookmarks.Item(n.Name) command, but I'm not certain the rest of the process works properly either.

  • Re: Excel VBA to cycle through Word bookmarks and insert Excel range value


    Maybe this will help, you can adapt to suit your needs

  • Re: Excel VBA to cycle through Word bookmarks and insert Excel range value


    Thanks Richard. I kept my code to insert the value from cells in Excel based on a n.Name reference, but you did show me the error in my ways.

    This line

    Code
    With wdApp


    should have been

    Code
    With wdDoc


    with the appropriate adjustments to the documents.open command.

  • Re: Excel VBA to cycle through Word bookmarks and insert Excel range value


    Follow up question....

    When running this more than once on the same Word document it is inserting the text multiple times at the same bookmark. Is there a way to overwrite the text that had previously been added at the bookmark?

    My thought is to use Word fields, but I want to make sure the formatting from Excel gets carried over. And on the whole I absolutely hate the switches on mergefields.

    Thanks.

  • Re: Excel VBA to cycle through Word bookmarks and insert Excel range value


    I haven't tried it but would this work? Let us know how you go

  • Re: Excel VBA to cycle through Word bookmarks and insert Excel range value


    I searched all over for other advice and found some that has worked. My complete sub works with the following:


    using another sub.....


    I lost the link to the site that created this FillBookmark process, but I'm infinitely grateful. I wish I could give you the due recognition for your help.

  • Re: Excel VBA to cycle through Word bookmarks and insert Excel range value


    Thanks for posting the final code, you have helped every member who might be looking for a similar solution, if only there were a few more who would conclude their threads to finish the story for the benefit of all.

    Richard

  • Re: Excel VBA to cycle through Word bookmarks and insert Excel range value


    Hello, I know that this thread has been inactive for a long time, but this is EXACTLY what I am looking for and I unfortunately cannot get it to work. I have copied & pasted the code from the 6th post into a new VBA module in my Microsoft Excel 2007 Spreadhseet and I have ensured that I have the Microsoft Word 12 Object Library selected in the references section.


    My first issue is that when I try to run the sub PopulateWordBookmarks I can't. My Excel doesn't like the (strDoc As String) in brackets. When I remove that and just write "Dim strDoc As String" I can run the script, but I get stuck on this line:

    If RangeNameExists(strName) Then


    with the error "Compile Error: Sub or Function not defined". I am a bit of a noob to VBA, so any help that you can offer would really be appreciated. I am probably doing something really basic and really simple wrong, but I just can't figure it out. Thanks!

  • Re: Excel VBA to cycle through Word bookmarks and insert Excel range value


    defining the variable in the first line of the sub should work. not sure why excel doesn't like that, but if you have a work-around then maybe you're ok.


    try this for the RangeNameExists sub:



    Hope that works for you

  • Re: Excel VBA to cycle through Word bookmarks and insert Excel range value


    That's absolutely brilliant! Thanks so much for that.


    Is there a way to be able to modify this particular bit of VBA to be able to paste graphs and tables from the Excel document. Currently when I try to "autocopy" from a range that is a table I get the error: "Run-time error '94': "Invalid use of Null" on the following line:


    strValue = Trim(Application.Range(strName).Text)


    I guess I would need to build an if statement for the vba script to be able to determine between text and between tables/graphs. I am a bit lost at the moment as to how I could do this. Any help here would also be massively appreciated.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!