VB6: MsWord PrintPreview in my Application form.

Posted on 2005-04-21
Last Modified: 2012-06-27

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

Question by:2Angel
    LVL 12

    Expert Comment

    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\", NewTemplate:= False
       'Change the path above to point to where your is

        Selection.TypeText Text:="Let's preview this"
        Selection.TypeText Text:="Hopefully it works!"
    LVL 2

    Author Comment


    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

    LVL 12

    Expert Comment

    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?
    LVL 2

    Author Comment

    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....
    LVL 12

    Expert Comment

    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
    LVL 2

    Author Comment

    Thak you (I dont have a lots of points to give....)
    I use Office2000

    Thaks again...
    LVL 12

    Expert Comment

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

    LVL 12

    Accepted Solution

    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
            m_appWord.ActiveWindow.View.Type = wdPageView
        End If
        ' size page fit full to start with
        m_appWord.ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitFullPage
        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
                   lBarTest = False
                End If

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

        openDocument2000 = True
        Exit Function

        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
            ' 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

    LVL 2

    Author Comment

    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!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now