473,320 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

help needed on Word._Document.Words property

31
Hi,
I am using Office XP PIA's to read a word document. I am able to read the whole content. The problem is that i want to get a particular word in the document instead of the whole content.
Will the _Document.Words property is useful to retrieve word by word form the document??? If it is possible, How to use it???

thanks
Nelluru
Apr 5 '07 #1
12 6515
SammyB
807 Expert 512MB
Hi,
I am using Office XP PIA's to read a word document. I am able to read the whole content. The problem is that i want to get a particular word in the document instead of the whole content.
Will the _Document.Words property is useful to retrieve word by word form the document??? If it is possible, How to use it???

thanks
Nelluru
It's hard to do Word automation in .NET, but, yes, you can use he Words collection. This example shows word 6 in the doc:
Expand|Select|Wrap|Line Numbers
  1. const string DOC = @"C:\Documents and Settings\Sam\My Documents\Visual Studio 2005\Projects\WordAutomation\Demo.doc";
  2. object ms = System.Reflection.Missing.Value;
  3. Word._Application wdApp = new Word.Application();
  4. wdApp.Visible = true;
  5. object oDoc = (object)DOC;
  6. Word._Document wdDoc = wdApp.Documents.Open(ref oDoc, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms);
  7. Word.Words w = wdDoc.Words;
  8. Word.Range r = w.Item(6);
  9. MessageBox.Show(r.Text);
  10.  
You can see how to use other Word objects in .NET at http://support.microsoft.com/kb/316384
Apr 5 '07 #2
Nelluru
31
hi,
Please help me..
I am getting this error:
'Microsoft.Office.Interop.Word.Words' does not contain a definition for 'Item'.

Code is:

Microsoft.Office.Interop.Word.Words w = wdDoc.Words;
Microsoft.Office.Interop.Word.Range r = w.Item(2);


I have included Microsoft.Office.Interop.Word dll in my project. And I am using Microsoft Visual C# 2005 Express Edition.

What would be the reason??
Is there anything I am missing???

thanks in Advance
Nelluru
Apr 9 '07 #3
SammyB
807 Expert 512MB
Hi,
I am using Office XP PIA's to read a word document. I am able to read the whole content. The problem is that i want to get a particular word in the document instead of the whole content.
Will the _Document.Words property is useful to retrieve word by word form the document??? If it is possible, How to use it???

thanks
Nelluru
Just a word of caution. If you are used to working with Word macros, then you will have a tendency to put Word objects together like wdDoc.Words.Item(6) instead of dealing with each object separately. This will not always work with C#. VBA kept a lot of details from us & pushed stuff into Variants. C# will not do this, but if you single step through your code, you can usually see what intermediate objects you need to create. I haven't done this with Word, but I have done it with Excel and it was very painful
Apr 10 '07 #4
Nelluru
31
Just a word of caution. If you are used to working with Word macros, then you will have a tendency to put Word objects together like wdDoc.Words.Item(6) instead of dealing with each object separately. This will not always work with C#. VBA kept a lot of details from us & pushed stuff into Variants. C# will not do this, but if you single step through your code, you can usually see what intermediate objects you need to create. I haven't done this with Word, but I have done it with Excel and it was very painful
Hi Sam,
I have already posted the message in the Forum http://www.thescripts.com/forum/thread629828.html . As I did not get any reply, I sent u a private message. And I am also sorry for the late reply. I didn't understand this reply. Can u please elaborate the reply???
Apr 12 '07 #5
SammyB
807 Expert 512MB
Hi Sam,
I have already posted the message in the Forum http://www.thescripts.com/forum/thread629828.html . As I did not get any reply, I sent u a private message. And I am also sorry for the late reply. I didn't understand this reply. Can u please elaborate the reply???
Sorry about the confusion. I didn't see your other post. I used a different reference: Microsoft Word x.x Object Library on the COM tab. I don't see Microsoft Office Interop, but I'm at work, stuck with Word 2000. I'll check when I get home, but it may be next week before I have time. I assume Interop is a DotNet thing, so could it be something simple like Microsoft.Office.Interop.Word.Range r = w[2]; instead of Microsoft.Office.Interop.Word.Range r = w.Item(2); Just a wild guess. --Sam
Apr 12 '07 #6
SammyB
807 Expert 512MB
Hi Sam,
I have already posted the message in the Forum http://www.thescripts.com/forum/thread629828.html . As I did not get any reply, I sent u a private message. And I am also sorry for the late reply. I didn't understand this reply. Can u please elaborate the reply???
Yes, from http://msdn2.microsoft.com/en-us/lib...ds(vs.80).aspx, I think w[2] will give you a Range object for the third word in the document.
Apr 12 '07 #7
SammyB
807 Expert 512MB
Hi Sam,
I have already posted the message in the Forum http://www.thescripts.com/forum/thread629828.html . As I did not get any reply, I sent u a private message. And I am also sorry for the late reply. I didn't understand this reply. Can u please elaborate the reply???
Well, I'm confused. I'm trying to verify if my replies are correct, but I cannot get the PIA installed. At home here I'm still running Office XP, but that's not a problem: I found the link to download the PIAs, http://www.microsoft.com/downloads/d...displaylang=en. This is a self-extracting zip, so I unzipped them and there is installation instructions in the Readme.htm. But the instructions assume that you have Visual Studio 2005 installed. How did you install the dll's with C# Express (which is what we both have)? Specifically, the install scripts need gacutil which is not on my machine. :confused: Sam
Apr 14 '07 #8
AricC
1,892 Expert 1GB
Sammy do you still need action on this thread?
Apr 15 '07 #9
SammyB
807 Expert 512MB
hi,
Please help me..
I am getting this error:
'Microsoft.Office.Interop.Word.Words' does not contain a definition for 'Item'.

