Please Help!! Tracking changes ...

S

Smriti

When we switch on "Track changes" in MS Word it shows the
text that is inserted and deleted thereafter.

I want to access this text using VB .NET. But there doesn't
seem to be any object that could give me a handle to the
inserted or deleted text. Can anybody help me please ??

Smriti
 
P

Peter Hewett

Hi Smriti

You can enumerate the Documents Revisions object. If you check out the object browser you
can explore its methods and properties. The key ones for you will by the Type property
which tells you the type of change that has been made to the document and the Range object
which will return the new/replaced/deleted text.

For some reason Dot .Net does not link to the Office help files even with the PIAs
installed. So you may find out more by exploring the particular objects you are
interested in from within the Ofiice applications VBA IDE. If may need to go back and
install the VBA help files if you did not do it as part of your office install. The best
way of doing this is from within the VBA IDE's immediate window or by writing code stubs
and just pressing F1 to get help on the object.

HTH + Cheers - Peter
 
S

Smriti

Thanks a lot, Peter. The revision object does give me a lot
of information on the inserted and deleted text. However it
does not tell me the point at which the character/word was
deleted.

Actually if I save the word document as an XML, I see that
it adds an annotation tag at the point where the text was
inserted/deleted along with the respective text and other
meta data. I was wondering if there was any object in VBA
that would give me all such information ?

Thanks,
Smriti

-----Original Message-----
Hi Smriti

You can enumerate the Documents Revisions object. If you
check out the object browser you
can explore its methods and properties. The key ones for you will by the Type property
which tells you the type of change that has been made to
the document and the Range object
which will return the new/replaced/deleted text.

For some reason Dot .Net does not link to the Office help files even with the PIAs
installed. So you may find out more by exploring the particular objects you are
interested in from within the Ofiice applications VBA IDE. If may need to go back and
install the VBA help files if you did not do it as part of your office install. The best
way of doing this is from within the VBA IDE's immediate
window or by writing code stubs
 
P

Peter Hewett

Hi Smriti

Did you check out the Revision objects Range property this should map to the
modified/deleted text.

HTH + Cheers - Peter
 
S

Smriti

Yes I did. And thats what gives me the text that has been
inserted/deleted. But what I'm also trying to get is the
point at which the text was inserted/deleted.

But I'm unable to get that.
 
P

Peter Hewett

Hi Smriti

Range objects ALWAYS specify a location, they don't just return text, they map a document
location and return text from that location. I think it may help you to read up on the
Range object.

I hope this VB.Net code sample (Console project) illustrates it a little. It was created
using VS2003, Office XP with the Office XP PIA's::

Imports Microsoft.Office.Interop
Imports System.Windows.Forms

Module WordAppTest
Private Const APP_NAME As String = "Word Range Test"

Sub Main()
Dim appWord As Word.Application
Dim testDoc As Word.Document

Try
' Create Word instance
appWord = New Word.Application
appWord.Visible = True

' Create a test document make sure we can see it!
testDoc = appWord.Documents.Add(appWord.NormalTemplate.FullName, _
False, Word.WdDocumentType.wdTypeDocument, True)

' Text to add to document
Dim text1 As String = "The quick brown fox jumps over the lazy dog. " & _
"The quick brown fox jumps over the lazy dog."
Dim text2 As String = _
"Little Jack Horner sat in the corner eating his curds and whey."

' Create 4 paragraphs, 2 sentences in each
testDoc.Content.Text = text1 & ControlChars.CrLf & _
text1 & ControlChars.CrLf & text1 & ControlChars.CrLf & _
text1 & ControlChars.CrLf

' Set up a range object to the second paragraph
Dim rangeTest As Word.Range
rangeTest = testDoc.Paragraphs(2).Range

' Display the text mapped by the range object
MessageBox.Show(rangeTest.Text, APP_NAME)

' Replace the text of the second paragraph
rangeTest.Text = text2 & ControlChars.CrLf

' Move the range start character forward 1 and last character backwards 3
rangeTest.Start += 1
rangeTest.End -= 3

' Set the colour of the mapped text to red
rangeTest.Font.Color = Word.WdColor.wdColorRed

' Display the text mapped by the range object
MessageBox.Show(rangeTest.Text, APP_NAME)

