PDA

View Full Version : Solved: Copy the text from WORD to EXCEL



Ann_BBO
11-16-2009, 11:51 PM
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".
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


Thanks&Regards,
Ann

Tinbendr
11-17-2009, 02:35 AM
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.

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

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.

Ann_BBO
11-17-2009, 03:17 AM
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

Tinbendr
11-17-2009, 04:25 AM
Sorry, I thought it only returned the number of the version.

Ann_BBO
11-17-2009, 06:40 PM
Hi Tinbender,

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

Thanks,
Ann