2 Replies - 23450 Views - Last Post: 17 October 2008 - 04:58 AM Rate Topic: -----

#1 sarasaad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-October 08

display word doc in browser

Posted 12 October 2008 - 01:36 PM

Public Class Form1
	Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

	Public Sub New()

		'This call is required by the Windows Form Designer.

		'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
			End If
		End If
	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.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
		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."
		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()

	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)
	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.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
 If .ShowDialog = DialogResult.OK Then

i attached the progam also;

please help me in this code or any other suggestion , thanx in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: display word doc in browser

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon

Reputation: 1431
  • View blog
  • Posts: 11,006
  • Joined: 06-February 08

Re: display word doc in browser

Posted 12 October 2008 - 05:33 PM

Moved to VB.NET :)
Was This Post Helpful? 0
  • +
  • -

#3 rollout369  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-October 08

Re: display word doc in browser

Posted 17 October 2008 - 04:58 AM

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...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1