Hello
I’ve just realised that I’ve posted this question in the wrong forum (General Excel Discussion & Other Questions) so have re-posted it here.
I’m currently having some difficulty with the code below because it uses Bookmarks. As Bookmarks can only be used in one place at any one time, I have added some Cross-reference fields to my Word template.
However I’m unable to get the Cross-reference fields to update correctly. I think this might be because the code replaces the bookmark field with data and therefore when the Cross-reference fields try to update with the same data, the reference source is not found as it no longer is a bookmark.
Please could someone let me know if my understanding of this is correct and hopefully help me find a way around this (perhaps I should use Merge Fields)?
Thanks in advance.
p.s. I’m currently using Windows 7 and Office 2007.
I’ve just realised that I’ve posted this question in the wrong forum (General Excel Discussion & Other Questions) so have re-posted it here.
I’m currently having some difficulty with the code below because it uses Bookmarks. As Bookmarks can only be used in one place at any one time, I have added some Cross-reference fields to my Word template.
However I’m unable to get the Cross-reference fields to update correctly. I think this might be because the code replaces the bookmark field with data and therefore when the Cross-reference fields try to update with the same data, the reference source is not found as it no longer is a bookmark.
Please could someone let me know if my understanding of this is correct and hopefully help me find a way around this (perhaps I should use Merge Fields)?
Thanks in advance.
p.s. I’m currently using Windows 7 and Office 2007.
Code:
Option ExplicitSub Create_Letters()
' Note: you will need to add error-trapping
Dim objX As Object
Dim rng1 As Range
Dim rng2 As Range
Dim wb As Workbook
Dim wsControl As Worksheet
Dim wsData As Worksheet
'
Dim oApp As Word.Application
Dim oBookMark As Word.Bookmark
Dim oDoc As Word.Document
'
Dim strDocumentFolder As String
Dim strTemplate As String
Dim strTemplateFolder As String
Dim lngTemplateNameColumn As Long
Dim strWordDocumentName As String
Dim lngDocumentNameColumn As Long
Dim lngRecordKount As Long ' not used but retained for future use
'
Set wb = ThisWorkbook
Set wsControl = wb.Worksheets("Control Sheet")
wsControl.Activate
Set wsData = wb.Worksheets(wsControl.[Data_Sheet].Value)
strTemplateFolder = wsControl.[Template_Folder].Value
strDocumentFolder = wsControl.[Document_Folder].Value
wsData.Activate
lngTemplateNameColumn = wsData.[Template_Name].Column
lngDocumentNameColumn = wsData.[Document_Name].Column
' number of letters required:
' must not have any blank cells in column A - except at the end
Set rng1 = wsData.Range(Cells(2, 1), Cells(Rows.Count, 1).End(xlUp))
lngRecordKount = rng1.Rows.Count
'
'Set oApp = CreateObject("Word Application")
Set oApp = New Word.Application
' Process each record in turn
For Each rng2 In rng1
strTemplate = strTemplateFolder & "\" & wsData.Cells(rng2.Row, lngTemplateNameColumn)
strWordDocumentName = strDocumentFolder & "\" & wsData.Cells(rng2.Row, lngDocumentNameColumn)
' check that template exists
If Dir(strTemplate) = "" Then
MsgBox strTemplate & " not found"
GoTo Tidy_Exit
End If
Set oDoc = oApp.Documents.Add(strTemplate)
oApp.Selection.InsertFile strTemplate
' locate each bookmark
For Each oBookMark In oDoc.Bookmarks
Set objX = wsData.Rows(1).Find(oBookMark.Name, LookIn:=xlValues, LookAt:=xlWhole)
If Not objX Is Nothing Then
' found
If Right(oBookMark.Name, 4) = "Date" Then
oBookMark.Range.Text = Format(wsData.Cells(rng2.Row, objX.Column), "dd mmmm yyyy")
ElseIf Right(oBookMark.Name, 6) = "Amount" Then
oBookMark.Range.Text = Format(wsData.Cells(rng2.Row, objX.Column), "£#,##0.00")
Else
oBookMark.Range.Text = wsData.Cells(rng2.Row, objX.Column)
End If
Else
MsgBox "Bookmark '" & oBookMark.Name & "' not found", vbOKOnly + vbCritical, "Error"
GoTo Tidy_Exit
End If
Next oBookMark
'
oDoc.SaveAs strWordDocumentName
oDoc.Close
Next rng2
'
Tidy_Exit:
On Error Resume Next
Set oDoc = Nothing
Set oBookMark = Nothing
Set objX = Nothing
Set rng1 = Nothing
Set rng2 = Nothing
oApp.Quit
Set oApp = Nothing
'
Set wsData = Nothing
Set wsControl = Nothing
Set wb = Nothing
'
End Sub