AaronT
06-25-2010, 08:35 PM
Hi All,
I've created a macro to search through a word doc and find any instance of bracketed text ({*}). Now I would like to copy this information to excel much like creating a log. I created a version that builds an array then simply pasts the copied array to excel. This worked well until I had a doc with thousands of these fields (I got an "overflow" error message). I've attached a sample word doc and excel spreadsheet. I was working on the "copy to excel" sub and I thought I would look to the forums for suggestions re: better or more efficient way to get this done. Ideally I would like to test if the workbook is already open and paste from there. Any assistance would be much appreciated. Thanks in advance!
Aaron
attachments:
3948 Excel
3949 Word Doc
My code so far :-):
Sub FindAndCopyFieldsIntoSpreadsheet()
Dim strDefFields, strFoundText As String
Dim objClipboard As DataLabel
Dim intFieldCounter As Integer
intFieldCounter = 0
On Error GoTo Emessage:
'Set cursor on first page
Selection.GoTo What:=wdGoToSection, Which:=wdGoToNext, Name:="1"
'Do until end of document
Do Until ActiveDocument.Bookmarks("\Sel") = ActiveDocument.Bookmarks("\EndOfDoc")
Selection.Find.ClearFormatting
'Text to find
With Selection.Find
'Any text with {} around it. Remember to use \ backslash with special characters like the {}
.Text = "\{*\}"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
'Wildcards is set to true to allow for {*}
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
'Find
Selection.Find.Execute
'If found do this.
If Selection.Find.Found = True Then
' ********************* OLD Version Code. Got overflow for building array. Pasting one-by-one now
'Tell me when the a matching string is found.
'MsgBox "This text found: " & Selection.Text
'Assign to string
'strFoundText = Selection.Text
'strDefFields = strFoundText & vbCrLf & strDefFields
' ********************* OLD Version Code. Got overflow for building array. Pasting one-by-one now
'Counter marks the number of fields and what row to paste the field into
intFieldCounter = intFieldCounter + 1
strDefFields = Selection.Text
'Copy string to clipboard
Text2Clipboard (strDefFields)
'Open DefField_ImportOnly.xls in same folder as this doc and paste your fields
Call OpenDFIO_And_Paste(intFieldCounter)
Else 'No instances then exit
MsgBox intFieldCounter & " fields were found."
intFieldCounter = 0
Exit Do
End If
'Done
Loop
Exit Sub
Emessage:
MsgBox "Error: " & Err.Description
Exit Sub
End Sub
I've created a macro to search through a word doc and find any instance of bracketed text ({*}). Now I would like to copy this information to excel much like creating a log. I created a version that builds an array then simply pasts the copied array to excel. This worked well until I had a doc with thousands of these fields (I got an "overflow" error message). I've attached a sample word doc and excel spreadsheet. I was working on the "copy to excel" sub and I thought I would look to the forums for suggestions re: better or more efficient way to get this done. Ideally I would like to test if the workbook is already open and paste from there. Any assistance would be much appreciated. Thanks in advance!
Aaron
attachments:
3948 Excel
3949 Word Doc
My code so far :-):
Sub FindAndCopyFieldsIntoSpreadsheet()
Dim strDefFields, strFoundText As String
Dim objClipboard As DataLabel
Dim intFieldCounter As Integer
intFieldCounter = 0
On Error GoTo Emessage:
'Set cursor on first page
Selection.GoTo What:=wdGoToSection, Which:=wdGoToNext, Name:="1"
'Do until end of document
Do Until ActiveDocument.Bookmarks("\Sel") = ActiveDocument.Bookmarks("\EndOfDoc")
Selection.Find.ClearFormatting
'Text to find
With Selection.Find
'Any text with {} around it. Remember to use \ backslash with special characters like the {}
.Text = "\{*\}"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
'Wildcards is set to true to allow for {*}
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
'Find
Selection.Find.Execute
'If found do this.
If Selection.Find.Found = True Then
' ********************* OLD Version Code. Got overflow for building array. Pasting one-by-one now
'Tell me when the a matching string is found.
'MsgBox "This text found: " & Selection.Text
'Assign to string
'strFoundText = Selection.Text
'strDefFields = strFoundText & vbCrLf & strDefFields
' ********************* OLD Version Code. Got overflow for building array. Pasting one-by-one now
'Counter marks the number of fields and what row to paste the field into
intFieldCounter = intFieldCounter + 1
strDefFields = Selection.Text
'Copy string to clipboard
Text2Clipboard (strDefFields)
'Open DefField_ImportOnly.xls in same folder as this doc and paste your fields
Call OpenDFIO_And_Paste(intFieldCounter)
Else 'No instances then exit
MsgBox intFieldCounter & " fields were found."
intFieldCounter = 0
Exit Do
End If
'Done
Loop
Exit Sub
Emessage:
MsgBox "Error: " & Err.Description
Exit Sub
End Sub