How to load the .docx to word doc, also make me to understand the code(black magic)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: How to load the .docx to word doc, also make me to understand the code(black magic)

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Posts
    23

    How to load the .docx to word doc, also make me to understand the code(black magic)

    Hi All,

    I want some help from you people, please help me.

    I have created a new program, in which i have added the below code from another program which is written by other person, who has left the job….. I am not at all able understand this piece of code… also the person has written it as BLACK Magic for that code above u cn find it below..



    But this code is loading the selected .docx file ….. How its doing, can any one plz plz plz help me…

    Code:
    Imports Microsoft.Office.Interop.Word 
    Imports Microsoft.Office.Interop.Excel 
      
    Imports Microsoft.Office.Interop 
      
    Public Class Word_doc_Export 
        Private WithEvents oWord As Word.Application 
        Private WithEvents oDoc As Word.Document 
        Dim Loading As Boolean 
        Dim ExperimentMode As String '"WordActivateMode" or "FakeHighlightMode" 
        Const FakeHighlightMode = "FakeHighlightMode" 
        Const WordActivateMode = "WordActivateMode" 
        Dim PreviousHighlightColorIndex As Word.WdColorIndex 
        Dim WorkingSourceFileName As String = "" 
        Public FileName As String 
        Dim WordHandle As Integer 
      
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer 
        Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer 
        Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer 
        Private Declare Function MoveWindow Lib "user32" Alias "MoveWindow" (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 
      
        Dim WordVersion As String 
      
        Private Sub Word_doc_Export_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
            oWord = New Word.Application 
            WorkingSourceFileName = " C:\Users\aseema\Desktop\zzz.docx" 
        End Sub 
      
        Private Sub frmRA2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated 
      
            If Not Loading Then 
                If Not oWord Is Nothing Then 
                    Try 
                        If oWord.Selection.Range.HighlightColorIndex = Word.WdColorIndex.wdNoHighlight Then 
                            If ExperimentMode = FakeHighlightMode Then 
                                oWord.Selection.Range.HighlightColorIndex = Word.WdColorIndex.wdGray25 
                            End If 
                        End If 
                    Catch ex As Exception 
                    End Try 
                End If 
            End If 
      
        End Sub 
      
        Private Sub frmRA2_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Deactivate 
      
            If Not Loading Then 
                If Not oWord Is Nothing Then 
                    If ExperimentMode = FakeHighlightMode Then 
                        If oWord.Selection.Range.HighlightColorIndex = Word.WdColorIndex.wdGray25 Then 
                            'oWord.Selection.Range.HighlightColorIndex = Word.WdColorIndex.wdNoHighlight 
                            If PreviousHighlightColorIndex <> 9999999 Then 
                                oWord.Selection.Range.HighlightColorIndex = PreviousHighlightColorIndex 
                            End If 
                        End If 
                    End If 
                End If 
            End If 
      
        End Sub 
      
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
            Dim sv As New FarPoint.Win.Spread.SheetView 
            Dim i As Integer 
            Dim j As Integer 
            Dim k As Integer 
            Dim sTemp As String 
            Dim chkbox As New FarPoint.Win.Spread.CellType.CheckBoxCellType 
            Dim iParent As Integer 
            Dim FileNameWithoutPath As String 
            Dim ServiceCategoryName As String 
      
             
            Loading = True 
      
            FileNameWithoutPath = System.IO.Path.GetFileName(FileName) 
      
            grd.Visible = False 
            Panel1.Dock = DockStyle.Fill 
            pnlWordContainer.Dock = DockStyle.Fill 
            pnlWordContainer.Visible = False 
      
      
            '****************************************************** 
            'Black Magic 
            '****************************************************** 
            oWord = New Word.Application 
      
            oWord.Caption = "SIM" 
            WordHandle = FindWindow("Opusapp", "SIM") 
            SetParent(WordHandle, Me.pnlWordContainer.Handle.ToInt32) 
            oWord.Caption = Nothing 
            My.Application.DoEvents() 
      
             
            oDoc = oWord.Documents.Open(FileName, False) 
            'FileOpen(1, FileName, OpenMode.Append, OpenAccess.Default, OpenShare.Default, -1) 
      
            oDoc.Application.Visible = True 
            oDoc.Application.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone 
      
            oWord.Options.WarnBeforeSavingPrintingSendingMarkup = False 
      
            My.Application.DoEvents() 
      
            ' ResizeWordWindow() 
            '****************************************************** 
       
        End Sub 
       
      
        End Function 
      
    End Class
    Will be waiting for your reply…

    Kind Regards
    Seema
    Last edited by SeemsP; January 30th, 2014 at 06:31 AM. Reason: title edit

  2. #2
    Join Date
    Jan 2013
    Posts
    23

    Re: How to load the .docx to word doc, also make me to understand the code(black magi

    Hi ALL,

    Yes you are right that its using the Word object model, well my main work is to understand the code i have posted, i cant use any webbrowser ctrl, boz the code they have written that i have to understand and build the application.

    Ok i have another question i tried to open the document using the below code, well its not opening, and its not showing the value in the variable oDoc

    Code:
     oDoc = oWord.Documents.Open(FileName, False)
    - this is the code where its not showing the value and its not putting in to the Panel control i.e pnlWordContainer


    Ok i am adding a very short code, i mean i have modified it, and please tell me why is it not taking the value... How to make it to take the value...

    i have 2 panels on my form
    1-panel1
    2-pnlWordContainer - on this i have to show the .docx document....

    Code:
    Imports Microsoft.Office.Interop
    
    Imports Microsoft.Office.Interop.Word
    Imports VB = Microsoft.VisualBasic
    'Imports BTIS.TList
    Imports System.Drawing
    Imports System.Diagnostics
    Imports System.Data
    Imports System.Windows.Forms.Application
    
    Public Class Word_doc_Export
    
     Public Const SWP_FRAMECHANGED As Integer = &H20
        Public Const SWP_NOACTIVATE As Integer = &H10
        Public Const SWP_DRAWFRAME As Integer = &H20
        Public Const SWP_NOMOVE As Integer = &H2
        Public Const SWP_NOSIZE As Integer = &H1
        Public Const SWP_NOZORDER As Integer = &H4
        Public Const SWP_SHOWWINDOW As Integer = &H40
        Public Const SWP_NOSENDCHANGING As Integer = &H400
    
        Public Const SMTO_ABORTIFHUNG As Int32 = &H2
    
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
        Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer
        Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
        Private Declare Function MoveWindow Lib "user32" Alias "MoveWindow" (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
    
        Private WithEvents oWord As Word.Application
        Private WithEvents oDoc As Word.Document
    
        Dim WordHandle As Integer
    
    
     Private Sub Word_doc_Export_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            oWord = New Word.Application
                End Sub
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim sv As New FarPoint.Win.Spread.SheetView
            Dim chkbox As New FarPoint.Win.Spread.CellType.CheckBoxCellType
            Dim FileNameWithoutPath As String
            FileName = " C:\Users\aseema\Desktop\RA code of my understanding1.docx"
    
    
            ' On Error GoTo LoadError
            Loading = True
            FileNameWithoutPath = System.IO.Path.GetFileName(FileName)
    
            grd.Visible = False
            Panel1.Dock = DockStyle.Fill
            pnlWordContainer.Dock = DockStyle.Fill
            pnlWordContainer.Visible = False
    
    
            '******************************************************
            'Black Magic
            '******************************************************
            oWord = New Word.Application
            oDoc = New Word.Document
    
            oWord.Caption = "SIM"
            WordHandle = FindWindow("Opusapp", "SIM")
            SetParent(WordHandle, Me.pnlWordContainer.Handle.ToInt32)
            oWord.Caption = Nothing
            My.Application.DoEvents()
    
            WordVersion = CheckMSWordVersion()
    
            oDoc = oWord.Documents.Open(FileName, False) 'Here the Word Documet is opened..
            oDoc.Application.Visible = True
            oDoc.Application.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
    
            oWord.Options.WarnBeforeSavingPrintingSendingMarkup = False
    
            My.Application.DoEvents()
    
            '******************************************************
        End Sub
    
    End Class
    Where is my code going wrong? why is it so....
    Also i dnt know about the handlers... so i m googling abt them now, but mean while if you get to know help me out.

    Will be waiting for your reply


    Kind Regards
    Seema
    Last edited by SeemsP; January 30th, 2014 at 06:32 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center