Results 1 to 1 of 1

Thread: Viewing Word Documents in a Webbrowser Control.

  1. #1

    Thread Starter
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Post Viewing Word Documents in a Webbrowser Control.

    if you've ever tried to open a Word Document in a Webbrowser or InternetExplorer , you will be aware that it's not an automatic process. you will be presented with a " Download File " dialog box.
    Well in i step , here's my unique method of opening a Word Document in a Webbrowser...
    VB Code:
    1. '/// NOTE : You need word installed to run this code...
    2.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3.         Dim DocViewer As New Threading.Thread(AddressOf ViewDocInWebbrowser)
    4.         DocViewer.Start()
    5.     End Sub
    6.  
    7.     Private Sub ViewDocInWebbrowser()
    8.         Dim od As New OpenFileDialog()
    9.         With od
    10.             .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
    11.             .Filter = "Word Documents|*.DOC"
    12.             If .ShowDialog = DialogResult.OK Then
    13.                 Dim typeWord As Type = Type.GetTypeFromProgID("Word.Application")
    14.                 Dim WordApp As Object = Activator.CreateInstance(typeWord)
    15.  
    16.                 Dim htmlFormat As Integer = 8
    17.                 Dim Docpath As Object() = {.FileName} '/// path to a valid .Doc file.
    18.                 Dim HtmPath As Object() = {Application.StartupPath & "\WordDoc.HTML", htmlFormat} '/// temp path to hold the html version of the .Doc file.
    19.  
    20.                 Dim WordDocs As Object = typeWord.InvokeMember("Documents", Reflection.BindingFlags.GetProperty, Nothing, WordApp, Nothing)
    21.                 Dim doc As Object = WordDocs.GetType.InvokeMember("Open", Reflection.BindingFlags.InvokeMethod, Nothing, WordDocs, Docpath)
    22.                 doc.GetType.InvokeMember("SaveAs", Reflection.BindingFlags.InvokeMethod, Nothing, doc, HtmPath)
    23.  
    24.                 WordApp.quit() '/// close the instance of Word down.
    25.  
    26.                 browser.Navigate(HtmPath(0)) '/// load the Word Document in to the webbrowser.
    27.             End If
    28.         End With
    29.     End Sub
    30.  
    31.     Private Sub browser_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles browser.DocumentComplete
    32.         '/// clean up temporary files...
    33.         If IO.File.Exists(Application.StartupPath & "\WordDoc.HTML") Then
    34.             IO.File.Delete(Application.StartupPath & "\WordDoc.HTML") '/// remove the temp file.
    35.         End If
    36.         If IO.Directory.Exists(Application.StartupPath & "\WordDoc_files") Then
    37.             Dim files As String() = IO.Directory.GetFiles(Application.StartupPath & "\WordDoc_files")
    38.             Dim file As String
    39.             For Each file In files
    40.                 IO.File.Delete(file)
    41.             Next
    42.             IO.Directory.Delete(Application.StartupPath & "\WordDoc_files") '/// remove the temp folder.
    43.         End If
    44.     End Sub
    included is a zipped project source.
    Attached Files Attached Files
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

Posting Permissions

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



Click Here to Expand Forum to Full Width