Acces to Word - Checking for Bookmarks

jubb

Registered User.
Local time
Tomorrow, 05:17
Joined
Jul 27, 2005
Messages
50
Hi,

Is there a way to check if a bookmark exists in a word document?

I need to do this as I have multiple templates that can be selected and filled out from 1 form, but I need to modify the code that transfers the data to the bookmarks in the word document to skip any missing bookmarks.

Any help or pointers in the right direction would be greatly appreciated.


Thanks

Jubb.

I can post the code I am using so far if required.
 
why dont you open the word document and check there?

Bones
 
Hi,

I am opening the form via VB code to insert the text where the bookmarks appear in the document. Not sure how to actually check if the bookmarks exist in the template.

Other users will be setting up different templates using a set of available bookmarks they have been given, but not all bookamrks will be used in every template so I basically need to check if bookmark exists then transfer text to bookmark.

At this stage I have just used
on error resume next at the start of the code for transferring the text from access to word. I am just about to try and see if this works or not.

Thanks

Jubb
 
Thanks to all who looked,

I have worked out how to check if a bookmark exists in a word document from VB in access.

I am posting my code as others may find it helpful.
Code:
Private Sub cmdWord_Click()
    On Error GoTo ErrHandler

'Decalre Variables
    Dim objWord As Word.Application
    Dim cdrs As Recordset
    Dim cdrsSQL As String
    Dim strTable As String
    Dim docsavelocation As String

'Set Values of Variables
    cdrsSQL = "SELECT [Task Name], [Date Initially Due], [Action Required], [Due On], [ContactId] FROM ContactTasks where [ContactId]= " & Me.ContactId & ";"
    Set cdrs = CurrentDb.OpenRecordset(cdrsSQL)

'copy the data to be converted to a word table
    cdrs.MoveFirst
    Do Until cdrs.EOF
        strTable = strTable & cdrs![Task Name] & vbTab
        strTable = strTable & cdrs![Date Initially Due] & vbTab
        strTable = strTable & cdrs![Action Required] & vbTab
        strTable = strTable & cdrs![Due On] & vbCr
        cdrs.MoveNext
    Loop
    cdrs.Close

'copy all single entries to word document
    Set objWord = CreateObject("Word.Application") 
    objWord.Visible = False 
    objWord.Documents.Add (strTemplateLocation)  ' set from list box of available templates

    If objWord.ActiveDocument.Bookmarks.Exists("ContactId") Then
        objWord.ActiveDocument.Bookmarks("ContactId").Select 
        objWord.Selection.Text = Me.ContactId 
    End If
    If objWord.ActiveDocument.Bookmarks.Exists("StudentId") Then
        objWord.ActiveDocument.Bookmarks("StudentId").Select
        objWord.Selection.Text = Me.StudentId 
    End If
'etc etc for as many bookmarks as you need to make available for template creation

'Insert Contact Details and create word table (NB Contact Details are from a 1:N Relationship)
    If objWord.ActiveDocument.Bookmarks.Exists("ContactDetails") Then
        objWord.ActiveDocument.Bookmarks("ContactDetails").Select
        objWord.Selection.Text = strTable
        objWord.Selection.ConvertToTable (vbTab)
        'objTable = objWord.Selection
    End If
    
    objWord.Visible = True     
    docsavelocation = CurrentProject.Path & "\Sent Letters\" & Me.ContactId & ".DOC"
    objWord.ActiveDocument.SaveAs (docsavelocation)
    Set objWord = Nothing

ErrHandler:
    Set objWord = Nothing

End Sub
 

Users who are viewing this thread

Back
Top Bottom