PDA

View Full Version : [SOLVED:] Writing correct string syntax



Roderick
07-31-2018, 06:16 AM
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?

macropod
07-31-2018, 06:39 AM
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

Roderick
07-31-2018, 07:56 AM
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.

gmaxey
07-31-2018, 07:59 AM
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

gmaxey
07-31-2018, 08:05 AM
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

Roderick
07-31-2018, 03:23 PM
The guidance is much appreciated.

Thank you.