Home » Category » DLL & COM & ActiveX

DLL & COM & ActiveX: Word2000 from VB6

104| Sat, 01 Dec 2007 01:29:00 GMT| billcrawley| Comments (1)
Hi All,

I'm trying to use word to print a document from VB. Everything works fine until I get to the bit that needs to close the word object. I've surfed the net and found other articles and basically I seem to be doing everything correct, but obviously there is somthing not quite right. You see in the code below, once I come out of the while loop, it errors with an automation error. each time I try and go in the document count has also increased by one. So somehow once word loads and then the user closes word VB has pointers referencing Dead objects. still. This has been driving me crazy for Days now. Could someone please have a look at the code below and tell me what I'm doing wrong. The report itself is fine.

Public Sub GenericTablePrint(ByVal rcsIn As ADODB.Recordset, ByVal Orientation As WdOrientation, ByVal Title As String)
Dim msword As Word.Application
Dim doc As Document
Dim myTable As Table
Dim i As Integer
Dim x As Integer
Dim Ret As Long
Dim WordHwnd As Long
Dim DocHwnd As Long
Const pstfName As String = "GenericTablePrint"

On Error GoTo GenericTablePrintErr

Set msword = CreateObject("Word.Application")
Set doc = New Document
Set doc = msword.Documents.Add(, , , True)

'msword.Documents.Open "C:\temp.doc"

WordHwnd = FindWindowEx(0&, 0&, "OpusApp", vbNullString)
DocHwnd = FindWindowEx(WordHwnd, 0&, "_WwG", vbNullString)
'set the page details
With ActiveDocument.PageSetup
.Orientation = Orientation
.LeftMargin = 2 'want as much of the page as poss
.RightMargin = 2
End With
'format the Document Header
With Selection
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.BoldRun
.TypeText Text:=Title
.BoldRun
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.TypeParagraph
.TypeParagraph
End With
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=rcsIn.RecordCount + 1, NumColumns:= _
rcsIn.Fields.Count - 1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed

' Set myTable = doc.Tables.Add(dRange, rcsIn.RecordCount + 1, rcsIn.Fields.Count - 1)
'Set Header
For i = 0 To rcsIn.Fields.Count - 1
With ActiveDocument.Tables.Item(1)
.Cell(1, i).Range.Bold = True
.Cell(1, i).Range.Font.Name = "MS Serif"
.Cell(1, i).Range.Font.Size = 6
.Cell(1, i).Range.Text = rcsIn.Fields(i).Name
End With
Next
ActiveDocument.Tables.Item(1).Rows(1).HeadingFormat = True 'this means that Headings will carry over on mutli-page documents
'set detail
For i = 0 To rcsIn.RecordCount - 1 'rows
For x = 0 To rcsIn.Fields.Count - 1
With ActiveDocument.Tables.Item(1)
.Cell(i + 2, x).Range.Bold = False
.Cell(i + 2, x).Range.Font.Name = "TimesRoman"
.Cell(i + 2, x).Range.Font.Size = 8
.Cell(i + 2, x).Range.Text = rcsIn.Fields(x).Value
End With
Next
rcsIn.MoveNext
Next
rcsIn.MoveFirst
msword.ActiveDocument.PrintPreview

msword.Visible = True
' Do While Not ActiveDocument.Close
' Loop
' Do While DocHwnd <> 0
' Ret = WaitForSingleObject(DocHwnd, INFINITE)
' Loop
While msword.Application.PrintPreview = True
Wend
' Makes Word InVisible
' doc.Close

Set doc = Nothing
Set msword = Nothing
Exit Sub
GenericTablePrintErr:
ErrorHand.GetError Err.Number, mstfName & ":" & pstfName, Err.Description
Set doc = Nothing
Set msword = Nothing
End Sub

Regards
Bill Crawley

Keywords & Tags: word2000, vb6, dll, com, activex, visual basic

URL: http://programming.itags.org/com-activex/174565/
 
«« Prev - Next »» 1 helpful answers below.
No,

When I load word it doesn't automatically open a DOC, well my code doesn't if I leave out the 'Doc' Variable. I have however removed the first line as per your suggestion. This hasn't made any difference though.

Any more suggestions would be appreciated.

Regards
Bill Crawley

billcrawley | Wed, 05 Dec 2007 13:35:00 GMT |

DLL & COM & ActiveX Hot Answers

DLL & COM & ActiveX New questions

DLL & COM & ActiveX Related Categories