Create letter from database - code listing

This is a complete listing of the code referred to in the previous example.

If you want to make use of this macro, copy and paste into the macro editor of Database. It need to be a global macro so use Tools/Macros. Since it works with the current table, you'll need to execute the macro while actually viewing a table. For example, you can add a button to the toolbar that allows you to do this. (Select Tools/Customize and then the Macro Shortcuts tab. Drag the macro "MakeLetterFromDB", by its button, onto one of the toolbars at the top of the screen).

To tailor it to your own data, replace the field names starting at line 5 with your own.

Sub MakeLetterFromDB

Dim myflds(9), lfld, s, tb, appWrite, mydoc, i 

 

lastfld = 8 

myflds(0) = "CustTitle" 

myflds(1) = "Firstname" 

myflds(2) = "Lastname" 

myflds(3) = "Company" 

myflds(4) = "Add1" 

myflds(5) = "Add2"  

myflds(6) = "Add3"  

myflds(7) = "Add4"  

myflds(8) = "Pcode"  

 

Set tb = ActiveDataObject 

s = "" 

For i = 0 To lastfld 

txt = tb.Fields(myflds(i)).Value 

If Len(txt) > 0 Then 

s = s & txt 

If i < 2 Then 

s = s & " " 

Else 

s = s & vbCr 

End If 

End If 

Next 

 

i = MsgBox("Make a letter with this record?" & _ 

vbCr & vbCr & s, vbOKCancel, _ 

"Ability Database Macro Message") 

If i = vbCancel Then 

Exit Sub 

End If 

 

Set appWrite = CreateObject("AbilityWrite.Application") 

Set mydoc = appWrite.Documents.Add("NORMAL") 

mydoc.Text.Insert 0, s & vbCr & vbCr  

mydoc.Selection.End = mydoc.Text.Count 

mydoc.Selection.Start = Selection.End 

 

appWrite.Activate 

End Sub