Unwanted Carriage Return

lorraine
NewLounger
Posts: 9
Joined: 09 Feb 2011, 22:51

Unwanted Carriage Return

Post by lorraine »

Good to see some familiar faces here from Woodys Lounge ... and a great dedication to Eileen.

I have a problem with some vba code in Word 2010 which did not occur in Word 2003. My code is:

If ActiveDocument.Bookmarks.Exists(sName) Then
Selection.GoTo What:=wdGoToBookmark, Name:=sName
Selection.Delete
End If

The problem is that in Word 2010 a trailing carriage return is left behind on deletion of the bookmark. This did not happen in Word 2003.

Any ideas how to fix it?

I found the following info (although I can't act on it as I don't have VBA 6.0 registry keys to copy from):

    "In Office 2010, VBA 6.0 was updated to VBA 7.0. VBA 7.0 settings were reset to their defaults after migration instead of automatically repopulating. This occurred because the registry settings for VBA are in a different hive in Office 2010. To correct this problem, copy the VBA 6.0 registry keys from the 6.0 hive to the 7.0 hive."

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Unwanted Carriage Return

Post by HansV »

Welcome to Eileen's Lounge!

This is not due to the change from VBA6 to VBA7, nor is it specific to macros. It is caused by a change in the way Word 2007 and later handle deleting text when the selection covers parts of paragraphs - one paragraph break in between is left behind instead of deleted.

Fortunately, there's an easy solution: replace the selection with a space, then delete it:

Code: Select all

  If ActiveDocument.Bookmarks.Exists(sName) Then
    Selection.GoTo What:=wdGoToBookmark, Name:=sName
    Selection.Text = " "
    Selection.Delete
  End If
or without actually selecting the text:

Code: Select all

  If ActiveDocument.Bookmarks.Exists(sName) Then
    With ActiveDocument.Bookmarks(sName).Range
      .Text = " "
      .Delete
    End With
  End If
Best wishes,
Hans

lorraine
NewLounger
Posts: 9
Joined: 09 Feb 2011, 22:51

Re: Unwanted Carriage Return

Post by lorraine »

Ah thanks Hans ... You are the Very Special One! I always remember your help in Woody's Lounge - whatever would we do without YOU!

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Unwanted Carriage Return

Post by HansV »

:blush: Thank you...
Best wishes,
Hans