ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Excel to Word (https://www.excelbanter.com/excel-programming/341556-excel-word.html)

[email protected]

Excel to Word
 
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


David Lloyd[_3_]

Excel to Word
 
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.


wrote in message
oups.com...
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



[email protected]

Excel to Word
 
Wow,

Thanks alot



All times are GMT +1. The time now is 11:47 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com