Code is:

Microsoft.Office.Interop.Word.Words w = wdDoc.Words;
Microsoft.Office.Interop.Word.Range r = w.Item(2);

I have included Microsoft.Office.Interop.Word dll in my project. And I am using Microsoft Visual C# 2005 Express Edition.

What would be the reason??
Is there anything I am missing???

thanks in Advance
Nelluru
I decided to see what would happen if I tried to use the Interop.Word dll without installation. Everything worked fine. I did have to browse for the dll, but I expected that. Maybe the "install" just makes the dll's visible on the References NET tab. Anyway, this code works for me:
Expand|Select|Wrap|Line Numbers
  1. const string DOC = @"C:\Documents and Settings\Sam\My Documents\Visual Studio 2005\Projects\WordAutomation\Demo.doc";
  2. object ms = System.Reflection.Missing.Value;
  3. //Word._Application wdApp = new Word.Application();
  4. Microsoft.Office.Interop.Word.Application wdApp = new Microsoft.Office.Interop.Word.ApplicationClass();
  5. wdApp.Visible = true;
  6. object oDoc = (object)DOC;
  7. Microsoft.Office.Interop.Word._Document wdDoc = wdApp.Documents.Open(ref oDoc, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms);
  8. Microsoft.Office.Interop.Word.Words w = wdDoc.Words;
  9. Microsoft.Office.Interop.Word.Range r = w[2];
  10. MessageBox.Show(r.Text);
Does that help? --Sam
Apr 16 '07 #10
Nelluru
31
I decided to see what would happen if I tried to use the Interop.Word dll without installation. Everything worked fine. I did have to browse for the dll, but I expected that. Maybe the "install" just makes the dll's visible on the References NET tab. Anyway, this code works for me:
Expand|Select|Wrap|Line Numbers
  1. const string DOC = @"C:\Documents and Settings\Sam\My Documents\Visual Studio 2005\Projects\WordAutomation\Demo.doc";
  2. object ms = System.Reflection.Missing.Value;
  3. //Word._Application wdApp = new Word.Application();
  4. Microsoft.Office.Interop.Word.Application wdApp = new Microsoft.Office.Interop.Word.ApplicationClass();
  5. wdApp.Visible = true;
  6. object oDoc = (object)DOC;
  7. Microsoft.Office.Interop.Word._Document wdDoc = wdApp.Documents.Open(ref oDoc, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms);
  8. Microsoft.Office.Interop.Word.Words w = wdDoc.Words;
  9. Microsoft.Office.Interop.Word.Range r = w[2];
  10. MessageBox.Show(r.Text);
Does that help? --Sam

