VB6: MsWord PrintPreview in my Application form.

Hi,

Can I run the MsWord PrintPreview in my VB Application form?


Tks!
LVL 2
2AngelAsked:
Who is Participating?
 
gbzhhuConnect With a Mentor Commented:
Add this to a form (assumed form is called form1)

Option Explicit

Private Sub Command1_Click()
    openDocument2000 "D:\test.doc", Me.Frame1.hwnd
End Sub

Private Function openDocument2000(sDocPath As String, lTargetHwnd As Long) As Boolean
    Dim m_appWord As Word.Application
    Dim aDoc As Document
    Dim lWordWnd As Long
    Dim lDocWnd As Long
    Dim defParentHwnd As Long
   
    On Error GoTo proc_err
   
    Set m_appWord = New Word.Application
       
    lWordWnd = FindWindow("OpusApp", m_appWord.Caption)
   
    Set aDoc = m_appWord.Documents.Open(sDocPath, AddToRecentFiles:=False, ReadOnly:=True)
    m_appWord.ActiveWindow.WindowState = wdWindowStateMaximize
    m_appWord.ActiveWindow.DisplayRulers = False
    m_appWord.ActiveWindow.HorizontalPercentScrolled = 0
   
    ' make document view
    If m_appWord.ActiveWindow.View.SplitSpecial = wdPaneNone Then
        m_appWord.ActiveWindow.ActivePane.View.Type = wdPageView
    Else
        m_appWord.ActiveWindow.View.Type = wdPageView
    End If
   
    ' size page fit full to start with
    m_appWord.ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitFullPage
   
    'Preview
    m_appWord.ActiveDocument.PrintPreview
    m_appWord.Visible = True
   
    lDocWnd = findbyCaption("??", lWordWnd)

    BringWindowToTop lDocWnd
    SetParent lDocWnd, lTargetHwnd


    If lDocWnd <> 0 Then
        'Disable the page view buttons on the bottom left corner
        Dim lMSOBarHwnd As Long
        Dim lBarTest As Boolean
        lMSOBarHwnd = 0
        lBarTest = True
        While lBarTest
            lMSOBarHwnd = FindWindowEx(lDocWnd, lMSOBarHwnd, "MsoCommandBar", vbNullString)

            If lMSOBarHwnd > 0 Then
               EnableWindow lMSOBarHwnd, False
            Else
               lBarTest = False
            End If
        Wend

        ' make the frame the new documents parent
        defParentHwnd = SetParent(lDocWnd, Form1.Frame1.hwnd)
    Else
        MsgBox "Failed to find word window"
    End If

    openDocument2000 = True
proc_exit:
    Exit Function

proc_err:
    openDocument2000 = False
    GoTo proc_exit
End Function

===================================

Add this to a module

Option Explicit

Private Declare Function EnumChildWindows Lib "user32" _
(ByVal lparentwnd As Long, _
   ByVal lpEnumFunc As Long, _
   ByVal lParam As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" _
    Alias "GetWindowTextLengthA" _
   (ByVal hwnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" _
    Alias "GetWindowTextA" _
   (ByVal hwnd As Long, _
    ByVal lpString As String, _
    ByVal cch As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Public Declare Function EnableWindow Lib "user32" _
(ByVal hwnd As Long, ByVal fEnable As Long) As Long

Public Declare Function SetParent Lib "user32" _
(ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

Public Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Private m_TargetString As String
Private m_TargetHwnd As Long

Public Function findbyCaption(ByVal _
    target_string As String, ByVal parentHwnd As Long) As Long
    Dim result As Long
    Dim myHwnd As Long
    m_TargetString = target_string
    m_TargetHwnd = 0

    ' Enumerate the child windows and use EnumCallback to check each window's title.
    Call EnumChildWindows(parentHwnd, AddressOf EnumCallback, 0)
     
    ' Return the hWnd found (if any).
    findbyCaption = m_TargetHwnd
   
End Function
   
Public Function EnumCallback(ByVal app_hWnd As Long, ByVal _
    param As Long) As Long
Dim buf As String
Dim title As String
Dim length As Long
On Error Resume Next

    ' Get the window's title.
    length = GetWindowTextLength(app_hWnd)
    buf = Space$(length)
    length = GetWindowText(app_hWnd, buf, length + 1)
    title = Left$(buf, length)
   
Debug.Print title

    ' See if window's title is the one we require
    If title = m_TargetString Then
        ' This is the one we want.
        m_TargetHwnd = app_hWnd

        ' Stop searching.
        EnumCallback = 0
    Else
        ' Continue searching.
        EnumCallback = 1
    End If
End Function


This is your start, you will probably need to tweak the positioning of the preview and its size, use something like

m_appWord.ActiveWindow.ActivePane.View.Zoom.Percentage = 130  ' or a different number

If you close Word you will lose the preview, but if you hide it you should still have the preview



0
 
gbzhhuCommented:
yeah you can.

reference microsoft word (whatever version)

   dim wordApp as Word.Application

   set  wordApp = new word.application
   wordApp.visible = true
   wordApp.Documents.Add Template:= "C:\Program Files\Microsoft Office\Templates\Normal.dot", NewTemplate:= False
   'Change the path above to point to where your Normal.dot is

    Selection.TypeText Text:="Let's preview this"
    Selection.TypeParagraph
    Selection.TypeText Text:="Hopefully it works!"
    ActiveDocument.PrintPreview
0
 
2AngelAuthor Commented:

Thank you very much for your comment.

Well, this I alredy knew....
I want to "make" a PrintPreview on my own from and not to command it to be open on the MsWord document him self....

Any idea

Tks
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
gbzhhuCommented:
This can still be done if you are preview only word documents.  Once you preview in word (hide word) , grap the the internal document window (which is showing the preview), stick that into your form.  Is that acceptable?
0
 
2AngelAuthor Commented:
Yes, but pls show me how to "grap" it and to "stick" it to my form. Also, if I hide the document the preview is gon....
0
 
gbzhhuCommented:
You are asking alot for 50 points my friend ;-) but I'll try to help

What version of Word are you using?

And no if you hide the preview will not be gone, I'll show you how
0
 
2AngelAuthor Commented:
Thak you (I dont have a lots of points to give....)
I use Office2000

Thaks again...
0
 
gbzhhuCommented:
OK, I am just in the middle of something.  I'll post the code after lunch an hour and half or so.

Cheers
0
 
2AngelAuthor Commented:
Hi,
Thanks for posting the code...
I couldn't make it to work, but anyhow you've got the points even just for the eforts.

Thank you very much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.