Results 1 to 9 of 9
  1. #1
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts

    VBA to find where Word will put a line break

    How can I get VBA to determine the point where Word will place a line break in a string? I want to control where the line breaks, by finding the last instance of a certain character before Word’s line break and replacing that character with “Chr(13)”. The idea is to have a string formatted before it prints.
    Stylus

  2. #2
    3 Star Lounger
    Join Date
    Apr 2012
    Posts
    240
    Thanks
    3
    Thanked 24 Times in 24 Posts
    Sorry for my ignorance Stylus, but does Word actually insert line breaks? I thought it used some kind of wrapping dependant on where the Margins were, to allow the document to flow easily when fonts are changed, words are added...

  3. #3
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Yes, Word wraps dependent on where the margins are - at the margin, regardless of the content it is wrapping.

    I want to use VBA to insert a line break so that I can control where in the text the break happens. If I can figure out where the word wrap will happen, then I can search back through the text string from the wrapping point, until I find the spot where I prefer the break to happen.

    Here's my situation: I have a string of varying length. It contains a series of data, separated by a forward slash. Sometime this string is so long that Word wraps the line. When that happens, the line is broken not at a slash, but anywhere. Often the wrap happens in the middle of a bit of data.

    If I know where Word will wrap the line (if it's too long), then I can search backwards from that point until I find the first slash, and then replace that slash with a line break. That way, when the string actually prints, the line will break without splitting one of my bits of data.
    Stylus

  4. #4
    3 Star Lounger
    Join Date
    Apr 2012
    Posts
    240
    Thanks
    3
    Thanked 24 Times in 24 Posts
    Assuming that your data string will include spaces, you could use non breaking spaces.
    See http://office.microsoft.com/en-us/wo...001044897.aspx

    If that's not the case you may want to switch to a mono space font like courier to ease calculations, since most Word fonts are proportional and the space used by letters varies.

  5. #5
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    BigMac is on the right track but I don't think non-breaking spaces is going to work.

    There is a character for this purpose. It is called a No-Width Optional Break and can be found in Insert > Symbol > Special Characters.

    I would try replacing every forward slash with a forward slash + No-Width Optional Break
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #6
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    BigMac56 & Andrew

    Thank you. The thing is that the slashes are for human eyeballs, so replacing them won't work.

    However... I could combine the slash with the NWOB! I just googled that thought and found an article at TechRepublic to that effect:

    http://www.techrepublic.com/blog/mso...-wrapping/5385

    Now all I have to do is chose whether I want a slash at the end of a line or at the beginning of the next line. Neither ideal, but much better than splitting my data!

    I like this tool a lot, it's a kind of conditional formatting that makes it possible to build strings with VBA, and then print 'em with no fear of Word's 'Sword of Doom' word wrapping.

    Thanks all for your input!
    Stylus

  7. #7
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    aaaah, I was a bit hasty there...

    By recording a macro I found that the code for the No-Width Optional Break symbol is 8204. I attempted to insert that into my macro-generated string by writing

    StringName = blabla & "/" & Chr(8204). This compiled ok, yet the result baffles me: the entire string either fails to print or prints out blank!

    So: am I using the wrong method for inserting the character, or is there something else afoot?
    Stylus

  8. #8
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Try ChrW(8204) instead. That character code is in the unicode set, not the ascii set.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  9. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    Stylus (2012-09-14)

  10. #9
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Andrew

    That fixed it, thank you.
    Stylus

Posting Permissions

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