I didn't bother with the error-handling, which should not have any effect on the error you say you were getting. Indeed, the code could be further simplified. For example' leaving aside all the DAO-related code:
Code:
Sub Demo()
Dim appWord As New Word.Application
appWord.Documents.Add
'insert format document titel
With appWord.Selection
.TypeText "Aktuelle Kontakte mit " & Format(Date, "Long Date")
.TypeParagraph
.MoveLeft Unit:=wdWord, Count:=11, Extend:=wdExtend
.Font.Size = 14
.Font.Bold = wdToggle
.MoveDown Unit:=wdLine, Count:=1
'insert two column table to hold contact data(one column for contact names, the other for user comments)
.Tables.Add Range:=.Range, NumRows:=1, NumColumns:=2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
With .Tables(1)
.Style = "Table Grid"
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With
End With
appWord.Visible = True
End Sub
Note: I wouldn't do my own coding your way - or the way I've outlined it in my replies. I've just tried to make minimal changes to your code.
If I were trying to start a new Word session only if Word wasn't already running, I'd use late binding code like:
Code:
Sub Demo()
Dim wdApp As Object, wdDoc As Object, bStrt As Boolean
' Test whether Word is already running.
On Error Resume Next
bStrt = False ' Flag to record if we start Word, so we can close it later.
Set wdApp = GetObject(, "Word.Application")
'Start Word if it isn't running
If wdApp Is Nothing Then
Set wdApp = CreateObject("Word.Application")
If wdApp Is Nothing Then
MsgBox "Can't start Word.", vbExclamation
Exit Sub
End If
' Record that we've started Word, so we can terminate it later.
bStrt = True
End If
On Error GoTo 0
wdApp.Visible = True
End Sub
or early binding code like:
Code:
Sub Demo()
Dim wdApp As Word.Application, wdDoc As Word.Document, bStrt As Boolean
' Test whether Word is already running.
On Error Resume Next
bStrt = False ' Flag to record if we start Word, so we can close it later.
Set wdApp = GetObject(, "Word.Application")
'Start Word if it isn't running
If wdApp Is Nothing Then
Set wdApp = CreateObject("Word.Application")
If wdApp Is Nothing Then
MsgBox "Can't start Word.", vbExclamation
Exit Sub
End If
' Record that we've started Word, so we can terminate it later.
bStrt = True
End If
On Error GoTo 0
wdApp.Visible = True
End Sub