Consulting

Results 1 to 7 of 7

Thread: How not to copy line numbers when select text from auto-numbered lines

  1. #1
    VBAX Regular
    Joined
    Oct 2023
    Posts
    8
    Location

    How not to copy line numbers when select text from auto-numbered lines

    I'm posting this workaround for others as it's been incredibly annoying to copy a list line and have it also copy the line number that I did not select for copy. Actually, I'm surprised that Microsoft has not provided a fix for this problem; even for Microsoft this is a bad problem to leave for a decade or more.
    The issue is this:
    Create a numbered list like:
    1. A line that I'll want to copy and paste into another application
    2. Another line that I'll want to copy and paste into another application (like putty unix commands)

    When I select with the mouse "A line that I'll want to copy and paste into another application" and paste it in putty, it pastes:
    "1. A line that I'll want to copy and paste into another application" which of course will cause an error because I did not select nor want the "1. "
    I had tried the "ConvertNumbersToText" Word method and that sort of works, but it's a one way conversion. One can't easily go back to automatic numbering after converting to text, and doing the conversion also removes text attributes like "bold". I had also read that in some cases it removes numbers altogether. So, this was sort of useable if I made a copy of the original document and worked with the scratch document without text emphasis.

    My next try was to just add a space to the beginning of every list line because when you do that it solves the problem and doesn't select the number, period and space as long as you don't also copy that space that you added to the beginning of the line. But then the answer emerged. Don't add a space; add a non-printing character...one that won't print and one that has no width. That is the "soft hyphen". This works fantastic so far.
    So, I just search and replace the entire word document and add a soft hyphen to the beginning of every line. It shows no visual effect at all and line breaks are going to happen toward the end of line anyway, not the beginning.
    To employ this, you can do a search and replace like below.

    Search:   ([!^13]{1,})
    Replace: ^-\1
    You must check the "Use Wildcards" checkbox under the "more" button in search and replace dialog

    That will demonstrate how this works. Once you replace every line in your Word document with a soft hyphen "^-" at the start of line, you can now select the whole numbered line and paste it elsewhere without the stupid number, period and space as long as you use the right method to copy the line. And that is described under the "Caveats" below.

    To make this more usable, you can create a macro. Definition below. Then to make it really useable, set that macro to an F-key

    Sub AddSoftHyphen()
    With ActiveDocument.Content.Find
      .MatchWildcards = True
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "([!^13]{1,})"
      .Replacement.Text = "^-\1"
      .Wrap = wdFindStop
      .Execute Replace:=wdReplaceAll
    End With
    End Sub
    Now add the macro to a shortcut key combination like Alt - F11

    After composing a document using autonumbering, then at the end run this macro and click save.
    Or, open an existing document and run the macro. You can either save or not save the changes, but if you do, there should be no noticeable difference in the way the document looks or behaves.
    Caveats -
    1. Using this method, it's best to have the Word Option "Display "Optional Hyphens" unchecked so that the soft hyphens are not visible on your screen. That is found at File, Options, Display, and uncheck the "Optional Hyphens" checkbox. You can enable it just to see if hyphens are in a file already, but it's easiest to use this method with them being invisible. And if you like them displayed, that's fine, but don't select that soft hyphen when you copy the item text, otherwise you end up getting the number, period, space again like you don't want.
    The reason to keep those hyphens hidden is that if you don't have them displayed, it's easier to use the mouse to copy text and not have to try to avoid copying the hyphen character.
    2. Don't use double and triple mouse click to copy a text line because it will also grab the hidden hyphen even if it's not being displayed. Use the mouse and click and drag.
    3. To allow better control of click and drag when selecting with the mouse, go to File, Options, Advanced, and uncheck the "When selecting, automatically select entire word".
    When that setting is checked, it gives you less control when selecting and can end up copying the soft hyphen characters.
    4. When selecting the line with the mouse, don't select the return or blank space after the last character in a line. If you do, it will copy the list number like you don't want it to.

    Hope that helps. Let me know if I missed something in this description.
    Last edited by Aussiebear; 10-28-2023 at 01:19 PM. Reason: Added code tags to supplied code

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Jeff,

    Your work around seems like a lot of effort. Now that I have a better idea what you are trying to do, how about:

    Sub CopyListParagraphWithTheNumber()
     Dim oPar As Paragraph
      Set oPar = Selection.Paragraphs(1)
      oPar.Range.ListFormat.RemoveNumbers
      oPar.Range.Copy
      ActiveDocument.Undo 1
    lbl_Exit:
      Exit Sub
    End Sub
    Assign your shortcut to that. After running, paste into your new document or whatever.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Oct 2023
    Posts
    8
    Location
    Quote Originally Posted by gmaxey View Post
    Jeff,

    Your work around seems like a lot of effort. Now that I have a better idea what you are trying to do, how about:

    Sub CopyListParagraphWithTheNumber()
     Dim oPar As Paragraph
      Set oPar = Selection.Paragraphs(1)
      oPar.Range.ListFormat.RemoveNumbers
      oPar.Range.Copy
      ActiveDocument.Undo 1
    lbl_Exit:
      Exit Sub
    End Sub
    Assign your shortcut to that. After running, paste into your new document or whatever.
    I see why that didn't work for me. I was expecting your macro to act on the entire document. Then I saw this thread that you are in - "copy text without paragraph number".
    I would find it very cumbersome to run your macro for every line I wanted to copy. It is not a viable solution for what I need to do.
    However, the solution I posted in this thread is a viable solution if you are open-minded to try it. It converts every list line to "copy-able" without the number, period, and space", or whatever the list character(s) happen to be.
    So once applied to a document, I can copy at will without having to run a separate macro every time.


    Thanks,
    Jeff

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Jeff,
    I was open to try it. That is how I formed the opinion that it seemed to take a lot of effort. Just an opinion. Much like the opinions that others post here. Solicited or unsolicited.
    I am certainly glad that your solution works for you.

    For others reading this thread. With the macro method, you click in the paragraph you want to copy. Anywhere in the paragraph. You don't have to display or hide characters, you don't have to be careful about what you do or don't select. You don't have to change any settings. Just click. You then run the macro. You will have to decide for yourself which is more viable. 1) After carefully selecting the text to copy right click and select copy or press Control C 2) After clicking in the paragraph execute the macro. To make that simple you can use the shortcut that Jeff proposes Alt - F11
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Regular
    Joined
    Oct 2023
    Posts
    8
    Location
    Quote Originally Posted by gmaxey View Post
    Jeff,
    I was open to try it. That is how I formed the opinion that it seemed to take a lot of effort. Just an opinion. Much like the opinions that others post here. Solicited or unsolicited.
    I am certainly glad that your solution works for you.

    For others reading this thread. With the macro method, you click in the paragraph you want to copy. Anywhere in the paragraph. You don't have to display or hide characters, you don't have to be careful about what you do or don't select. You don't have to change any settings. Just click. You then run the macro. You will have to decide for yourself which is more viable. 1) After carefully selecting the text to copy right click and select copy or press Control C 2) After clicking in the paragraph execute the macro. To make that simple you can use the shortcut that Jeff proposes Alt - F11
    True; best to let folks decide which method works for them. Since you've listed what you think are the faults of my method, I'll rebutt.
    1. Selecting text is not a difficult thing, but you have to use click and drag and you can actually select the invisible end of line character along with text. I was wrong that that causes any issue. By default the "optional hyphen" is not displayed, so that won't cause any issue. In fact that is what makes this method work so well. Despite that it's there, when you select the line by a left button mouse swipe from left to end of line, you can just copy the whole line. Just don't use double or triple mouse click to copy. If clicking and dragging to copy a line is not your thing, then don't use this method. I never use double and triple mouse click so it's natural for me to copy with mouse drag. I sometimes use single click at start of line and shift click at the end to copy. That also works fine.
    2. Your method requires the macro to be ran every single line. That's the primary objection I have to your method. In fact I'd rather use the "ConvertNumbersToText" method than your macro because it fixes the problem with one execution throughout the entire file. The problems with that method though are that it modifies the file in one direction and removes autonumbering. I use copies of the docs I work with, so that would not be an issue. But imagine that I'm doing an installation and following keyboarding instructions in a Word document that have 200 line items. I've got to run your macro 200 times in addition to selecting the line. With my method, I only run the macro once for the entire file and just select autonumbered lines as I normally would and paste them into putty. Works seamlessly. And if the author completes the document is is nice about it, then s/he can run the macro, then save changes and I wouldn't have to run the macro at all. I would just select text like normal "for me".

    Thanks though for addressing my thread and let those decide which method they like. It's nice to have multiple options.

    Jeff

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Jeff,

    "Your method requires the macro to be ran every single line" You object to that. If a person has no objection to running macros (yours or mine) then it really doesn't matter. Selecting text and copying it is the process of executing a Word command (a built-in command using Ctrl+C or a mouse click event) i.e., the Copy command. Selecting text and running a macro is also the process of executing a command (a custom command). You can run that command with a shortcut e.g., your Alt+F11 or you could add a new button to the text menu on the mouse.
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Regular
    Joined
    Jan 2022
    Posts
    16
    Location
    Hi, Jeff_VB and Greg! Your ideas are remarkable and very helpful! Thank you! I've slightly modified Greg's code to make it possible to copy both a single paragraph (the cursor is on) and a number of selected paragraphs of a list. The modification is nothing, the idea is everything:

    Sub List_Copy_No_Numbers()
    Dim oRng As range
    Set oRng = selection.range
    oRng.ListFormat.RemoveNumbers
    oRng.Copy
    ActiveDocument.Undo 1
    lbl_Exit:
    Exit Sub
    End Sub

Tags for this Thread

Posting Permissions

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