Bookmarks in Word stopping at first blank record

Fiona H

Registered User.
Local time
Today, 09:36
Joined
Oct 25, 2006
Messages
28
Could anyone help me sort this out. My database inputs records into a template document in word. I have the template set up with bookmarks, but as soon as there is a blank record, the whole process stops. This is what I have been using

Code:
Private Sub MergeButton_Click()
        
    'Start Microsoft Word 97.
    Set objWord = CreateObject("Word.Application")

    With objWord
        'Make the application visible.
        .Visible = True

        'Open the document.
        .Documents.Open ("\\Globalinfra.net\Teams\AW_LI_OMC\Private\Water\Regional Networks\Modellers\Database\Proforma for Modelling Results Template")

      
        'Move to each bookmark and insert text from the form.
        objWord.ActiveDocument.Bookmarks("Requested").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![Requested By]
        
        objWord.ActiveDocument.Bookmarks("Business").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![Business Unit]
        
        objWord.ActiveDocument.Bookmarks("DateRequested").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![Date Requested]
        
        objWord.ActiveDocument.Bookmarks("DateRequired").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![Date Required]
        
        objWord.ActiveDocument.Bookmarks("Details").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![Modelling Request Text]
        
        objWord.ActiveDocument.Bookmarks("DMA").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![DMA's Affected]
                
        objWord.ActiveDocument.Bookmarks("DZ").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![DZ Affected]
               
        objWord.ActiveDocument.Bookmarks("Grid").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![Grid Reference]
        
        objWord.ActiveDocument.Bookmarks("Location").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![Location of Work]
        
        objWord.ActiveDocument.Bookmarks("Model").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![Model Used]
        
        objWord.ActiveDocument.Bookmarks("Requestno").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![OMC Number]
        
        objWord.ActiveDocument.Bookmarks("SAP").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![SAP Work Order Number]
        
        objWord.ActiveDocument.Bookmarks("Type").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![Type of Work]
        
        objWord.ActiveDocument.Bookmarks("Score").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![DMA Score]
        
        objWord.ActiveDocument.Bookmarks("Network").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![Network Manager Area]
        
        objWord.ActiveDocument.Bookmarks("Contact").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![Contact Number]
        
        objWord.ActiveDocument.Bookmarks("Capital").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![Capital Code]
        
        objWord.ActiveDocument.Bookmarks("Completedby").Select
        objWord.selection.Text = Forms![Modelling Results 2 - All]![Modeller]
     End With
    

End Sub

There need to be blank records and I have also tried

Code:
If IsNull(forms![Modelling Results]![DZ Affected]) = True Then
objWord.ActiveDocument.Bookmarks("DZ").Select 
objWord.selection.Text =""
Else
objWord.ActiveDocument.Bookmarks("DZ").Select
ObjWord.Selection.Text = Forms![Modelling Results]![DZ Affected]
End If

but this code isn't adding the fields in the correct places, and I have no idea why!

Please please please would someone have a look at this for me!!
 
I used this, remembering to go into the tools menu, references and add the Microsoft Word Object Library

Code:
Private Sub MergeButton_Click()
       
Dim objWord As Word.Application

     'Start Microsoft Word 97.
    Set objWord = CreateObject("Word.Application")

    With objWord
        'Make the application visible.
        .Visible = True

        'Open the document.
        .Documents.Open ("C:\Documents and Settings\Fee\My Documents\Database\Proforma for Modelling Results Template")
        '"\\Globalinfra.net\Teams\AW_LI_OMC\Private\Water\Regional Networks\Modellers\Database\Proforma for Modelling Results Template")

        'Move to each bookmark and insert text from the form.
        
.ActiveDocument.Bookmarks("Requested").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![Requested By], "Requested")

.ActiveDocument.Bookmarks("Business").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![Business Unit], "Business")

.ActiveDocument.Bookmarks("Capital").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![Capital Code], "Capital")
    
.ActiveDocument.Bookmarks("DateRequested").Select
.Selection.Text = Format(CDate(Forms![Modelling Results 2 - All]![Date Requested]), "dd/mm/yy")

.ActiveDocument.Bookmarks("DateRequired").Select
.Selection.Text = Format(CDate(Forms![Modelling Results 2 - All]![Date Required]), "dd/mm/yy")

.ActiveDocument.Bookmarks("Details").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![Modelling Request Text], "Details")

.ActiveDocument.Bookmarks("DMA").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![DMA's Affected], "DMA")

.ActiveDocument.Bookmarks("DZ").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![DZ Affected], "DZ")

.ActiveDocument.Bookmarks("Grid").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![Grid Reference], "Grid")

.ActiveDocument.Bookmarks("Location").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![Location of Work], "Location")

.ActiveDocument.Bookmarks("Model").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![Model Used], "Model")

.ActiveDocument.Bookmarks("Requestno").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![OMC Number], "00000")

.ActiveDocument.Bookmarks("SAP").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![SAP Work Order Number], "SAP")

.ActiveDocument.Bookmarks("Type").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![Type of Work], "Type")

.ActiveDocument.Bookmarks("Score").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![DMA Score], "&&")

.ActiveDocument.Bookmarks("Network").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![Network Manager Area], "Network")

.ActiveDocument.Bookmarks("Contact").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![Contact Number], "Contact")

.ActiveDocument.Bookmarks("Completedby").Select
.Selection.Text = Format(Forms![Modelling Results 2 - All]![Modeller], "Completedby")

End With

MergeButton_Err:
'If a field on the form is empty, remove the bookmark text, and
'continue.
If Err.Number = 94 Then
objWord.Selection.Text = ""
Resume Next
End If
End Sub

Its a bit of a mish mash of bits I've picked up from different threads, but it worked for me...
 

Users who are viewing this thread

Back
Top Bottom