954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

code for transferring contents of a word doc in a textbox

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

apuamy
Newbie Poster
22 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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
selvaganapathy
Posting Pro
547 posts since Feb 2008
Reputation Points: 44
Solved Threads: 100
 

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

apuamy
Newbie Poster
22 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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        <strong>As Paragraph</strong>
   Dim MyApplication    As Application
   
   Set MyApplication = New Application
   Set MyDocument = Documents.Open("C:\Word.doc", Visible:=False)

   Text1.Text = ""
   For Each RangeText In <strong>MyDocument.Paragraphs</strong>
      Text1.Text = Text1.Text & vbCrLf & <strong>RangeText.Range.Text</strong>
   Next
End Sub


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

selvaganapathy
Posting Pro
547 posts since Feb 2008
Reputation Points: 44
Solved Threads: 100
 

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

apuamy
Newbie Poster
22 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You