Hi Macropod
Still no success and the worst part is that I'm not sure if I understand what is going on here!!
Sorry this is a long post. I’m just trying to add as much detail as possible as I get the feeling that it’s just a slight tweak/s that is required.
I’m looking to initially add format switches manually to the Bookmarks in the actual Word Template and hopefully once the code runs it will fill the Bookmarks in the document with data in the same format as the switches in the Word Template. I’ll also have Cross-references to the Bookmarks in the Word Template which will update with each of the values of the Word Bookmarks and ideally none of the Bookmark or Cross-reference fields will be present in the output document although it won’t matter too much if they are.
Some of the code in this loop had formatting instructions included so I’ve decided to remove those bits to make the code clearer.
After deleting out the formatting code this is what is left:
Code:
Dim i As Long
For i = oDoc.Bookmarks.Count To 1 Step -1
With oDoc.Bookmarks(i)
Set objX = wsData.Rows(1).Find(.Name, LookIn:=xlValues, LookAt:=xlWhole)
If Not objX Is Nothing Then
' found
Set BmkRng = .Range
[COLOR=#008000]BmkRng.Text = wsData.Cells(rng2.Row, objX.Column)[/COLOR]
[COLOR=#ff0000]oDoc.Bookmarks.Add .Name, BmkRng[/COLOR]
Else
MsgBox "Bookmark '" & .Name & "' not found", vbOKOnly + vbCritical, "Error"
GoTo Tidy_Exit
End If
End With
Next
As I step through the code line by line everything appears to be working great up to and including the green line.
If we assume:
· there are six Bookmarks in the Word Template
· the first Bookmark is Surname
· the first Surname in the Excel Data Sheet is Smith
then whilst I’m on the green line and directly before I step onto the red line we have the following values:
Code:
i = 6
oDoc.Bookmarks.Count = 6
oDoc.Bookmarks(i) = 6
objX = Surname
.Name = Surname
BmkRng = Surname
.Range = Surname
BmkRng.Text = Surname
However once I step onto the red line the following values appear:
Code:
i = 6
[B]oDoc.Bookmarks.Count = 5[/B]
oDoc.Bookmarks(i) = 6
objX = Surname
[B].Name = Object has been deleted.[/B]
[B]BmkRng = Smith[/B]
[B].Range = Object has been deleted.[/B]
[B]BmkRng.Text = Smith[/B]
The red line is also the last line I can run through. If I try to go onto the next line of code the following error message appears with the red line highlighted once I click Debug:
Run-time error ‘5825’:
Object has been deleted.
I assume that the following line replaces the entire Bookmark (Surname) in the Word Document with the Data Value (Smith):
Code:
[COLOR=#008000]BmkRng.Text = wsData.Cells(rng2.Row, objX.Column)[/COLOR]
Then on the following line when the same Bookmark (Surname) needs to be re-inserted, the code is unable to find the correct location to insert the Bookmark as it has been replaced with the Data Value (Smith):
Code:
[COLOR=#ff0000]oDoc.Bookmarks.Add .Name, BmkRng[/COLOR]
I think what might be required is that before each Bookmark (Surname) is replaced with the Data Value (Smith), all of the Cross-reference fields for that Bookmark (Surname) need to be located. Once all of the relevant Cross-reference fields have been found, the Bookmark (Surname) needs to be replaced with the Data Value (Smith) and then the Cross-reference fields somehow re-inserted or re-linked back to the Data Value (Smith)
This is my take on the issue but I’m quite new to all of this so it’s quite likely that I have gone wildly off on a tangent!
Either way if you’re able to help in any way it would be very much appreciated it. Also, thanks for your help so far and HAPPY NEW YEAR!!