Excel to Word

A

atrscomputers

Hey,


I'm trying to make a macro that reads a column in Excel.I've only ever
done VBA for excel and this is confusing me. I need the macro to read
the cell in excel and find it in the word document, then copy the 20
charactors to the right of the found numbers and paste them in excel.
And loop this for a complete column


I know thats a little confusing but if you need any clarification or if



you could help please let me know.


Andrew
 
D

David Lloyd

Andrew:

Below is some sample code to accomplish something similar to what you
described. Obviously, you will have to adjust it to your specific needs.
You will need a reference to the Word Object Library, of course.

Public Sub FindWordString()
Dim i As Integer
Dim sFind As String
Dim wd As New Word.Application
Dim doc As Word.Document

On Error GoTo Errorhandler

'Open the word document
Set doc = wd.Documents.Open("H:\TestDoc.doc")

'Macro assumes ActiveCell is at top of column of search values
For i = 1 To ActiveSheet.UsedRange.CurrentRegion.Rows.Count
'Grab the search value
sFind = ActiveCell.Value
'Select the entire Word document
doc.Select
'Perform the find
doc.Application.Selection.Find.ClearFormatting
doc.Application.Selection.Find.Text = sFind
doc.Application.Selection.Find.Execute Forward:=True
'Our search value is now selected, so move one character to put
cursor at end of search value
doc.Application.Selection.MoveRight Unit:=wdCharacter, Count:=1
'Select the next 20 characters
doc.Application.Selection.MoveRight Unit:=wdCharacter, Count:=20,
Extend:=wdExtend
'Copy
doc.Application.Selection.Copy
'Paste in column to the right of search value
ActiveCell.Offset(0, 1).PasteSpecial xlPasteValues
'ActiveCell is now in paste column so adjust and select next search
value
ActiveCell.Offset(1, -1).Select
Next i

FindWordString_Exit:
doc.Close
wd.Quit
Set doc = Nothing
Set wd = Nothing

Exit Sub

Errorhandler:
If Err.Number <> 0 Then
MsgBox "Message: " & Err.Message, vbCritical, "Error"
Resume FindWordString_Exit:
End If

End Sub


--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


Hey,


I'm trying to make a macro that reads a column in Excel.I've only ever
done VBA for excel and this is confusing me. I need the macro to read
the cell in excel and find it in the word document, then copy the 20
charactors to the right of the found numbers and paste them in excel.
And loop this for a complete column


I know thats a little confusing but if you need any clarification or if



you could help please let me know.


Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top