none
Open word in panel vb.net RRS feed

  • Question

  •  

    please, i only want to open any office app into panel using vb.net

    please help

    thnks

    my code not work

     Dim pathword As String = "C:\Users\Sixsigma\Desktop\Shareproject_code\Pathfile\test.docx"
            oWord = New Word.Application
            oWord.Application.DisplayAlerts = False
            oWord = CreateObject("Word.Application")
            '  oWord.WindowState = Word.WdWindowState.wdWindowStateMaximize
            oDoc = oWord.Documents.Open(pathword)
            SetParent(oWord.hwnd, Panel1.Handle)
            SendMessage(oWord.Hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
            oWord.Visible = True

    Wednesday, November 14, 2018 5:49 AM

Answers

  • my code 

    Imports Microsoft.Office.Interop
    Imports System.Runtime.InteropServices
    Public Class Form3
     <DllImport("user32.dll", CharSet:=CharSet.Auto)>
        Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInt32, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
        End Function
        Private Const WM_SYSCOMMAND As Integer = &H112
        Private Const SC_MINIMIZE As Integer = &HF020
        Private Const SC_MAXIMIZE As Integer = &HF030
        Public Structure RECT 
            Dim Left As Integer
            Dim Top As Integer
            Dim Right As Integer
            Dim Bottom As Integer
        End Structure
        Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
        Declare Function GetWindowRect Lib "user32" (ByVal Hwnd As Integer, ByRef lpRect As RECT) As Integer
        Declare Function MoveWindow Lib "user32" (ByVal Hwnd As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Integer) As Integer
        Declare Function SetParent Lib "user32" (ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer
    
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        
            Dim pathword As String = "C:\Users\Sixsigma\Desktop\Shareproject_code\Pathfile\test.docx"
            Dim mwrdHwnd As Integer      
            oWord = New Word.Application
            oWord.Application.DisplayAlerts = False 
           oDoc = oWord.Documents.Open(pathword)
            mwrdHwnd = FindWindow("OpusApp", oDoc.ActiveWindow.Caption & " - " & oWord.Caption)
            
            SetParent(mwrdHwnd, Panel1.Handle)
            SendMessage(mwrdHwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    
            oWord.Visible = True
    End Sub
    End Class

    Friday, November 16, 2018 9:35 AM

All replies

  • Hello,

    Try this hope it will help you

    You have to add Microsoft.Office.Interop.Word.dll as reference in your project in order to open word files in panel

    Imports System.Runtime.InteropServices
    Imports Microsoft.Office.Interop
    
    Public Class Form19
        Private xlsFileName As String = "E:\SampleCert.docx"
        Private m_workbook As Word.Document
        Const WM_SYSCOMMAND As Integer = 274
        Const SC_MAXIMIZE As Integer = 61488
        Private proc As Process
        <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
        Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInt32, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
        End Function
        <DllImport("user32.dll", SetLastError:=True)> _
        Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
        End Function
        Private Sub Form19_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Try
                proc = Process.Start(xlsFileName)
                proc.WaitForInputIdle()
                SetParent(proc.MainWindowHandle, Me.Panel1.Handle)
                SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try
        End Sub
    End Class

    Wednesday, November 14, 2018 7:00 AM
  • thank you. but not work.

    what word  version.

    Wednesday, November 14, 2018 7:11 AM
  • Whats the error ?
    Wednesday, November 14, 2018 7:15 AM
  • The usual way is with the WebBrowser control, like in the KB243058

    (tested on Windows 10 with Office 2016)

    (+ KB927009 for registry)

    • Edited by Castorix31 Wednesday, November 14, 2018 7:22 AM
    Wednesday, November 14, 2018 7:19 AM
  • not error. but not embed in panel .
    Wednesday, November 14, 2018 7:23 AM
  • i'm try it.
    Wednesday, November 14, 2018 7:29 AM
  • I haves tested with windows 10 64 bit , Office.Interop version 11.0.0.0

    whats your enviorment for testing ? let me know so thatI can check on that enviorment 

    Wednesday, November 14, 2018 7:34 AM
  • I test with wordpad it work. but my boss him need open microsoft word in panel.

    thank you .

    Wednesday, November 14, 2018 7:59 AM
  • I'm sorry.English is not my main language.If you misunderstand me.
    Wednesday, November 14, 2018 8:04 AM
  • Hello,

    Checked with Microsoft word too when I go to properties of my .docx file and change Open with program to Microsoft word It works as expected

    Wednesday, November 14, 2018 10:13 AM
  • Are you using process function .

    what your version microsoft office?

    thank you.

    Thursday, November 15, 2018 3:41 AM
  • i can't use version 2016 . because my client wants to use 2013
    Thursday, November 15, 2018 3:45 AM
  • I have tested with Microsoft office 2007.

    Are you using process function .? Using same code as posted above

    Thursday, November 15, 2018 5:10 AM
  • Yes i do.I try it process function . But not work on Microsoft office 2013.

    and i try use on excel  and powerpoint. And it's availible.

    Thursday, November 15, 2018 6:42 AM
  • Means Excel and power point is working at your end whereas word is not ?
    Thursday, November 15, 2018 6:52 AM
  • Yes i do.I try it process function . But not work on Microsoft office 2013.

    and i try use on excel  and powerpoint. And it's availible.

    Hi,

    You can try to add the code:

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Try
                proc = Process.Start(xlsFileName)
                proc.WaitForInputIdle()
                Threading.Thread.Sleep(4000)
                SetParent(proc.MainWindowHandle, Me.Panel1.Handle)
                SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
                'Me.Panel1.
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try
    
        End Sub
    If I don't add this code, it seems to have the same problem as you.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 15, 2018 7:44 AM
  • Thank you.

    I try it. It's avalible on version 2007. but i can't controls it . 

    example.

    hide toolbar,print,select page .

    Thursday, November 15, 2018 8:20 AM
  • Yes I cant hide toolbar from Microsoft word when open in toolbar but I can minimize the ribbon have you tried ?

    Else What can we do is we can Open Microsoft word make necessary toolbar settings then from next time we can use from our application with that settings

    Thursday, November 15, 2018 9:16 AM
  • Thank you.

    I try it. It's avalible on version 2007. but i can't controls it . 

    example.

    hide toolbar,print,select page .

    As already said, use the WebBrowser control with the right BrowserFlags in the registry

    This is the recommended method from MS (since 2004)

    OLECMDID_HIDETOOLBARS to hide Toolbars

    Thursday, November 15, 2018 9:58 AM
  • Thank you everyone.

    I do it success.

    Friday, November 16, 2018 9:29 AM
  • my code 

    Imports Microsoft.Office.Interop
    Imports System.Runtime.InteropServices
    Public Class Form3
     <DllImport("user32.dll", CharSet:=CharSet.Auto)>
        Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInt32, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
        End Function
        Private Const WM_SYSCOMMAND As Integer = &H112
        Private Const SC_MINIMIZE As Integer = &HF020
        Private Const SC_MAXIMIZE As Integer = &HF030
        Public Structure RECT 
            Dim Left As Integer
            Dim Top As Integer
            Dim Right As Integer
            Dim Bottom As Integer
        End Structure
        Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
        Declare Function GetWindowRect Lib "user32" (ByVal Hwnd As Integer, ByRef lpRect As RECT) As Integer
        Declare Function MoveWindow Lib "user32" (ByVal Hwnd As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Integer) As Integer
        Declare Function SetParent Lib "user32" (ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer
    
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        
            Dim pathword As String = "C:\Users\Sixsigma\Desktop\Shareproject_code\Pathfile\test.docx"
            Dim mwrdHwnd As Integer      
            oWord = New Word.Application
            oWord.Application.DisplayAlerts = False 
           oDoc = oWord.Documents.Open(pathword)
            mwrdHwnd = FindWindow("OpusApp", oDoc.ActiveWindow.Caption & " - " & oWord.Caption)
            
            SetParent(mwrdHwnd, Panel1.Handle)
            SendMessage(mwrdHwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    
            oWord.Visible = True
    End Sub
    End Class

    Friday, November 16, 2018 9:35 AM
  • Hi,

    We appreciate that you shared your solution with us. Please mark the post(s) that helped you as the answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 16, 2018 9:47 AM
  • thank alex.
    Sunday, November 18, 2018 10:03 AM
  • Hi,
     
    Please find below link and refer the 2nd example.
     
    https://social.msdn.microsoft.com/Forums/en-US/484659e0-024e-403d-a728-3b40940a4506/open-pdfword-file-on-button-click-vbnet?forum=Vsexpressvb 

    Monday, November 19, 2018 4:13 AM