2

This line of code...

    MyRange = MyRange.Paragraphs(1).Range 'Intended to select the paragraph
         ' that contained the previous range

...enters text right where the previous range was. I figured it out via pressing F8 in VBE for step by step execution. It concretely pastes the paragraph that the range defined in this line is supposed to encompass. This is driving me crazy as the line is just assigning a new range value to MyRange...

Why is doing so??

For example, having ths text selected:

"Author: CMV Subject: Highlight Date: 12/2/2014, 2:23:41 PM [As a VB /VBA developer you] glue the various"

Results in

"Author: CMV Subject: Highlight Date: 12/2/2014, 2:23:41/Author: CMV Subject: Highlight Date: 12/2/2014, 2:23:41 PM

[As a VB /VBA developer you] glue the various"

The text pasted corresponds to the paragraph (you can't see paragraph marks here) referred in this code line. It is pasted over the string the code searches for previously. I'm inserting the whole code just in case:

Set MyRange = Selection.Range
    With MyRange.Find 'Busca _AM o _PM
        .ClearFormatting
        .Replacement.ClearFormatting
        .MatchWildcards = True
        .Text = " [AP]M"
    .Execute
End With
MyRange = MyRange.Paragraphs(1).Range

An issue with these previous lines is posted here (in case it matters): VBA: Why is Range.Find.Execute deselecting text in a Word doc? (posted as there was other issue with them, but it seems unrelated to this to me)...

Can anyone figure out what's going on here? Please?

Community
  • 1
  • 1
Martin
  • 414
  • 7
  • 21
  • 2
    You have to use `Set myRange = newRange`. I'm searching for a duplicate. – RubberDuck Dec 30 '14 at 18:07
  • 2
    possible duplicate of [Difference in type between using and not using Set keyword](http://stackoverflow.com/questions/11089040/difference-in-type-between-using-and-not-using-set-keyword) – RubberDuck Dec 30 '14 at 18:10
  • 1
    Ooh, it all makes a lot of sense! I wasn't redefining the range but its content. In the case of the code I posted it seems that text is inserted because MyRange variable's **content**, which previously was equal to "PM", it now has the value of the entire paragraph that contained that "PM". So even though Myrange **range** is not redefined it now contains the entire previous paragraph content, which replaces what it was just "PM"... Also, even though the range has not been redefined per se, as the content is now bigger, the range has changed accordingly... Interesting! Can't I reward you? – Martin Dec 30 '14 at 19:23
  • Bingo! You've got it @Martec! – RubberDuck Dec 30 '14 at 19:24
  • I also voted for the link you posted as it is useful. Not sure it is a duplicate though... but can't find a way to reward or mark your tips as an answer... Many thanks @RubberDuck!! – Martin Dec 30 '14 at 19:31
  • It's the same concept, it's just Excel instead of Word. Close enough in my book. And seeing you *just get it* was plenty award for me. – RubberDuck Dec 30 '14 at 19:36
  • Just wanted to post a smile for your empathetic self-rewarding system :) – Martin Dec 30 '14 at 20:28

0 Answers0