Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Oct 2002
    Location
    Lakewood, New Jersey, USA
    Posts
    187
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

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

    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.

  3. #3
    2 Star Lounger
    Join Date
    Oct 2002
    Location
    Lakewood, New Jersey, USA
    Posts
    187
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

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

    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.

Posting Permissions

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