Consulting

Results 1 to 6 of 6

Thread: Writing correct string syntax

  1. #1

    Writing correct string syntax

    In a procedure in Word VBA I'm searching for the elements of a field in my document. This is what I have at the moment to search for when I display the field using ALT+F9:
    strSearch = "StyleRef 1 \s"
    The initial command to search for it works correctly. Once found, I now have to replace just the figure 1 with the name of a style and which must also include double quotes at either end. This is because the style name is a multi-word item.

    I'm getting in an awful tangle with my '&' signs to concatenate things as well as with my double quotes. Basically, I have to end up with StyleRef "My Style Name" \s bracketed by " signs at either end. This is an example of what it should end up like:
    strReplace="StyleRef "My Style Name" \s"
    Can anyone get me out of this muddle, please?

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Presumably, your STYLEREF field is in the page header. In that case, you might use code like:
    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument
      .ActiveWindow.View.ShowFieldCodes = True
      With .StoryRanges(wdPrimaryHeaderStory)
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "^d STYLEREF 1 \s"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchCase = False
          .Execute
        End With
        If .Find.Found = True Then
          With .Fields(1).Code
            .Text = Replace(.Text, "1", """My Style Name""")
          End With
        End If
      End With
      .ActiveWindow.View.ShowFieldCodes = False
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Thanks, Paul.

    That solved the problem.

    Based on your sugestions, my code now looks like this:
      strSearch = "StyleRef 1 \s"
      strReplace = "StyleRef " & """GA Numbered Heading 1""" & " \s"
    and when the replace action has been executed, the field code looks like this in the body of the document:
    StyleRef "GA Numbered Heading 1" \s
    ...exactly as it should be. It was the use of those three sets of double quotes before and after my style name that did the trick.

    What I can't understand is why three sets of quotes did it for me?

    Thanks again.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    I usually find it easier to apply literal quotes in code using Chr(34).

    Sub Demo1()
    Dim oRng As Range
    Application.ScreenUpdating = False
      With ActiveDocument
        .ActiveWindow.View.ShowFieldCodes = True
        Set oRng = .StoryRanges(wdPrimaryHeaderStory)
          With oRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "^d STYLEREF 1 \s"
            If .Execute Then
              With oRng.Fields(1).Code
                .Text = Replace(.Text, "1", Chr(34) & "My Style Name" & Chr(34))
              End With
            End If
        End With
        .ActiveWindow.View.ShowFieldCodes = False
      End With
      Application.ScreenUpdating = True
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Sub Demo2()
      MsgBox "Hello World" 'No quotes
      MsgBox """Hello World"""  'Quotes  "" represents a literal quote mark
      MsgBox Chr(34) & "Hello World" & Chr(34) 'as does Chr(34)
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    The guidance is much appreciated.

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •