D
Dale Fye
I'm using Access to build a Word document from scratch.
Sometimes, the code runs smoothly, but others, I get this error message. My
code (abbreviated) looks like below. When I insert a breakpoint and walk it
through, the error appears to be occuring is in the BoldUnderText subroutine.
When I get to that routine, and mouse over the first "Selection" line it
displays the message.
Would appreciate any help you can provide.
Public Sub Word_Doc()
Dim strSQL As String
Dim rs As dao.Recordset
Dim appWord As Word.Application
Dim wdDoc As Word.Document
Dim bWordWasOpen As Boolean
On Error GoTo ProcError
bWordWasOpen = True
Set appWord = GetObject(, "Word.Application")
appWord.Visible = True
Set wdDoc = appWord.Documents.Add(, , , True)
strSQL = "SELECT * FROM myQuery"
Set rs = CurrentDb.OpenRecordset(strSQL, , dbFailOnError)
While Not rs.EOF
With wdDoc
Call BoldUnderText(wdDoc, "Title:")
Selection.TypeText Text:=" " & rs("Field1")
Selection.TypeParagraph
Call BoldUnderText(wdDoc, "Statement:")
Selection.TypeText Text:=" " & rs("Field2")
Selection.TypeParagraph
rs.MoveNext
If Not rs.EOF Then Selection.InsertBreak Type:=wdPageBreak
End With
Wend
ProcExit:
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Set wdDoc = Nothing
Set appWord = Nothing
Exit Sub
ProcError:
If Err.Number = 429 Then
bWordWasOpen = False
Set appWord = CreateObject("Word.Application")
Resume Next
Else
Debug.Print Err.Number, Err.Description
MsgBox Err.Number & vbCrLf & Err.Description
Resume ProcExit
End If
End Sub
Private Sub BoldUnderText(ByRef wdDoc As Word.Document, TextToBoldUnder As
Variant)
With wdDoc
Selection.Font.Bold = wdToggle
Selection.Font.UnderlineColor = wdColorAutomatic
Selection.Font.Underline = wdUnderlineSingle
Selection.TypeText Text:=TextToBoldUnder
Selection.Font.Bold = wdToggle
Selection.Font.UnderlineColor = wdColorAutomatic
Selection.Font.Underline = wdUnderlineNone
End With
End Sub
Sometimes, the code runs smoothly, but others, I get this error message. My
code (abbreviated) looks like below. When I insert a breakpoint and walk it
through, the error appears to be occuring is in the BoldUnderText subroutine.
When I get to that routine, and mouse over the first "Selection" line it
displays the message.
Would appreciate any help you can provide.
Public Sub Word_Doc()
Dim strSQL As String
Dim rs As dao.Recordset
Dim appWord As Word.Application
Dim wdDoc As Word.Document
Dim bWordWasOpen As Boolean
On Error GoTo ProcError
bWordWasOpen = True
Set appWord = GetObject(, "Word.Application")
appWord.Visible = True
Set wdDoc = appWord.Documents.Add(, , , True)
strSQL = "SELECT * FROM myQuery"
Set rs = CurrentDb.OpenRecordset(strSQL, , dbFailOnError)
While Not rs.EOF
With wdDoc
Call BoldUnderText(wdDoc, "Title:")
Selection.TypeText Text:=" " & rs("Field1")
Selection.TypeParagraph
Call BoldUnderText(wdDoc, "Statement:")
Selection.TypeText Text:=" " & rs("Field2")
Selection.TypeParagraph
rs.MoveNext
If Not rs.EOF Then Selection.InsertBreak Type:=wdPageBreak
End With
Wend
ProcExit:
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Set wdDoc = Nothing
Set appWord = Nothing
Exit Sub
ProcError:
If Err.Number = 429 Then
bWordWasOpen = False
Set appWord = CreateObject("Word.Application")
Resume Next
Else
Debug.Print Err.Number, Err.Description
MsgBox Err.Number & vbCrLf & Err.Description
Resume ProcExit
End If
End Sub
Private Sub BoldUnderText(ByRef wdDoc As Word.Document, TextToBoldUnder As
Variant)
With wdDoc
Selection.Font.Bold = wdToggle
Selection.Font.UnderlineColor = wdColorAutomatic
Selection.Font.Underline = wdUnderlineSingle
Selection.TypeText Text:=TextToBoldUnder
Selection.Font.Bold = wdToggle
Selection.Font.UnderlineColor = wdColorAutomatic
Selection.Font.Underline = wdUnderlineNone
End With
End Sub