Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting the content of Word Document from a folder or database

Status
Not open for further replies.
Jul 28, 2011
167
NG
Hi everyone,

I have an interesting situation at hand.
I already succeeded in uploading files (.doc, .docx, etc) to both the database and a folder on the server. I can also download the files to client systems successfully without any issue.

However, my manager wants people to be able to see the content of some of the file on the home page of our website. I've been thinking on how to extract the content of the files (stored in database and server) and then display it on the page.

He says the files can be updated from time to time, thus the display should be the most recent update. This is why I've been thinking of extracting the content of the data already uploaded.

Any ideas.

____________________
Men put up a strong face just to cover their weaknesses...good!
But a smile makes them live longer....
Which would you choose?

Think about it.
 
Well, after a lot of tries, I came up with the following to extract the content from word document on a folder.
Code:
Imports Microsoft.Office.Interop  'Requires reference to Office.dll 

Partial Class _Default
    Inherits System.Web.UI.Page

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim objWord As Word.ApplicationClass
    Dim strText As String
    objWord = New Word.Application()
    With objWord
      .Visible = False
      .Documents.Open("D:\Users\ahassan\Documents\test.doc", , True)
      .WordBasic.EditSelectAll()
      .WordBasic.SetDocumentVar("MyVar", .ActiveDocument.ActiveWindow.Selection.Text)
      strText = .WordBasic.GetDocumentVar("MyVar")
      Label1.Text = InsertNewLineChars(Strings.Left(strText, Len(strText) - 1))
      .Documents.Close(0)
      .Quit()
    End With
    'End Sub

  End Sub

  Private Function InsertNewLineChars(ByVal strText As String) As String
    Dim pos As Integer, l As Integer
    pos = 1
    Do While pos < Len(strText)
      l = Strings.InStr(pos, strText, vbCr)
      If l = 0 Then Exit Do
      strText = Strings.Left(strText, l - 1) & vbCrLf & Mid(strText, l + 1)
      pos = l + 2
    Loop
    Return strText
  End Function
End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top