Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem using Word to from within Visual Basic

Status
Not open for further replies.

catalina36

Programmer
Aug 28, 2000
36
0
0
US
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.
Third & Subsequent prints: Run-time Error 5850 – The specified range is not from the correct document or story.

The two things that I noticed with Error 5850 is that 1) there are multiple instances of Winword open and 2) Debug takes me to the line of code that sets up a table in the word document (ie .ActiveDocument.Tables.Add NumRows:=1, NumColumns:=3, Range:=Selection.Range).

Can anybody help me fix this problem?

TIA
[sig][/sig]
 

Can you post the related part of the code?
[sig]<p> Tarek<br><a href= > </a><br>The more I learn, the more I need to learn![/sig]
 
The following is a highly abbreviated version of the code that is causing the problems. Specifically, debug takes me to .ActiveDocument.Tables.Add… when I incur an error 5850.

Private Sub mnuInvoicesPrint_Click()

Dim wdApp As Word.Application
Dim wdDoc As Word.Document

Set wdApp = New Word.Application
Set wdDoc = wdApp.Documents.Add

With wdApp
.Visible = False

'general setup
.Selection.PageSetup.TopMargin = InchesToPoints(0.5)
.Selection.PageSetup.BottomMargin = InchesToPoints(0.5)
.Selection.PageSetup.LeftMargin = InchesToPoints(1)
.Selection.PageSetup.RightMargin = InchesToPoints(1)
.Selection.ParagraphFormat.TabStops.ClearAll
.ActiveDocument.DefaultTabStop = InchesToPoints(1)
.
.
.
.ActiveDocument.Tables.Add NumRows:=1, NumColumns:=3, Range:=Selection.Range
.Selection.Cells.SetWidth ColumnWidth:=InchesToPoints(4.58), RulerStyle:=wdAdjustNone
.Selection.MoveRight unit:=wdCell, Count:=1
.Selection.Cells.SetWidth ColumnWidth:=InchesToPoints(1.06), RulerStyle:=wdAdjustNone
.Selection.MoveRight unit:=wdCell, Count:=1
.Selection.Cells.SetWidth ColumnWidth:=InchesToPoints(1.01), RulerStyle:=wdAdjustNone
.Selection.MoveLeft unit:=wdCell, Count:=2
.
.
.
.Application.PrintOut FileName:=&quot;&quot;, Range:=wdPrintAllDocument, _
Item:=wdPrintDocumentContent, Copies:=1, pages:=&quot;&quot;, _
pagetype:=wdPrintAllPages, collate:=True, _
Background:=False, printtofile:=False

.ActiveWindow.Close savechanges:=False
End With

wdApp.Application.Quit

Set wdDoc = Nothing
Set wdApp = Nothing


Have fun!
[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top