Catch ex As Exception
MessageBox.Show(ex.Message, APP_NAME, MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Finally

' Destroy the Word reference variable but leave Word running
If Not testDoc Is Nothing Then testDoc = Nothing
If Not appWord Is Nothing Then appWord = Nothing
End Try
End Sub
End Module

HTH + Cheers - Peter
 
S

Smriti

Hi Peter,

When in case of deleted text, when I access the
range.paragraphs(1), it gives me the first paragraph of the
document.

But in case of inserted text, range.paragraph(1) gives me
the paragraph in whihc the text was inserted.

So thats my problem.

Thanks,
Smriti
-----Original Message-----
Hi Smriti

Range objects ALWAYS specify a location, they don't just
return text, they map a document
location and return text from that location. I think it may help you to read up on the
Range object.

I hope this VB.Net code sample (Console project)
illustrates it a little. It was created
using VS2003, Office XP with the Office XP PIA's::

Imports Microsoft.Office.Interop
Imports System.Windows.Forms

Module WordAppTest
Private Const APP_NAME As String = "Word Range Test"

Sub Main()
Dim appWord As Word.Application
Dim testDoc As Word.Document

Try
' Create Word instance
appWord = New Word.Application
appWord.Visible = True

' Create a test document make sure we can see it!
testDoc =
appWord.Documents.Add(appWord.NormalTemplate.FullName, _
 
C

Cindy M -WordMVP-

Hi Smriti,
When in case of deleted text, when I access the
range.paragraphs(1), it gives me the first paragraph of the
document.

But in case of inserted text, range.paragraph(1) gives me
the paragraph in whihc the text was inserted.
It all depends on how you've defined RANGE. Show us an
example of your code that shows how you declare and set the
original range, then paste into it?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun
8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
P

Peter Hewett

Hi Smriti

Now I understand. Ok, as you've discovered there's a problem! When the revision type is
deleted "Revison.Range.Paragraphs(1)" for some reason always seems to return the first
paragraph of the document!

Now here's the work around:

Dim testDoc As Word.Document
With testDoc

' Handle the anomalous case of deleted text where:
' .Revisions(n).Range.Paragraphs(1) returns the first paragraph in the document
If .Revisions(1).Type = wdRevisionDelete Then

' Map range object to the deleted text
Dim deletedRange As Word.Range
deletedRange = .Revisions(1).Range.Duplicate

' Push range back a character to force it into the
' paragraph it was deleted from
deletedRange.Move wdCharacter, -1

' Now we have the correct paragraph
Dim deletedFromPara As Word.Paragraph
deletedFromPara = deletedRange.Paragraphs(1)
End If
End With

' Just to prove we have the correct paragraph
deletedFromPara.Range.Select

Note the use of ".Revisions(1).Range.Duplicate". This is the Range objects equivalent of
implementing the ICloneable interface! In theory I don't think it should be required, but
in practice it doesn't work without it!

HTH + Cheers - Peter
 
S

Smriti

Hi Peter,

Thanks a lot! It works now.

Just one more question :) - once I get the paragraph from
which the text was deleted I get the word which occurs
before the text that was deleted. So I do:

deletedrange.words(1).text

However, sometimes I get the word which occurs before the
deleted text while sometimes I get the word which occurs
*after* the deleted text. DO you know what could be the
reason ?

Thanks,
Smriti
-----Original Message-----
Hi Smriti

Now I understand. Ok, as you've discovered there's a
problem! When the revision type is
deleted "Revison.Range.Paragraphs(1)" for some reason
always seems to return the first
paragraph of the document!

Now here's the work around:

Dim testDoc As Word.Document
With testDoc

' Handle the anomalous case of deleted text where:
' .Revisions(n).Range.Paragraphs(1) returns the
first paragraph in the document
If .Revisions(1).Type = wdRevisionDelete Then

' Map range object to the deleted text
Dim deletedRange As Word.Range
deletedRange = .Revisions(1).Range.Duplicate

' Push range back a character to force it into the
' paragraph it was deleted from
deletedRange.Move wdCharacter, -1

' Now we have the correct paragraph
Dim deletedFromPara As Word.Paragraph
deletedFromPara = deletedRange.Paragraphs(1)
End If
End With

' Just to prove we have the correct paragraph
deletedFromPara.Range.Select

Note the use of ".Revisions(1).Range.Duplicate". This is
the Range objects equivalent of
implementing the ICloneable interface! In theory I don't
think it should be required, but
 
P

Peter Hewett

Hi Smriti

I'm not quite sure what it is you're trying to do in toto. It might be helpful to
understand this and cut through some of the intermediate steps.

Again, I don't know what your data is and I don't have an overall context for the snippet
of code you've posted.

Paste the appropriate code and two pieces of data, one that works as you expect and one
that doesn't. I'll take a look and get back to you.

HTH + Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top