Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Wow,
Thanks alot |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Need Excel count of 1 word if found in multi-word cells of column | Excel Worksheet Functions | |||
Excel 7, paste linked to word becomes black when word pdf'd | Excel Discussion (Misc queries) | |||
Copy from Word to Excel, and retain indent, plus word wrap | Excel Discussion (Misc queries) | |||
Print labels by using Excel data in a Word mail into word | Excel Discussion (Misc queries) | |||
Printing Word Document using Excel Programming hangs Word | Excel Programming |