Open Record Set method

N

Nithya

This is part of my code.

Private Sub Generate_Letter_Click()
Dim dbLocal As DAO.Database
Dim replaceCodes As DAO.Recordset
Dim strCurrAppDir As String
Dim strFinalDoc As String
Dim varReplaceWith As Variant
Dim wrdApp As Object
Dim wrdDoc As Object
On Error GoTo Err_Generate_Letter_Click
Set dbLocal = CurrentDb()
strCurrAppDir = Left$(dbLocal.Name, InStr
(dbLocal.Name, -"\ECMS old.mdb"))
On Error Resume Next
Kill strFinalDoc
On Error GoTo Err_Generate_Letter_Click
FileCopy strCurrAppDir & "\sample.doc", strFinalDoc
Set appWord = CreateObject("Word.Application")
Set wordDoc = appWord.Documents.Add(strFinalDoc)
appWord.Visible = True
Set replaceCodes = dbLocal.OpenRecordset
("StateReplaceCodes") --- Here I get the type
mismatch error ( I have also made the reference to DAO 3.6
and also removed the reference to ADO Object)

TIA,
 
V

Van T. Dinh

What is "StateReplaceCodes"?

The declarations and code for the Recordset look fine.
 
V

Van T. Dinh

Can't see how you got the Type Mismatch error. The code to create the
Recordset looks fine and you have fully declared the Recordset object with
proper Reference (assuming you are using A2K or AXP).
 
N

Nithya

Actually I made a mistake by putting a minus sign in
between. Now that's clear. Thank you very much. Now I have
another question I am really new to VB. Can you please
explain me what this ".Execute" does? My table is
StateReplaceCodes with 2 fields Code and ReplaceWithText.
During run time when I tried to look what was there in
FindText, it was empty. So I could not replace anything in
my Word template.

Set replaceCodes = dbLocal.OpenRecordset
("StateReplaceCodes", dbOpenSnapshot)

Do While Not replaceCodes.EOF

varReplaceWith = replaceCodes!ReplaceWithText
varReplaceWith = IIf(IsNull(varReplaceWith), " ",
CStr(varReplaceWith))

With wordDoc.Content.Find

If replaceCodes!Code = "{Contact Name}" Then
With .Replacement
.ClearFormatting
.Font.Bold = True
End With
End If

.Execute FindText:=replaceCodes!Code, _
ReplaceWith:=varReplaceWith, Format:=True, _
Replace:=wdReplaceAll
End With

replaceCodes.MoveNext
Loop

replaceCodes.Close

Thanks in Advance,
Nithya
 
V

Van T. Dinh

That's Word VBA, NOT Access but I am sure it is the Find and Replace action
in Word.

You need to check Word VB Help for the full explanation.

--
HTH
Van T. Dinh
MVP (Access)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top