473,405 Members | 2,176 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,405 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 6517
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.