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.