Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #6334
    Stefan
    Participant

    Reformat: break line at n-chars (VBScript)

    Made because of http://www.emeditor.com/modules/newbb/viewtopic.php?topic_id=156&forum=2&post_id=2556#forumpost2556

    Based on an script from mp3cdman at http:// www. haneng.com/asp-forum/Vbscript-Line-Wrap_2521.html
    But i found this algorithmen is not the best, i had mixed results. I’ll try to find an more robust code.

    Provided “as is”. No guaranty.
    Test first on non important documents! I am no programmer.

    Please comment below how it works.

    See EmEditor Help for how to use macros. Here’s an short help:
    1.) Save the code between 8< and >8 -lines as text file “somename.vbee” in EmEditor folder
    2.) use Menu “Macros > Select…” and choose this somename.vbee as current default macro
    3.) open an test file (try first on an test document!)
    and select some long lines
    4.) press F4 and enter max chars to break line into two
    Your done.

    ———–8<---------------8<-----------



    #title = Reformat
    #tooltip = Reformat: break line at n-chars
    '// Select some text and execute this macro to wrap/break the text

    str = document.selection.Text
    If str = "" THEN
    alert "Please select some text (or all) first" _
    & vbCRLF & "Then start the macro again."
    Quit
    End IF

    WrapAt = prompt( "Break line at:", "72" )
    If IsNumeric(WrapAt) THEN
    If WrapAt < 1 THEN
    alert "Enter amount of chars per line, for exemple ""72""" _
    & vbCRLF & "Please start the macro again."
    Quit
    End If
    ELSE
    alert WrapAt & " is not an number!" _
    & vbCRLF & "Please enter amount of chars per line numeric, for exempel ""72""" _
    & vbCRLF & "Please start the macro again."
    Quit
    End If

    document.selection.Text = WordWrap(str,WrapAt)


    Function WordWrap(theString, wrapLength)
    'credits to mp3cdman at http:// www. haneng.com/asp-forum/Vbscript-Line-Wrap_2521.html
    Dim tempString,lenOfString,index
    Dim lastSpace,startOfLine

    lenOfString = Len(theString)
    index = 1
    startOfLine = 1

    While index < lenOfString
    If Mid(theString, index, 1) = " " Then
    lastSpace = index
    End If
    If index = startOfLine + wrapLength Then
    tempString = tempString & Trim(Mid(theString, startOfLine, lastSpace - startOfLine + 1)) & vbCrLf
    startOfLine = lastSpace + 1

    While Mid(theString, startOfLine, 1) = " "
    startOfLine = startOfLine + 1
    Wend
    End If
    index = index + 1
    Wend

    tempString = tempString & Trim(Mid(theString,startOfLine)) & vbCrLf
    WordWrap = tempString
    End Function

    ———–>8—————>8———–

    Enjoy
    Stefan

    Comments and improvement suggestion welcome.

    #6572
    saphire
    Member

    It seems do not work on Chinese Word!
    How do i use marcos to specify WrapByChar and set margins(ex:75)?

    #29497
    Scott Daughtry
    Participant

    Question: does the macro code posted above work with the current EmEditor version? I’ve copied/pasted it into a new macro file but keep receiving a runtime error complaining about a semi-colon character missing.. when I manually add the semi-colon character to the end of the line and save the macro and re-run it, the same popup error message is displayed onscreen.

    #29499
    em-like
    Participant

    Yes the macro works with the current version.
    Did you really perform all steps (1 to 3) as described by Stefan?
    What is the semi-colon at the end of the line supposed to do?
    My guess is that you did not save the macro as described in point 1. Stefan has clearly described as …vbee save. EmEditor recognizes this as the macro is written in Vbscript. I know a semi-colon at the end only with java script, as file extension …jsee.
    Please read also the help, Mr. Yutaka Emura describes also the the possibility to use #language = “ScriptName”.

    #29500
    Scott Daughtry
    Participant

    Hello em-like

    Thank you for your reply. Taking your advice, I deleted the first attempt at the .vbee file then did the following:

    1. Selected the code between 8< and >8 -lines as text file “somename.vbee” in EmEditor folder (reformat.vbee)
    2. Opened a text file that had lines of text > 100 characters in width and ran the macro
    3. received errors regarding the Title and Tooltip; after moving them leftwards to start at column 1 those 2 errors disappeared
    4. received errors regarding the alert message at their respective vbCRLF code; after combining the multiple lines into one and removing the _ character those errors disappeared
    5. now I’m stuck with an error stating ‘Object required: ‘document’ on line 7, which is this line
    str = document.selection.Text

    This is why I asked if this macro works with the current EmEditor version. I’m not trying to be difficult – I’m following the steps outlined above and encountering errors. I’m not a VBscript programmer – the languages that I know do not have syntax similar to VBscript and I’ve owned EmEditor for a week and trying to divorce myself from my longtime editor that has features that I rely on that EmEditor lacks.

    #29501
    em-like
    Participant

    Hello Scott Daughtry,
    since i had read at your message … semi-colone … , and the confusion may arise quickly, I have reported at short notice on your case. Usually only one script language or only the own macro language can be processed by the editor. EmEditor can handle both vbscript and javascript.
    To see if the script still works after 15 years, I copied the commands and removed unnecessary blank lines. As a test I used only a few sample sentences. The script ran without errors. So I can say that it works in general, but I didn’t do any detailed tests. Therefore I can’t say anything about error catching etc.. Maybe Stefan will get back to you in the meantime, as the author can best explain and understand what this script does. Due to time constraints I can’t take a closer look at the script at the moment, as I am on my way to a longer business trip. However, I wanted to briefly share the hint with the two different script languages.

    #29502
    Scott Daughtry
    Participant

    em-like,

    After closing/restarting EmEditor a few times the problem seems to have sorted itself out and the mixture of macros are now working. If the problem returns I will add another post to this topic. Thank you for sharing that macro with the community, and thank you all that lent me assistance. Safe travels to you!

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.