VB6: MsWord PrintPreview in my Application form.

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

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

Question by:2Angel
  • 5
  • 4
LVL 12

Expert Comment

ID: 13835073
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.TypeText Text:="Hopefully it works!"

Author Comment

ID: 13835616

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

ID: 13835705
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?
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 13837378
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

ID: 13841205
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

Author Comment

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

Thaks again...
LVL 12

Expert Comment

ID: 13841454
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

gbzhhu earned 200 total points
ID: 13843318
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


Author Comment

ID: 13879337
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!

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Suggested Courses

864 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