Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 131,734 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,224 people online right now. Registration is fast and FREE... Join Now!




display word doc in browser

 
Reply to this topicStart new topic

display word doc in browser

sarasaad
post 12 Oct, 2008 - 12:36 PM
Post #1


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 2

CODE
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents browser As AxSHDocVw.AxWebBrowser
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.browser = New AxSHDocVw.AxWebBrowser()
        Me.Button1 = New System.Windows.Forms.Button()
        CType(Me.browser, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'browser
        '
        Me.browser.Enabled = True
        Me.browser.OcxState = CType(resources.GetObject("browser.OcxState"), System.Windows.Forms.AxHost.State)
        Me.browser.Size = New System.Drawing.Size(736, 400)
        Me.browser.TabIndex = 0
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(0, 400)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(744, 23)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Open a Word Document in the Browser Above."
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(736, 422)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.browser})
        Me.Name = "Form1"
        Me.Text = "Dynamic's Doc Viewer"
        CType(Me.browser, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    '/// NOTE : You need word installed to run this code...
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DocViewer As New Threading.Thread(AddressOf ViewDocInWebbrowser)
        DocViewer.Start()
    End Sub

    Private Sub ViewDocInWebbrowser()
        Dim od As New OpenFileDialog()
        With od
            .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
            .Filter = "Word Documents|*.DOC"
            If .ShowDialog = DialogResult.OK Then
                Dim typeWord As Type = Type.GetTypeFromProgID("Word.Application")
                Dim WordApp As Object = Activator.CreateInstance(typeWord)

                Dim htmlFormat As Integer = 8
                Dim Docpath As Object() = {.FileName} '/// path to a valid .Doc file.
                Dim HtmPath As Object() = {Application.StartupPath & "\WordDoc.HTML", htmlFormat} '/// temp path to hold the html version of the .Doc file.

                Dim WordDocs As Object = typeWord.InvokeMember("Documents", Reflection.BindingFlags.GetProperty, Nothing, WordApp, Nothing)
                Dim doc As Object = WordDocs.GetType.InvokeMember("Open", Reflection.BindingFlags.InvokeMethod, Nothing, WordDocs, Docpath)
                doc.GetType.InvokeMember("SaveAs", Reflection.BindingFlags.InvokeMethod, Nothing, doc, HtmPath)

                WordApp.quit() '/// close the instance of Word down.

                browser.Navigate(HtmPath(0)) '/// load the Word Document in to the webbrowser.
            End If
        End With
    End Sub

    Private Sub browser_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles browser.DocumentComplete
        '/// clean up temporary files...
        If IO.File.Exists(Application.StartupPath & "\WordDoc.HTML") Then
            IO.File.Delete(Application.StartupPath & "\WordDoc.HTML") '/// remove the temp file.
        End If
        If IO.Directory.Exists(Application.StartupPath & "\WordDoc_files") Then
            Dim files As String() = IO.Directory.GetFiles(Application.StartupPath & "\WordDoc_files")
            Dim file As String
            For Each file In files
                IO.File.Delete(file)
            Next
            IO.Directory.Delete(Application.StartupPath & "\WordDoc_files") '/// remove the temp folder.
        End If
    End Sub
End Class



i want by this code to display doc in word formate in html formate in browser but i have 1 exception that

"Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process."

in this line
CODE
If .ShowDialog = DialogResult.OK Then

i attached the progam also;

please help me in this code or any other suggestion , thanx in advance.
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 12 Oct, 2008 - 04:33 PM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,377



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


Moved to VB.NET smile.gif
User is offlineProfile CardPM

Go to the top of the page

rollout369
post 17 Oct, 2008 - 03:58 AM
Post #3


New D.I.C Head

*
Joined: 17 Oct, 2008
Posts: 6

You may run into unanticipated problems with users being able to view a Word document in their browser. First, they're going to need to have either Word or the Word viewer installed. They're also going to need a browser that allows for Word documents to be displayed in the browser. And, you may have some users (like me) who turn this feature off so that all Word documents I run across are displayed in Word, regardless of the programmer's intent. Just so you know...
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 10:45AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month