Share via

macro to remove double quotes?

Anonymous
2012-04-16T10:57:03+00:00

I'm trying to devise a macro that will remove the double quotes around the insertion point in a document. The point I have got to so far is as follows:

Sub DeleteNearestDoubQuotes()

  Application.ScreenUpdating = False

  ActiveDocument.Bookmarks.Add Name:="LastPosition"

  Selection.Find.ClearFormatting

  With Selection.Find

    .Text = """

    .Replacement.Text = ""

    .Forward = False

    .MatchWildcards = False

    .Wrap = wdFindStop

    .Execute Replace:=wdReplaceOne

  End With

  With Selection.Find

    .Text = """

    .Forward = True

    .Execute Replace:=wdReplaceOne

    .Text = ""

  End With

  Selection.GoTo What:=wdGoToBookmark, Name:="LastPosition"

  ActiveDocument.Bookmarks(Index:="LastPosition").Delete

  Application.ScreenUpdating = True

End Sub

This actually worked the first time I tried it, but not subsequently. What I then discovered was the the code <.Text = """> automatically got changed in the VBA window to <.Text = """"> each time I tried to type it.

I'd had my doubts from the start that " (enclosed in " ") was acceptible in code for the quote character, and that I should instead be using the numbered character code for the quote characters (that would be left and right curly quotes?), but I don't know the right way to do this.

Could some kind person put me on the right path?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2012-04-16T11:57:31+00:00

The following will remove the quotes from around the text containing the cursor position whether they are single or double, straight or smart.

Sub RemoveQuotes()

Dim oRng As Range

Set oRng = Selection.Range

With oRng

    .MoveEndUntil Chr(34) & Chr(148) & Chr(39) & Chr(146), wdForward

    .End = .End + 1

    .MoveStartUntil Chr(34) & Chr(147) & Chr(39) & Chr(145), wdBackward

    .Start = .Start - 1

    .Characters.First.Delete

    .Characters.Last.Delete

End With

End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2012-04-16T12:37:58+00:00

    Exactly what I needed. Many thanks.

    Was this answer helpful?

    0 comments No comments