Reading stuff from a database to add into a doc with
docvariables I did the following.
Under Modules made a new module called AutoExec so this
executes when you go into word. Your module must be called
AutoExec and the routine must be called Main. Read about
auto macros to learn more. The first part is where I read
info from the Registry and use in as doc variables and the
next reads from a database. I use 4 address fields and
decide if the one is blank what to do so if the address is
3 lines instead of 4 it also works and vice versa.
My database is called ADNDatabase.
Trust this will solve the problem.
Sub Main()
strADNDB = ""
strMASTPATH = ""
Dim strSection As String
strSection = "HKEY_CURRENT_USER\Software\XYZ Company
Name"
strADNDB = System.PrivateProfileString(FileName:="",
Section:=strSection, Key:="ADNDataBase")
strMASTPATH = System.PrivateProfileString
(FileName:="", Section:=strSection, Key:="ADNMastPath")
Set ADNDataBase = OpenDatabase(strADNDB)
Set rdCompInfo = ADNDataBase.OpenRecordset("CompInfo")
If IsNull(rdCompInfo.Fields(1).Value) = True Then
CoName = ""
Else
CoName = rdCompInfo.Fields(1).Value
End If
If IsNull(rdCompInfo.Fields(6).Value) = True Then
CoAdd1 = ""
Else
CoAdd1 = rdCompInfo.Fields(6).Value
End If
If IsNull(rdCompInfo.Fields(7).Value) = True Then
CoAdd2 = ""
Else
CoAdd2 = rdCompInfo.Fields(7).Value
End If
If IsNull(rdCompInfo.Fields(8).Value) = True Then
CoAdd3 = ""
Else
CoAdd3 = rdCompInfo.Fields(8).Value
End If
If IsNull(rdCompInfo.Fields(9).Value) = True Then
CoAdd4 = ""
Else
CoAdd4 = rdCompInfo.Fields(9).Value
End If
rdCompInfo.Close
ADNDataBase.Close
If (CoAdd1 <> "") And (CoAdd2 <> "") And (CoAdd3 <> "")
And (CoAdd4 <> "") Then
CoAdd = CoAdd1 & ", " & CoAdd2 & ", " & CoAdd3 & ", "
& CoAdd4
Documents.add
Else
If (CoAdd1 <> "") And (CoAdd2 <> "") And (CoAdd3
<> "") Then
CoAdd = CoAdd1 & ", " & CoAdd2 & ", " & CoAdd3
Documents.add
Else
If (CoAdd1 <> "") And (CoAdd2 <> "") And (CoAdd4
<> "") Then
CoAdd = CoAdd1 & ", " & CoAdd2 & ", " & CoAdd4
Documents.add
Else
If (CoAdd1 <> "") And (CoAdd3 <> "") And
(CoAdd4 <> "") Then
CoAdd = CoAdd1 & ", " & CoAdd3 & ", " &
CoAdd4
Documents.add
Else
If (CoAdd1 <> "") And (CoAdd2 <> "") Then
CoAdd = CoAdd1 & ", " & CoAdd2
Documents.add
Else
If (CoAdd1 <> "") And (CoAdd3 <> "")
Then
CoAdd = CoAdd1 & ", " & CoAdd3
Documents.add
Else
If (CoAdd1 <> "") And (CoAdd4
<> "") Then
CoAdd = CoAdd1 & ", " & CoAdd4
Documents.add
Else
If (CoAdd1 <> "") Then
CoAdd = CoAdd1
Documents.add
Else
CoAdd = " "
Documents.add
End If
End If
End If
End If
End If
End If
End If
End If
Dim X As New EventClassModule
Set X.app = Word.Application
End Sub