Range Character Count is off....can't figure this out.

D

dvdastor

Hi All,

Below is a macro followed by a document that I am running through
looking for instances of a Test. The Tests are marked by [~Test
ID=xxxx~], (xxxx is a variable number). I need to select the range
from [~Test ID=xxxx~] to the beginning of the next Test tag.

When I use the macro below, I do not get the results I'm looking for
and I can't figure it out. I think it has something to do with the
hyperlinks I have in the document. When I delete them or make them non
hyperlinks, I get the range selection that I need. Can you offer any
help?

Sub SelectIt()

Dim rng As Range
Dim SearchRange As Range
Dim iTestCharCount As Integer

Set rng = ActiveDocument.Range


With rng

iTestCharCount = rng.Characters.Count

.Start = InStr(rng.Text, "[~Test ID=" & 9463 &
"~]") - 1
iCurrentTestTag = .Start

.End = InStr(.Start + 1, rng.Text, "[~Test
ID=")
iNextTestTag = .End


If iNextTestTag = 0 Then
.Start = iCurrentTestTag
.MoveEnd Word.WdUnits.wdCharacter,
iTestCharCount
Else
.Start = iCurrentTestTag
.End = (iNextTestTag + iCurrentTestTag) - 1
End If
.Select
'.Delete
End With
End Sub

-----------------DOCUMENT-----------------------

[~Exam ID=856~]97-TUT -[~ExamName~] Practice Import
[~Vendor~]Microsoft
[~ExamVersion~]Internal Version: 1.0.3
[~ExamDescription~]test
[~ExamInstructions~]
[~ExamResources~]test

'RANGE SHOULD START HERE
[~Test ID=9463~]
Test for Import
[~TestType~]Practice Test
[~ItemCount~]Item Count: 0

[~Objective ID=66428~]Here is the house
[~ObjSeq~]Objective Sequence: 1

[~SubObjective ID=274124~]Where it all happened
[~SubObjSeq~]1
[~SubShow~]False

[~Item~]
[~ItemType~]Single Answer Multiple Choice
[~ItemATID~]70-057.1.1.1
[~ItemText~]You are developing a Web site infrastructure for your Web
site, and you must meet the following requirements:

The required result is to be able to add VBScript functionality to the
Web pages.

The first optional result is to have the ability to run scripts on the
server for purposes of retrieving database information.
The second optional result is to be able to output this information in
HTML format so the majority of Web browsers can view this information.

The proposed solution is to implement Active Server Pages on a Windows
NT Server 4.0 computer that is running Site Server 3.0.

What does the proposed solution provide?


[~Item~]
[~ItemType~]Single Answer Multiple Choice
[~ItemATID~]70-057.1.1.2


[~ItemText~]
You are developing a Web site infrastructure for your Web site, and you
must meet the following requirements:

The required result is to be able to add VBScript functionality to the
Web pages.

The first optional result is to have the ability to run scripts on the
server for purposes of retrieving database information.
The second optional result is to be able to output this information in
HTML format so the majority of Web browsers can view this information.

The proposed solution is to implement a COM component.

What does the proposed solution provide?
[~RemediationText~]
COM components allow software to be written in components that perform
separate functions. This allows several procedures to call the same
components. None of the results are met by implementing a COM
component. To meet the results, you should use Active Server Pages.
www.yahoo.com

www.cnn.com

[~ReferenceText~]

1. General Knowledge and Experience - <none>

Here is more '<-- THE RANGE IS ENDING RIGHT AROUND HERE
www.kaplanit.com

www.erert.com

www.yahoo.com

www.cnn.com

'THIS IS WHERE I NEED TO RANGE TO END
[~Test ID=9466~]
Flash Card Test import
[~TestType~]Flash Card
[~ItemCount~]Item Count: 0

---------------------DOCUMENT END---------------------

Thanks for any help.
David
 
D

David Sisson

I didn't test this too thoughly, but it worked on your example text.

Sub RangeBetweenTwo()
'David Sisson July 2005
'Return the range between two characters
Dim Rng, Rng2, Rng3 As Range

Set Rng = ActiveDocument.Range
Set Rng2 = Rng.Duplicate
Set Rng3 = Rng.Duplicate

Do
With Rng2.Find
.ClearFormatting
.Text = "[~Test ID="
.Forward = True
.Wrap = wdFindStop
.Execute
End With

'exit if not found
If Rng2.Find.Found Then
Rng3.SetRange Rng2.End, Rng.End
With Rng3.Find
.ClearFormatting
.Text = "[~Test ID="
.Forward = True
.Wrap = wdFindStop
.Execute
End With

If Rng3.Find.Found Then
Begin = Rng2.Start
TheEnd = Rng3.Start + 1
Rng3.SetRange Begin, TheEnd - 1
Rng3.Select 'For testing purposes
'rng.Delete
End If
Else
MsgBox ("Nothing found!")
End If
Loop Until Not Rng2.Find.Found



End Sub
 
D

dvdastor

Thanks for the reply David. I have a few questions though.

It seems that this is good for looping through the tests in order that
they appear in the document. however, I am interested in how to find a
specific test ID. For example, if I need to find and delete [~Test
ID=9466~], ( the second test in the document), I don't necessarily need
to go through every test. I just want to go directly to that test and
remove that range.

My second question is: How can I select the last test in the list. If
there are no more [~Test ID=....~] tags, i need the range to start at
the "[" and go to the last character in the document. At this point it
should select that last test in the document.

Once again, thanks for any help you can provide.
 
D

David Sisson

I think you might have gotten a quicker answer if you asked how to
delete a range based on the ID, but...

Sub DeleteRange()
'David Sisson July 2005
'Return the range between two characters
Dim Rng, Rng2, Rng3 As Range

Set Rng = ActiveDocument.Range
Set Rng2 = Rng.Duplicate
Set Rng3 = Rng.Duplicate

Ans = InputBox("Enter Id Number", vbOKCancel)

With Rng2.Find
.ClearFormatting
.Text = Ans
.Forward = True
.Wrap = wdFindStop
.Execute
End With

'exit if not found
If Rng2.Find.Found Then
Rng3.SetRange Rng2.End, Rng.End
With Rng3.Find
.ClearFormatting
.Text = "[~Test ID="
.Forward = True
.Wrap = wdFindStop
.Execute
End With

If Rng3.Find.Found Then
Begin = Rng2.Start - 10
TheEnd = Rng3.Start + 1
Rng3.SetRange Begin, TheEnd - 1
'Rng3.Select 'For testing purposes
Rng3.Delete
End If
Else
MsgBox ("Nothing found!")
End If

End Sub
 
D

David Sisson

I don't have the correct syntax on Inputbox, so here's new version.
This also tests the length of the Input in case you press cancel.

Sub DeleteRange()
'David Sisson July 2005
'Return the range between two characters
Dim Rng, Rng2, Rng3 As Range
Dim Ans As String

Set Rng = ActiveDocument.Range
Set Rng2 = Rng.Duplicate
Set Rng3 = Rng.Duplicate


Ans = InputBox("Enter Id Number", "Delete ID Number")
If Len(Ans) > 1 Then

With Rng2.Find
.ClearFormatting
.Text = Ans
.Forward = True
.Wrap = wdFindStop
.Execute
End With

'exit if not found
If Rng2.Find.Found Then
Rng3.SetRange Rng2.End, Rng.End
With Rng3.Find
.ClearFormatting
.Text = "[~Test ID="
.Forward = True
.Wrap = wdFindStop
.Execute
End With

If Rng3.Find.Found Then
Begin = Rng2.Start - 10
TheEnd = Rng3.Start + 1
Rng3.SetRange Begin, TheEnd - 1
'Rng3.Select 'For testing purposes
Rng3.Delete
End If
Else
MsgBox ("Nothing found!")
End If
End If

End Sub
 
Top