I’m trying to use Word from within Visual Basic to print a document. Everything works perfectly the first time I print but all subsequent attempts to print fail. The sequence of events is as follows:
First print: everything is fine.
Second print: Run-time Error 462 – The remote server machine does not exist or is unavailable.
Debug takes me to the line of code that sets up a table in the word document (ie .ActiveDocument.Tables.Add......
I have no idea what this would mean. I have attached my code to take a look at. Thanks in advance,
On Error GoTo ErrorHandler
Dim WordObject As Object
Set WordObject = CreateObject("Word.Application")
Dim NumRows As Integer
Dim myTable As Table
Dim X As Integer
Dim Y As Integer
MousePointer = vbHourglass
Printing.Show vbModeless
With WordObject
.Documents.Open ("M:\VB_Projects\AH-OOS\Reports\Violations")
.Visible = False
.ActiveDocument.Bookmarks("txtName").Select
.Selection.Text = (TxtFirstName & " " & txtMI & Space(Len(txtMI)) & TxtLastName)
.ActiveDocument.Bookmarks("txtDln").Select
.Selection.Text = UCase(medDLNo)
.ActiveDocument.Bookmarks("txtEligibilityDate").Select
.Selection.Text = Format(medEligibilityDate, "mm/dd/yyyy")
'place the cursor where the table is to start in word doc
.ActiveDocument.Bookmarks("txtStartTable").Select
'get number of rows for table
NumRows = (lstViolations.ListCount)
'create table
Set myTable = ActiveDocument.Tables.Add(Selection.Range, NumRows, 4)
With myTable
.Borders.Enable = False
For X = 1 To NumRows
For Y = 1 To 4 'columns
'set column sizes and populate tehm
If Y = 1 Then
.Cell(X, Y).Column.Width = InchesToPoints(3)
.Cell(X, Y).Range.InsertAfter Left(lstViolations.List(X - 1), 22)
ElseIf Y = 2 Then
.Cell(X, Y).Column.Width = InchesToPoints(1.5)
.Cell(X, Y).Range.InsertAfter Mid(lstViolations.List(X - 1), 23, 12)
ElseIf Y = 3 Then
.Cell(X, Y).Column.Width = InchesToPoints(1.4)
.Cell(X, Y).Range.InsertAfter Mid(lstViolations.List(X - 1), 34, 12)
ElseIf Y = 4 Then
.Cell(X, Y).Column.Width = InchesToPoints(0.5)
.Cell(X, Y).Range.InsertAfter Mid(lstViolations.List(X - 1), 47, 2)
End If
Next Y
Next X
End With
.ActiveDocument.PrintOut
.ActiveDocument.Close (0)
Set WordObject = Nothing
.Application.Quit
End With
Printing.Hide
MousePointer = vbDefault
Exit Sub '........exit if no errors
ErrorHandler:
With WordObject
.ActiveDocument.Close (0)
.Application.Quit
End With
Set WordObject = Nothing
Printing.Hide
MousePointer = vbDefault
MsgBox Err.Description
First print: everything is fine.
Second print: Run-time Error 462 – The remote server machine does not exist or is unavailable.
Debug takes me to the line of code that sets up a table in the word document (ie .ActiveDocument.Tables.Add......
I have no idea what this would mean. I have attached my code to take a look at. Thanks in advance,
On Error GoTo ErrorHandler
Dim WordObject As Object
Set WordObject = CreateObject("Word.Application")
Dim NumRows As Integer
Dim myTable As Table
Dim X As Integer
Dim Y As Integer
MousePointer = vbHourglass
Printing.Show vbModeless
With WordObject
.Documents.Open ("M:\VB_Projects\AH-OOS\Reports\Violations")
.Visible = False
.ActiveDocument.Bookmarks("txtName").Select
.Selection.Text = (TxtFirstName & " " & txtMI & Space(Len(txtMI)) & TxtLastName)
.ActiveDocument.Bookmarks("txtDln").Select
.Selection.Text = UCase(medDLNo)
.ActiveDocument.Bookmarks("txtEligibilityDate").Select
.Selection.Text = Format(medEligibilityDate, "mm/dd/yyyy")
'place the cursor where the table is to start in word doc
.ActiveDocument.Bookmarks("txtStartTable").Select
'get number of rows for table
NumRows = (lstViolations.ListCount)
'create table
Set myTable = ActiveDocument.Tables.Add(Selection.Range, NumRows, 4)
With myTable
.Borders.Enable = False
For X = 1 To NumRows
For Y = 1 To 4 'columns
'set column sizes and populate tehm
If Y = 1 Then
.Cell(X, Y).Column.Width = InchesToPoints(3)
.Cell(X, Y).Range.InsertAfter Left(lstViolations.List(X - 1), 22)
ElseIf Y = 2 Then
.Cell(X, Y).Column.Width = InchesToPoints(1.5)
.Cell(X, Y).Range.InsertAfter Mid(lstViolations.List(X - 1), 23, 12)
ElseIf Y = 3 Then
.Cell(X, Y).Column.Width = InchesToPoints(1.4)
.Cell(X, Y).Range.InsertAfter Mid(lstViolations.List(X - 1), 34, 12)
ElseIf Y = 4 Then
.Cell(X, Y).Column.Width = InchesToPoints(0.5)
.Cell(X, Y).Range.InsertAfter Mid(lstViolations.List(X - 1), 47, 2)
End If
Next Y
Next X
End With
.ActiveDocument.PrintOut
.ActiveDocument.Close (0)
Set WordObject = Nothing
.Application.Quit
End With
Printing.Hide
MousePointer = vbDefault
Exit Sub '........exit if no errors
ErrorHandler:
With WordObject
.ActiveDocument.Close (0)
.Application.Quit
End With
Set WordObject = Nothing
Printing.Hide
MousePointer = vbDefault
MsgBox Err.Description