Hi all,
I'm trying to store scattered text data from several word docs to database MS Access and into test word document but at the end there is no data in databse or even in test word document. Macro is connecting to database and opening test word document without problems but somewhere else marco brakes down . Thank You very much for any help.
Here is code from macro :
Sub ExtractData()
Dim sDTE
As String Dim sSubject
As String Dim strFileName
As String Dim strPath
As String Dim FileArray()
As String Dim i
As Long Dim vConnection
As New ADODB.Connection
Dim vRecordSet
As New ADODB.Recordset
Dim oDoc
As Word.Document
Dim dataDoc
As Document
Dim fDialog
As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
'Pick the folder with the letters With fDialog
.Title = "Odaberite direktorij s zahtjevima i kliknite na gumb OK"
.AllowMultiSelect =
False .InitialView = msoFileDialogViewList
If .Show <> -1
Then MsgBox "Cancelled By User"
Exit
Sub End If strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\"
Then strPath = strPath + "\"
End With strFileName = Dir$(strPath & "*.doc")
ReDim FileArray(1
To 1000)
'A number larger the expected number of replies Do While strFileName <> ""
i = i + 1
FileArray(i) = strFileName
'Get the next file name strFileName = Dir$
Loop 'Resize and preserve the array ReDim Preserve FileArray(1
To i)
Application.ScreenUpdating =
False 'Provide connection string for data using Jet Provider for Access database vConnection.ConnectionString = "data source=C:\MagRad\Baza.mdb;" & _
"Provider=Microsoft.Jet.OLEDB.4.0;"
vConnection.Open
vRecordSet.Open "MyTable", vConnection, adOpenKeyset, adLockOptimistic
'Retrieve the data vConnection.Execute "DELETE * FROM MyTable"
'Assign the name of the document to take the data Documents.Open ("""C:\MagRad\Rez.doc""")
Set dataDoc = ActiveDocument
'Open the letters in turn While strFileName <> ""
Set oDoc = Documents.Open(strPath & strFileName)
Selection.HomeKey wdStory
'Start from the top of the letter With Selection.Find
'find the first string .ClearFormatting
Do While .Execute(findText:="DTE/*^13", _
MatchWildcards:=
True, _
Wrap:=wdFindStop, Forward:=
True) =
True 'Assign the found text to a variable and chop off 'the last character - 'ΒΆ' sDTE = Left(Selection.Range, Len(Selection.Range) - 1)
Loop End With Selection.HomeKey wdStory
'Start from the top of the letter With Selection.Find
'find the second string .ClearFormatting
Do While .Execute(findText:="Subject :*^13", _
MatchWildcards:=
True, _
Wrap:=wdFindStop, Forward:=
True) =
True 'Assign the second string to a variable and chop off 'the last character and the leading text sSubject = Mid(Selection.Range, 10, Len(Selection.Range) - 10)
Loop End With 'Switch to the data document and add the content of 'the variables to the blank row of the table dataDoc.Activate
dataDoc.Save
If sDTE <> ""
Then _
vRecordSet("DTE") = sDTE
If sSubject <> ""
Then _
vRecordSet("Subject") = sSubject
vRecordSet.Update
With Selection
.EndKey wdStory
.MoveUp Unit:=wdLine, Count:=1
.MoveRight Unit:=wdCell, Count:=2
'Add a new blank row .TypeText Text:=sDTE
.MoveRight Unit:=wdCell
.TypeText Text:=sSubject
End With 'Close the letter without saving oDoc.Close SaveChanges:=wdDoNotSaveChanges
Set oDoc =
Nothing strFileName = Dir$()
Wend vRecordSet.Close
vConnection.Close
Set vRecordSet =
Nothing Set vConnection =
Nothing Application.ScreenUpdating =
True End Sub