I'm quite sure that a single regular expression Find + Replace All can do the reformatting task. But I have troubles understanding the requirements of the task.
Would you please create two examples files, pack them with ZIP or RAR into an archive and upload this ZIP/RAR archive as attachment to your next post. The first example file should contain the input example for the macro - the X12 line(s). The second file should contain the wanted output of the macro for the first file. That would make it much easier for us to understand the requirements and code the macro.
For example the
Perl regular expression search string
(.{105}[^\w\s])(?!\n) and the replace string
\1\r\n used in a Replace All could do the job.
(...
) marks (tags) the string found by the expression inside the round brackets for back referencing (re-use) which is done here in the replace string by
\1
.{105} finds exactly 105 characters not including carriage return and line-feed.
[^\w\s] is a negative character set definition for a single character. The 106th character should not be a
- \w ... a word character which includes all letters in all languages in any case (not just a-z), all digits and the underscore
- \s ... a whitespace character which includes space, horizontal tab, carriage return, line-feed and some more whitespace characters from which you most likely never heard and we therefore can expect not existing in your files.
(?!\n) is a negative lookahead for the next character which is evaluated, but not selected. After 106 characters there should be no line-feed. (This is correct even for DOS terminated files although the next character stored on disk is a carriage return.) Lines with exactly 106 characters and then the line termination follows are ignored because of this negative lookahead.
The replace string replaces the 106 characters matched by the same 106 characters (= change nothing) and adds a carriage return + line-feed.
Therefore this Perl regular expression Replace All inserts a DOS line termination after 106 characters if the line is longer than 106 characters and the 106th character is not a letter, digit or whitespace character.