Hi,
Can anyone tell me why this code is not working.
I try to run this code. It send data to MS Word, but the second times I run it, I get a error message: ' The remote server machine does not exist or is unavailable (Error 462)
It stops in this segment: .Rows(1).Select
Thanks in advance
Here is the code:
Private Sub WordRapport_Click()
Dim objWord As Word.Application
Dim rst As Recordset
Dim strSQL As String
Set objWord = New Word.Application
objWord.Documents.Add Application.CurrentProject.Path & "\Item.dot"
objWord.Visible = True
strSQL = "SELECT tbl_Item.Item1, tbl_Item.Item2, tbl_Item.Item3 FROM tbl_Item;"
Set rst = New Recordset
rst.Open strSQL, CurrentProject.Connection
With CreateTableFromRecordset(objWord.ActiveDocument.Bookmarks("Mandag").Range, rst, True)
' The remote server machine does not exist or is unavailable (Error 462)
.Rows(1).Select
With Selection.Font
.Name = "Arial"
.Size = 12
.Bold = True
.AllCaps = True
.Color = wdColorBlue
End With
End With
rst.Close
Set objWord = Nothing
Set rst = Nothing
End Sub
Function CreateTableFromRecordset(rngAny As Word.Range, rstAny As ADODB.Recordset, Optional fIncludeFieldNames As Boolean = False) As Word.Table
Dim objTable As Word.Table
Dim fldAny As ADODB.Field
Dim varData As Variant
Dim strBookmark As String
Dim cField As Long
varData = rstAny.GetString()
With rngAny
.InsertAfter varData
Set objTable = .ConvertToTable(Separator:=wdSeparateByTabs)
If fIncludeFieldNames Then
With objTable
.Rows.Add(.Rows(1)).HeadingFormat = True
For Each fldAny In rstAny.Fields
cField = cField + 1
.Cell(1, cField).Range.Text = fldAny.Name
Next
End With
End If
End With
Set CreateTableFromRecordset = objTable
End Function