• VBA to find where Word will put a line break

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » VBA to find where Word will put a line break

    Author
    Topic
    #485307

    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.

    Viewing 3 reply threads
    Author
    Replies
    • #1348815

      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…

      • #1348819

        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.

    • #1348844

      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

      • #1348868

        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/msoffice/use-words-no-width-optional-break-character-to-facilitate-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!

        • #1348907

          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?

    • #1348968

      Try ChrW(8204) instead. That character code is in the unicode set, not the ascii set.

    • #1348996

      Andrew

      That fixed it, thank you.

    Viewing 3 reply threads
    Reply To: VBA to find where Word will put a line break

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

    Your information: