• extra spaces in VBA find & replace (Word XP on Windows XP Home)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » extra spaces in VBA find & replace (Word XP on Windows XP Home)

    Author
    Topic
    #381036

    I’m trying to code a find & replace that will replace all instances of [customer contact] in my document with specific customer information contained in variables in my code. My problem is that all lines after the first one are appearing with an extra space at the beginning. Here’s my code:

    ‘ the following code builds a string (strFullContactInfo) containing the complete contact information
    With gCustInfo
    strFullContactInfo = .ContactFirstName & ” ” & .ContactLastName & vbCrLf & .CustomerCompany & vbCrLf & .ContactAddress
    End With

    ‘ the following code does the find & replace
    With ActiveDocument
    .Content.Find.Execute “[customer contact]”, False, True, False, False, False, True, wdFindContinue, False, strFullContactInfo, wdReplaceAll, False, False, False, False
    End With

    Can anyone help me remove these extra spaces.

    thanks!

    Taibe

    Viewing 0 reply threads
    Author
    Replies
    • #640465

      It all depends on whether the space is in your document (and therefore needs to be part of the Find string) or is in the new data. If the latter, take a look at Trim() and see if using that on strFullContactInfo solves the problem. If it’s the former, experiment with wildcard searches (interactively, in Word) until you find a reliable syntax that gets both variations but nothing else. Then you can revise your code.

      • #640727

        Guess what? The problem was in the vbcrlf that I used to place various parts of my string on new lines. The vblf part was indenting the line by one space. When I changed the vbcrlf to vbcr, problem was solved. I really don’t understand this because from what I know, a carriage return just takes the cursor to the beginning of the line, it’s the line feed that gets it to the new line. However, if this worked, I can live with it!

        Thanks for the help,
        Taibe

        • #640745

          Well, a space is a space, no matter how you break a line, but maybe it isn’t a space? Does vbCr insert a “manual line break” rather than a new paragraph? That’s the only way I can see that a “first-line indent” the size of a space might not occur after the vbCr. If you figure out the answer to this riddle, please post again.

    Viewing 0 reply threads
    Reply To: extra spaces in VBA find & replace (Word XP on Windows XP Home)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: