VBA: problem with triming line in word

  • i have text document with lines 1000 lines long which is copied to word


    first 5 charater as space
    next 45 non space character
    other 950 charater as spaces


    now i need to trim the first and the last spaces . After selecting the line and copying it to a string variable a


    ltrim$(rtim$(a)) does not work
    or ltrim(rtim(a)) does not work
    also trim(a) does not work


    how do i trim such lines pls help me


    thanks in advance

  • OzGrid is devoted primarily to Excel, but I'll see if I can help since I use Word frequently also.


    First, if this is a single document rather than a repeating problem, I would simply use the Word's Find and Replace (CNTR+H or EDIT > REPLACE) to shape up the document. (If it's a repeat problem, then I still would do this, but I'd turn on the Macro Recorder while I did the following to see what VBA code it records.)


    Before you begin, I'd suggest telling Word (if you haven't already) to display non-printing characters (you may have a paragraph symbol in your tolbar that will do this as a toogle. Otherwise go to OPTIONS > VIEW > and check "All" by "Non-Printing charaters"). This will help you see what you are dealing with.


    I am assuming that the text (wanted content) in each of the 1,000 lines consists of words that shjould be separated by single spaces. If there a places where multiple consecutive space are needed, youneed to be careful how you apply the following.


    First, I would bring up the REPLACE dialog box as described earlier. Then in the top "Find What" box I would enter 5 spaces. Put nothing in the bottom box and click "Replace All" Then do this again (and again until it finds nothing more to replace) to get rid of more of the long space sequences at the end of each line.


    Next, Replace all double spaces (type "space space" in the Find What box) with a single space (type a single "space" in the Replace With box) and click Replace All. Repeat this until all multiple spaces are gone -- the Replace All finds nothing more to replace.


    This should get file looking a lot better width-wise. Then, using the "Special" dropdown list at the bottom of the Replace dialog box, change all "space paragraph mark" or "space Manuel Line Feed" (depending on what your file has given you) to a "Paragraph Mark" (or "Manuel Line Feed") only.


    Then (this is an important step) Replace consequtive "Paragraph Marks" (put two in the top box to some charater set (like "ZZZZZ") that will not be found in the file. This step will help preserve paragraph integrity.


    Next, Replace any remaining "Paragraph Marks" with Nothing (the same way you replaced multiple spaces with nothing at the start.


    Then, Replace the "ZZZZZZ" with a single or double "Paragraph Mark" (choice of oen or two depends on if you want a blank line between paragraphs).


    Next, Replace all double spaces (type "space space" in the Find What box) with a single space (type a single "space" in the Replace With box) and click Replace All. Repeat this until all multiple spaces are gone -- the Replace All finds nothing more to replace.



    I strongly recommend that you make a backup copy of your original file before doing this. Or, make sure that you do not Save until you are sure you have what you want.


    Hope this helps. Good luck.

  • If you have Excel you could also try this:


    Open the Original Text File in Excel.
    If its filename is in the form something.txt then when you open it, Excel will bring up Text Import dialog box.
    Pick Fixed Width as the data type
    On the next screen make sure there are no break lines and click Finish.


    The text file should open in Excel with Line 1 in cell A1, Line 2 in cell A2, etc. without the leading or trailing spaces.


    Save this file as a Text file (change the name), then open the new file in Word and you lines should only be 45 characters long.



    .....Ralph

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!