Error 1004 with VBA code with bookmarks

436 Views Asked by At

I am using a macro to populate a word document with text from named ranges in excel. The word document has bookmarks that correspond with the named excel ranges. I did not write the code, but rather copied it from another source.

There is quite a bit more to this macro than the snippet I posted. I could post the rest if that is useful. I had about half of my word document bookmarked and the macro was working fine then it suddenly stopped working.

I am receiving a error 1004 in the line highlighted below. I am a newbie so I'm not even quite sure what I should be searching for to fix this issue. Any assistance you could provide would be appreciated! Thanks in advance!

P.S. In case it's relevant, I am using Word and Excel 2007

 'PASTE TEXT STRINGS LOOP
    n = 1
    For Each temp In BkmTxt
        p = p + 1
        Prompt = "Please wait. Copying text. Carrying out operation " & p & " of " & pcount & "."
        Application.StatusBar = Prompt

    'If The Bkmtxt(n) is empty then go to the next one, once that has been found do next operation.
    If BkmTxt(n) = Empty Then
        n = n + 1

    'should find match and work
    Else

        'You might want to use multiple copies of the same text string.
        'In this case you need to call the bookmark as follows: "ARTextWhatever_01"
        'You can use as many bookmarks as you want.
        BkmTxtSplit = Split(BkmTxt(n), "_")
        vValue = Range(BkmTxtSplit(0)).Text **<----- ERROR HERE**

        Set wdRng = wdApp.ActiveDocument.Bookmarks(BkmTxt(n)).Range
        If Len(sFormat) = 0 Then
            'replace the bookmark text
            wdRng.Text = vValue
        Else
            'replace the bookmark text with formatted text
            wdRng.Text = Format(vValue, sFormat)
        End If

        'Re-add the Bookmark
         wdRng.Bookmarks.Add BkmTxt(n), wdRng
         n = n + 1
    End If
Next
2

There are 2 best solutions below

0
On

Step 1: Don't copy code from external sources. Use external sources as a learning tool and try to understand what they are actually doing.

Now if I understand you correctly, you simply have an Excel sheet with named ranges, I assume they have information already within them, and a word document with bookmarks that EXACTLY match the named ranges:

Step 2: Make sure you have the word object library reference within excel

Here:

sub KeepItDoin()
    dim xlRange as Excel.Range
    dim wdApp as new Word.Application
    dim wdDoc as Word.Document
    dim wdBkm as Word.Bookmark

    set wdDoc = wdApp.Documents.Open( "Filepath" ) 'get filepath from where ever  

    for each wdBkm in wdDoc.Bookmarks
        set xlRange = Application.Range(wdBkm.Name)
        wdBkm.range.text = xlRange.Value
    next wdBkm
end sub

That will get you close probably (didn't test, don't care if it works. Use it to learn). The idea is that if the bookmarks match up to the range, we can use their names to find the ranges in excel and then tell excel to move the data within it into the bookmarks range.

You will likely need to add some formatting or maybe create a table and then move cell by cell in the range and fill the table but this is as close as I'm willing to get since you like to copy pasta.

0
On

In case anyone is interested, I figured it out. There was an error with the bookmarks I inserted into my Word document. This macro returns Error 1004 if the word document contains a bookmark that does not correspond to a range in excel. Thank you for your help.