Link to home
Start Free TrialLog in
Avatar of DaliHow
DaliHow

asked on

Word Application Object

Hello,

I am trying to preview a Word document from a VB6 application.  I have added a reference to the Microsoft Word 9.0 Object Library, and created a sub called PreviewReport.  I am using Word 2000 running on a WindowsXP machine.  I am also doing the application development on WindowsXP.

The code runs without errors, and I can see Word load and unload in the Windows Task Manager as the sub executes.  However, the PrintPreview command simple does nothing.  When I step through the code, it passes right over it as if it is executing, but the document does not display.

Can anyone tell me what I am doing wrong?

The following code is what I have for my sub...

Private Sub PreviewReport

    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Dim wrdSel As Word.Selection

    Set wrdApp = New Word.Application
    Set wrdDoc = wrdApp.Documents.Add
    Set wrdSel = wrdApp.Selection

    wrdApp.Documents.Open ("D:\Test.doc")
    wrdDoc.PrintPreview

    Set wrdSel = Nothing
    Set wrdDoc = Nothing
    wrdApp.Visible = True
    wrdApp.Quit
    Set ardApp = nothing

End Sub

Thanks!
Dave
ASKER CERTIFIED SOLUTION
Avatar of JohnMcCann
JohnMcCann

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi DaliHow,

You don't set wrdApp.Visible=True until after all the processing has happened. You need to set the object visible at the beginning. I would also suggest that you only close and set the object to nothing in another call as VB will continue to process the commands which will do this so you will still not see much of the preview.

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
Avatar of JohnMcCann
JohnMcCann

Private Sub PreviewReport

   Dim wrdApp As Word.Application
   Dim wrdDoc As Word.Document
   Dim wrdSel As Word.Selection

   Set wrdApp = New Word.Application
   Set wrdDoc = wrdApp.Documents.Add
   Set wrdSel = wrdApp.Selection

   wrdApp.Documents.Open ("D:\Test.doc")
   wrdDoc.PrintPreview
   wrdApp.Visible = True

   Set wrdSel = Nothing
   Set wrdDoc = Nothing
   wrdApp.Quit
   Set ardApp = nothing

End Sub
Dear DaliHow,

I have tried the following code with Word 8.0.

   Dim wrdApp As Word.Application
   Dim wrdDoc As Word.Document

   Set wrdApp = New Word.Application

   wrdApp.Visible = True
   Set wrdDoc = wrdApp.Documents.Open("d:\test.doc")
   wrdDoc.PrintPreview

   'Preview is available
   'as long as next statements are NOT executed
   'because word will be closed
   'Tested with Word 8.0

   Set wrdDoc = Nothing
   wrdApp.Quit
   Set wrdApp = Nothing


Hope this helps.
Hi, %%QUESTIONER%%

Indeed Marcel, this is as I said earlier, VB will continue to execute those statements so the preview cannot be seen properly. The only way to display a preview using this method is to close the word instance from a seperate function.

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member