Consulting

Results 1 to 5 of 5

Thread: Solved: Copy the text from WORD to EXCEL

  1. #1

    Solved: Copy the text from WORD to EXCEL

    Hi All,

    I would like to search the text which contains ".doc" (e.g. Verison1.doc) in WORD File and then copy to Excel File. I have record the marco in WORD first and then modify the code in EXCEl marco. However, in my code, it has error in "wdCharacter".
    [vba]Set wdRng = wdApp.Selection
    'Find the Version Number in Doc
    wdRng.Find.ClearFormatting
    wdRng.Find.Text = ".doc"
    wdRng.Find.Forward = True
    wdRng.Find.Execute
    wdRng.MoveRight Unit:=wdCharacter, count:=1
    wdRng.MoveLeft Unit:=wdCharacter, count:=1, Extend:=wdExtend
    wdRng.Selection.Copy[/vba]


    Thanks&Regards,
    Ann
    Last edited by Ann_BBO; 11-17-2009 at 01:20 AM.

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Excel is not reconciling the Word Constant wdUnits, probably because of late binding. (Someone please correct me if I'm wrong)

    So you have to give the Enum constant.

    [vba]Sub FindVersionNum()
    'Late binding. Just a guess here.
    Dim iWord As Object
    Set iWord = GetObject(, "word.Application")

    Set aDoc = iWord.ActiveDocument
    Set Rng = aDoc.Range

    With Rng.Find
    .Text = ".doc"
    .Forward = True
    .Wrap = wdFindStop
    .Execute
    End With

    If Rng.Find.Found Then
    'Specify the Word Constant
    Rng.Collapse 1
    Rng.MoveStart 1, -1
    MsgBox Rng
    End If

    End Sub
    [/vba]
    You can find the constant number in the Word object browser. Search for WdUnits.

    I've rewote your code using Range. (You know how we Word coders hate using Selection. ) If you decide to use Range, be sure to set the WORD RANGE, as Excel will think you're trying to use it's Range.

    David


  3. #3
    Hi Tinbendr,

    Using your code, it will output the constant number as you said that before. However, how to copy the "Verison1.doc" into Excel. I have attached the Word file as a example. Would you mind to show the demo how to copy the "Version1.doc" into Excel sheet (say in A1 cell). Thanks.

    Thanks,
    Ann

  4. #4
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Sorry, I thought it only returned the number of the version.

    David


  5. #5
    Hi Tinbender,

    No, it totally solves my problem. (Maybe my english is poor so that let you misunderstanding.) BTW, Thanks x 10!!!

    Thanks,
    Ann

Posting Permissions

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