Copy And Paste A String Of Numbers/text Automatically In word

  • Hi!


    I have an word document which contains different 5 digit numbers. It is not in any kind of order but randomly between text.
    I am trying to copy and paste a thses string of 5 digit numbers (for ex: 11200,11300,02500) from a word document to a new document. I tried to do a macro but didn't quite work the way I wanted it. If anyone can help me create a macro/vb script to do this - that'd be great.


    thanks!
    Dan

  • Re: Copy And Paste A String Of Numbers/text Automatically In word


    Are they always separated by commas, and is there only numbers, or is there text too ? Can you please attach an sample file of your data and a sample of the way the resulting data should be displayed ?

    Regards.
    Daniel

  • Re: Copy And Paste A String Of Numbers/text Automatically In word


    Hi Daniel:


    The following is a sample of word document. The text may or may not be longer than 1 line/row but the numbers always start from the same column number.


    *****
    Sample File:


    Identification number for a product 1.


    20345


    Identification number for a product 2.
    Bar code # for item 2.


    20099


    Customer receipt # for company XYZ


    20323


    Customer discount #

    20340[/B]


    ********
    The result should look like:


    20345
    20099
    20323
    20340

  • Re: Copy And Paste A String Of Numbers/text Automatically In word


    The following macro opens the source document and copies the numbers into a new file. I suppose the numbers are the first characters of a paragraph. Let me know if this is not exact.

    Regards.
    Daniel

  • Re: Copy And Paste A String Of Numbers/text Automatically In word


    Thanks Daniel: Works great! by the way, what if the numeric starts at the 20th character?


    I appreciate you help![hr]*[/hr] Auto Merged Post Until 24 Hrs Passes;[dl]*[/dl]Another question: The output file should be the same as before but what if there is a text in front of the numeric and the numeric always starts from the same col. (for ex: col: 25).


    Thanks in advance for your help.


    Identification number for a product 1.


    ID NUMBER: 20345


    Identification number for a product 2.
    Bar code # for item 2.


    BAR NUMBER: 20099


    Customer receipt # for company XYZ


    CUSTOMER#: 20323[hr]*[/hr] Auto Merged Post Until 24 Hrs Passes;[dl]*[/dl]Another question: The output file should be the same as before but what if there is a text in front of the numeric and the numeric always starts from the same col. (for ex: col: 25).


    Thanks in advance for your help.


    Identification number for a product 1.


    ID NUMBER: 20345


    Identification number for a product 2.
    Bar code # for item 2.


    BAR NUMBER: 20099


    Customer receipt # for company XYZ


    CUSTOMER#: 20323[hr]*[/hr] Auto Merged Post Until 24 Hrs Passes;[dl]*[hr]*[/hr] Auto Merged Post Until 24 Hrs Passes;[dl]*[/dl]Sorry for the multiple post!

  • Re: Copy And Paste A String Of Numbers/text Automatically In word


    No need to be sorry, the message is misleading ;)
    The macro below should do the trick. It is even possible to extract each numeric string wherever it is (no need to begin at the same place).

    Regards.
    Daniel

  • Re: Copy And Paste A String Of Numbers/text Automatically In word


    Daniel: The code worked perfect. But for some reason the last numeric repeats twice. Not a biggie though!


    You mentioned that I can extract all the numeric of say 6 digits from anywhere in the doc without having to state the col number. I was just curious how it can be done - in case something like that comes up in the future or the number lining is off by a column or two.


    Also, is it possible to make script available to any word file document since I'll be using the script for more than 1 word documents each named differently and in different locations.


    Thanks for the help.

  • Re: Copy And Paste A String Of Numbers/text Automatically In word


    Here are the macros. Both allows you to choose your input file. The first macro is the same as the preceding one (I remove the duplicate problem) :


    The next one picks up each 6 digits sequence whatever its position :

    Regards.
    Daniel

  • Re: Copy And Paste A String Of Numbers/text Automatically In word


    Hi Daniel:
    I hate to ask you this but whenever I try to run the macros, it highlights the Sub ExtractEveryWhere() and selects the .FileDialog with an error message: Method or Data not found.


    I tried installing COMDLG32.OCX to see if it might be missing any components/files but doesn't quite seem to work. Any help??


    thanks again!

  • Re: Copy And Paste A String Of Numbers/text Automatically In word


    I did a search on the internet and can't find such a case. I tested it with Word 2007, so I can't see any difference. Look at Tools / References in VBE window.
    I have the following references checked :
    - Visual Basic for Applications
    - Microsoft Word 12.0 Object Library
    - OLE Automation
    - Normal
    - Microsoft Word 12.0 Object Library
    Do you have the same (Normal may refer to the macros of the "Normal" template) ?


    Please, try the following macro :

    Code
    Sub test()
        With Application.Dialogs(wdDialogFileOpen)
            .Name = "*.doc*"
            .Show
        End With
        myFile = ActiveDocument.Name
    End Sub


    I you have no error, replace in the macros :


    Code
    With Application.FileDialog(msoFileDialogOpen)
            .Show
            .Filters.Add "Word docs", "*.doc*"
            myFile = .SelectedItems(1)
        End With
        Set DocSource = Documents.Open(myFile)
        myFile = ActiveDocument.Name


    with


    Code
    With Application.Dialogs(wdDialogFileOpen)
            .Name = "*.doc*"
            .Show
        End With
        myFile = ActiveDocument.Name

    Regards.
    Daniel

  • Re: Copy And Paste A String Of Numbers/text Automatically In word


    Thanks Daniel:


    It looks like the error for the previous script was because of my MS office version. I ran the script on a computer that had MS word 2003 - worked great! but when I ran the same macro on my computer (MS Word 2000) - it gave me that error "Method or data not found".


    I tested the test() script and worked just fine but when I replaced that with the new script, it highlighted the docSource.Activate and didn't seem to work.


    Just wondering if I did use the previous code - is there a way to make it compatible for lower MS Office version?

  • Re: Copy And Paste A String Of Numbers/text Automatically In word


    Quote


    Just wondering if I did use the previous code - is there a way to make it compatible for lower MS Office version?


    I don't think it's possible.
    I am quite sorry not to have tested the code I gave you. If you use the second method (that of the "test" macro), you need to replace :

    Code
    Set DocSource = Documents.Open(myFile)


    with

    Code
    Set DocSource = ActiveDocument

    Regards.
    Daniel

  • Re: Copy And Paste A String Of Numbers/text Automatically In word


    Great No error!!! The code seems to work just fine but I'm not sure why it copies not only the 6 digit numbers but also other numerics within the documents which are either 1 or 2 digits (like the question #'s). Also it doesn't seem to work on the lines where tab key is used ... It looses a number or two.


    Thanks for the help!

  • Re: Copy And Paste A String Of Numbers/text Automatically In word


    Please, can you attach a small sample file which generates ill treatment ?

    Regards.
    Daniel

  • Re: Copy And Paste A String Of Numbers/text Automatically In word


    Well, OK, I'm quite new with Word programmation. Strangely, the tab marks are considered as numerals. I modified the macro in accordance. I have not tested other special characters, so you may have other misbehaviours. Here is the macro "ExtractEveryWhere". If you need the same modification in an other macro, please, tell me.

    Regards.
    Daniel

  • Re: Copy And Paste A String Of Numbers/text Automatically In word


    Thanks Daniel: Worked like a charm :)


    I really appreciate your help.

Participate now!

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