how to transfer contents of a word file into a text box by clicking a command button.
can anybody help me

Dani AI

Generated

Short answer: the simplest, most robust fix for a VB6 project running with Word 2002 is to avoid early-binding to a version-specific Word library and read the document's Content in one shot. Early binding (Project->References to a Word Object Library) works but will cause compile-time mismatches between Office versions; late binding or generic Object declarations sidestep that and eliminate the "compile" headaches saw. 's early-binding approach (iterating Words/Paragraphs) is valid, but you can often get the full plain text far more reliably by reading Document.Content.Text.

Recommended approach (VB6): create the Word application at runtime, open the file read the document Content.Text, then close and release objects. This returns just text (no images/tables). If you need a plain file instead, open and SaveAs a text file and then read the .txt with the FileSystemObject. For .docx files, consider using Open XML tooling to extract text without automating Word.

Example (late binding, VB6):

Private Sub Command1_Click()
    Dim wdApp As Object
    Dim wdDoc As Object

    Set wdApp = CreateObject("Word.Application")
    Set wdDoc = wdApp.Documents.Open("C:\Word.doc", ReadOnly:=True)
    Text1.Text = wdDoc.Content.Text

    wdDoc.Close False
    wdApp.Quit
    Set wdDoc = Nothing
    Set wdApp = Nothing
End Sub

Troubleshooting notes: ensure Word is installed and the path is correct; open read-only for safety; long documents may be slow when transferred into a single TextBox. If you must use early binding, pick the matching Word xx.0 Object Library in Project->References. For non-automation solutions and .docx files, see Microsoft's guidance on the Word object model and extracting text from WordprocessingML: Word document Content property and .

Recommended Answers

All 4 Replies

Hi, I think, it is possible when u use MSOffice Type Library. To read word file, you need Microsoft Word (Version No) Object Library.
To add reference
> Project -> Reference -> Microsoft Word (Version No 11 or 12) Object Library

Now Try the coding
Create Word File as C:\Word.doc
This just give idea to read word file. Also this transfers Text Only not images, tables etc.

Private Sub Command1_Click()
   Dim MyDocument       As Document
   Dim RangeText        As Range
   Dim MyApplication    As Application
   
   Set MyApplication = New Application
   Set MyDocument = Documents.Open("C:\Word.doc", Visible:=False)

   Text1.Text = ""
   For Each RangeText In MyDocument.Words
      Text1.Text = Text1.Text & vbCrLf & RangeText.Text
   Next
End Sub

Thanks Ganapathy,
I have used the codes and i am getting error in the following line
----------------------------------------------------------
For Each RangeText In MyDocument.Words
-------------------------------------------------------------
Its a Compile error which is shown and the error is displayed as
error is Method or Datamember not found

Microsoft word 10.0 Object library was found in Reference and included, not 11 or 12
as mentioned by you

Hi, apuamy, I used Microsoft Word 12.0 Object Library. I think you have installed MSOffice 2000. I tried the Google and found something.
I dont know whether it is work or not in Object Library 10.

Private Sub Command1_Click()
   Dim MyDocument       As Document
   Dim RangeText        [B]As Paragraph[/B]
   Dim MyApplication    As Application
   
   Set MyApplication = New Application
   Set MyDocument = Documents.Open("C:\Word.doc", Visible:=False)

   Text1.Text = ""
   For Each RangeText In [B]MyDocument.Paragraphs[/B]
      Text1.Text = Text1.Text & vbCrLf & [B]RangeText.Range.Text[/B]
   Next
End Sub

I referred the page http://msdn.microsoft.com/en-us/library/aa140225(office.10).aspx

Hi ganapathy,
the code is not working. may be i am using word 2002. that is office XP. Any solution in word 2002.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.