How to delete a Range, based on a Fields-member

D

Dietmar Brueckmann

Hi,

I'd like to delete all the text from the begin of a document till the end of
the first Field:
_____________________________
Sub CreateTestDoc()
Application.Documents.Add
Selection.TypeText Text:="aslkfdj laskjf"
Selection.TypeParagraph
Selection.TypeText Text:="12345 "

Documents(1).Variables.Add "TestVar", "Text of TestVar"
Documents(1).Fields.Add Selection.Range, wdFieldDocVariable, "TestVar"

Selection.TypeText Text:=" 6789"
Selection.TypeParagraph

End Sub
Sub TestField1()
Dim aRange As Range

CreateTestDoc

Set aRange = Application.Documents(1).Fields(1).Result
aRange.MoveStart wdStory, -1

MsgBox "»" + aRange.Text + "«"

aRange.Delete

End Sub
_________________________________

MsgBox shows the espected text.
aRange.Delete fails with german error message "Bereich kann nicht bearbeitet
werden" what could be in english "The range couldn't be processed".

Also fails aRange.Text = ""

I've found a workaround:

aRange.Select
Selection.Delete

But the Delete kills also the space before "6789". MsgBox doesn't show it.

I've two questions:

1. what's the problem with aRange.Delete
2. how could I avoid deleting the space after the variable-field, I need it

Best regards
Dietmar
 
C

Cindy M -WordMVP-

Hi Dietmar,

The problem apparently is due to the range ending "inside" the field code
brackets. You can't see it, but Word can, and since doing what you want to do
would damage the file structure, Word won't allow it.

Believe it or not, moving the END of the range by one character will make
everything work, even though if you look at the result in the message box or
have VBA .Select the range you won't see any difference at all...

Set aRange = Application.Documents(1).Fields(1).Result
aRange.MoveEnd wdCharacter, 1
aRange.MoveStart wdStory, -1
I'd like to delete all the text from the begin of a document till the end of
the first Field:
_____________________________
Sub CreateTestDoc()
Application.Documents.Add
Selection.TypeText Text:="aslkfdj laskjf"
Selection.TypeParagraph
Selection.TypeText Text:="12345 "

Documents(1).Variables.Add "TestVar", "Text of TestVar"
Documents(1).Fields.Add Selection.Range, wdFieldDocVariable, "TestVar"

Selection.TypeText Text:=" 6789"
Selection.TypeParagraph

End Sub
Sub TestField1()
Dim aRange As Range

CreateTestDoc

Set aRange = Application.Documents(1).Fields(1).Result
aRange.MoveStart wdStory, -1

MsgBox "»" + aRange.Text + "«"

aRange.Delete

End Sub
_________________________________

MsgBox shows the espected text.
aRange.Delete fails with german error message "Bereich kann nicht bearbeitet
werden" what could be in english "The range couldn't be processed".

Also fails aRange.Text = ""

I've found a workaround:

aRange.Select
Selection.Delete

But the Delete kills also the space before "6789". MsgBox doesn't show it.

I've two questions:

1. what's the problem with aRange.Delete
2. how could I avoid deleting the space after the variable-field, I need it

Best regards
Dietmar

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

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

Dietmar Brueckmann

Cindy M -WordMVP- said:
Hi Dietmar,

aRange.MoveEnd wdCharacter, 1

Thank's a lot. This solves problem I. One unecessary Select can be deleted.

Problem II:

aRange.Delete deletes the space before " 6789" but I need as a part of the
whole text. (CreateTestDoc is in my first message)

Sub TestFieldCM2()
Dim aRange As Range
Dim sRemain As String
Dim sDeleted As String

CreateTestDoc
'1. looking for the remaining text
Set aRange = Application.Documents(1).Fields(1).Result

aRange.MoveEnd wdCharacter, 1 ' sugg. Cindy M.
aRange.Collapse wdCollapseEnd
aRange.MoveEnd wdCharacter, 5
sRemain = "»" + aRange.Text + "«"

'2. delete from begin to end of first field

Set aRange = Application.Documents(1).Fields(1).Result
aRange.MoveEnd wdCharacter, 1 ' sugg. Cindy M.
aRange.MoveStart wdStory, -1
sDeleted = Replace("»" + aRange.Text + "«", Chr(13), "~")

aRange.Delete ' Problem II: the space before " 6789" is also deleted

Selection.WholeStory
Selection.Collapse wdCollapseStart
Selection.MoveEnd wdCharacter, 4

MsgBox "After delete=»" + Selection.Text + "« sDeleted=" + sDeleted + "
sRemain=" + sRemain

End Sub
 
C

Cindy M -WordMVP-

Hi Dietmar,
aRange.Delete deletes the space before " 6789" but I need as a part of the
whole text. (CreateTestDoc is in my first message)
I'm not clear on everything you're trying to do, or where the number comes
from. But if the space is part of what you're searching for, then it would
be part of the range, wouldn't it? So you may need to move the start of the
range one character to the right?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

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

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