Hi Sam,
I am very sorry. I didn't checked the posts.What you suggested is working fine i.e., Microsoft.Office.Interop.Word.Range r = w[2]. I don't have any problems.
I am using Visual C# 2005 Express Edition with Office 2000. As far as the PIA's are concerned I didn't installed them. The same way as you have done( I am just using the Browse option to locate the Office dll's and just adding them.). Everything working fine now.
I am very thankful to you Sam and once again sorry for the late posts.
warm regards
Nelluru
Apr 16 '07 #11
Nelluru
31
I decided to see what would happen if I tried to use the Interop.Word dll without installation. Everything worked fine. I did have to browse for the dll, but I expected that. Maybe the "install" just makes the dll's visible on the References NET tab. Anyway, this code works for me:
Expand|Select|Wrap|Line Numbers
  1. const string DOC = @"C:\Documents and Settings\Sam\My Documents\Visual Studio 2005\Projects\WordAutomation\Demo.doc";
  2. object ms = System.Reflection.Missing.Value;
  3. //Word._Application wdApp = new Word.Application();
  4. Microsoft.Office.Interop.Word.Application wdApp = new Microsoft.Office.Interop.Word.ApplicationClass();
  5. wdApp.Visible = true;
  6. object oDoc = (object)DOC;
  7. Microsoft.Office.Interop.Word._Document wdDoc = wdApp.Documents.Open(ref oDoc, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms, ref ms);
  8. Microsoft.Office.Interop.Word.Words w = wdDoc.Words;
  9. Microsoft.Office.Interop.Word.Range r = w[2];
  10. MessageBox.Show(r.Text);
Does that help? --Sam

Hi Sam,
As I am using Office 2000, I had another problem. I have to use
wdApp.Documents.Open2000() instead of wdApp.Documents.Open().
when I am using Open() it is giving an Exception saying that:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at Microsoft.Office.Interop.Word.Documents.Open(Objec t& FileName, Object& ConfirmConversions, Object& ReadOnly, Object& AddToRecentFiles, Object& PasswordDocument, Object& PasswordTemplate, Object& Revert, Object& WritePasswordDocument, Object& WritePasswordTemplate, Object& Format, Object& Encoding, Object& Visible, Object& OpenAndRepair, Object& DocumentDirection, Object& NoEncodingDialog)


The issue is that the Open2000 will not work if another system contains Office Xp. Open() should be used for that.
What would be the problem?? Is there anything for the backward compatability???
Apr 16 '07 #12
SammyB
807 Expert 512MB
Hi Sam,
As I am using Office 2000, I had another problem. I have to use
wdApp.Documents.Open2000() instead of wdApp.Documents.Open().
when I am using Open() it is giving an Exception saying that:

Attempted to read or write protected memory. bla, bla ...

The issue is that the Open2000 will not work if another system contains Office Xp. Open() should be used for that.
What would be the problem?? Is there anything for the backward compatability???
I thought that I read somewhere that the PIAs only work for Office 2002 and later, but if that is the case, why do they have Open2000? I think that I would not use the PIA's. See if you can use the COM directly as I did in post #2. You just use the COM tab to add a reference to Microsoft Word Object Library, then see if my code work for Word 2K. I may be in the office tomorrow (I have 2000 there) and I will try it also.
Apr 16 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jack Smith | last post by:
Help needed on this question. Any help is appreciated. Thanks in advance. Given a binary string (i.e. a finite sequence of 0's and 1's) we choose any two digit substring 01 and replace it by a...
3
by: % =joe % | last post by:
I cannot get this code to work. Very simple...I have three list menus. I want to do a check before the form submits to make sure that the value of the 3 fields is equal to 12. Here's my...
1
by: worzel | last post by:
Hi All, I am looking for a reg ex that will match email addresses withing <a href=mailto blah > links. Actually, I already crafted my own, but with a slight problem: <a...
2
by: Willie | last post by:
I try to setup SQL Server to work with .NET Framework Anyone here willing to help? Step By Step Help Needed. Thanks
1
by: BHPexpert | last post by:
Regular Expression help needed -------------------------------------------------------------------------------- I want to extract all text that is contained inside the brackets after the word...
1
by: --== Alain ==-- | last post by:
Hi, Sorry to open a new post but i still do not have solved my issue with Collection property and i'm really blocked. I have a control which has a collection property. when an item of the...
0
by: Christopher | last post by:
Urgent Help Needed: The EPVH-1.1 Visual Hull Library. Dear All, I am a student doing research in computer vision. The EPVH-1.1 Visual Hull Library will really help a lot in my research. I...
1
by: Joel Fireman | last post by:
Help Needed: Upgrade Fedora 4 / Apache 2 to PHP 5.2.x from 5.0.4 I've been testing Joomla as a content manager for the County offices, and it looks pretty good. Unfortunately, I decided to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.