Solved: How to write VBA code to copy data from Word to Excel?
Hi All,
Here where I work, we have a group of colleagues who by default like to work in MS Word. On the other hand, our systems frequently want to read MS Excel documents to import data. So, when I saw rhuman's query I realized that it is a task I want to master, as well.
How do I build one VBA program that opens both a MS Word Doc and an MS Excel xls file, with code to search the Word doc for a 'key word' (or sequence of characters), then, grab a block of text (such as a 9-digit code following the key word), copy it to the clipboard, toggle to excel, find the next row in the worksheet, paste the value, and repeat the process as needed until no more instances of the key word are found.
Here is the code I captured with Word's macro recorder; unfortunately, it did not record the window change to Excel, selection of the cell, the paste or the window change back to Word.
[vba]Sub Macro2()
'
' Macro2 Macro
' Macro recorded 7/29/08 by Ron McKenzie
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "ID:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EscapeKey
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=9, _
Extend:=wdExtend
Selection.Copy
'// At this point, I changed windows and pasted the ID$# (those
' 9 characters) in to the next empty row in an Excel worksheet.
'// How do I tell VBA to tell Excel to find that next row and paste
' the value?
'// Is it better to code this in Excel VBA (instead of Word VBA
' driving Excel VBA) ?
'// Further, it seems to me rather than repeating code, that I want
' to loop back to the
' top and repeat the process and let the error on selection.find.execute
' failing to find an instance of 'ID:' signal the end of processing.
Selection.Find.ClearFormatting
With Selection.Find
.Text = "ID:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=9, Extend:=wdExtend
Selection.Copy
'// I had 4 ID:s in my word file so this block was repeated two more times;
' omitted to save space
End Sub
[/vba]
All advice, guidance, point outs of resources (PDF docs, KB article here, stuff on other websites) and other helps will be gratefully received.
Many Thanks!
Word to Excel, a KB article by Lucas
Malcolm (and Lucas, if you're lurking), et al,
Well, I found a very helpful article in the KB Extract sentences containing a specific word to excel file. After I get my cut on this working, I'll modify it so Word builds an array and Excel transfers the array into a worksheet.
The presenting problem is that VBE tells me there is an END WITH that has no WITH. But I do have the pair. Is the challenge that Lucas's code uses a DO - LOOP that has no WHILE on either end and this is confusing the compiler?
Another question, does this code automatically EXIT from the DO LOOP when SELECTION.FIND.EXECUTE fails to find any more instances of .TEXT?
Here's my modification of Lucas's code:[vba] Sub FindKeywordCopyAcctNum()
Dim appExcel As Object
Dim objSheet As Object
Dim aRange As Range
Dim intRowCount As Integer
Dim FilePath As String
Dim Keyword As String
Dim IsWindowsOS As String
intRowCount = 1
Set aRange = ActiveDocument.Range
Keyword = InputBox("Enter Keyword, Phrase or String to search for", "Search String", "ID:")
With Selection.Find
Do
.Text = Keyword 'was "ID:" ' the word I am looking for
.Execute
If .Found Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=9, Extend:=wdExtend
Selection.Copy
If objSheet Is Nothing Then
Set appExcel = CreateObject("Excel.Application") 'Change the file path to match the location of your test.xls
If IsWindowsOS Then
FilePath = "C:\temp\test.xls" ' Windows OS
Else
FilePath = "MacintoshHD:Users:ronald:Destop:test-blah.xls" 'Mac OS
End If
Set objSheet = appExcel.workbooks.Open(FilePath).Sheets("Sheet1")
intRowCount = 1
End If
objSheet.Cells(intRowCount, 1).Select
objSheet.Paste
intRowCount = intRowCount + 1
End If
Loop While .Found ' Yikes, my bad (Thanks, xld)
End With
If Not objSheet Is Nothing Then
appExcel.workbooks(1).Close True
appExcel.Quit
Set objSheet = Nothing
Set appExcel = Nothing
End If
Set aRange = Nothing
End Sub
Public Function IsWindowsOS() As Boolean
If Application.System.OperatingSystem Like "*Win*" Then 'Word vs. Excel difference - RM
IsWindowsOS = True
Else
IsWindowsOS = False
End If
End Function
[/vba]
Thanks in advance for your help.