Here is my code and what is does:
1) opens up word files
2) copies bookmarked fields
3) opens up access database
4) copies the data to the table
Now, i need some help with the error trapping. When the code runs and finds a word doc that does not contain bookmarks, it gives me a error 5941 saying that the item doesn't exist. As you notice there is another error trapping in the bottom of my code dealing with the database and updating records. Question: what is the best way to deal with both of these error traps?
Code:
Private Sub vawadoc_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Dim appAccess As New Access.Application
Dim objRecordSet As DAO.Recordset
Dim targetgrou As String
Dim appnum As String
Dim orgname As String
Dim protitle As String
Dim goalstate As String
Dim projectobj As String
Dim proact As String
Dim outcomem As String
ChDir "C:\Documents and Settings\ceyhorn\Desktop\VOCA\"
Myfile = Dir("*.doc", vbNormal)
Do While Myfile <> ""
Documents.Open FileName:="C:\Documents and Settings\ceyhorn\Desktop\VOCA\" & Myfile
appnum = Myfile
orgname = ActiveDocument.FormFields("WFDETx29").Result
protitle = ActiveDocument.FormFields("WFDETx30").Result
goalstate = ActiveDocument.FormFields("WFPNTx5").Result
targetgrou = ActiveDocument.FormFields("WFPNTx6").Result _
& " - " & ActiveDocument.FormFields("WFPNTx7").Result _
& " - " & ActiveDocument.FormFields("WFPNTx8").Result _
& " - " & ActiveDocument.FormFields("WFPNTx9").Result _
& " - " & ActiveDocument.FormFields("WFPNTx10").Result
proact = ActiveDocument.FormFields("WFPNTx11").Result
projectobj = ActiveDocument.FormFields("WFPNTx12").Result _
& " - " & ActiveDocument.FormFields("WFPNTx13").Result _
& " - " & ActiveDocument.FormFields("WFPNTx14").Result _
& " / " & ActiveDocument.FormFields("WFPNTx15").Result _
& " - " & ActiveDocument.FormFields("WFPNTx16").Result _
& " - " & ActiveDocument.FormFields("WFPNTx17").Result _
& " / " & ActiveDocument.FormFields("WFPNTx18").Result _
& " - " & ActiveDocument.FormFields("WFPNTx19").Result _
& " - " & ActiveDocument.FormFields("WFPNTx20").Result _
& " / " & ActiveDocument.FormFields("WFPNTx21").Result _
& " - " & ActiveDocument.FormFields("WFPNTx22").Result _
& " - " & ActiveDocument.FormFields("WFPNTx23").Result _
& " / " & ActiveDocument.FormFields("WFPNTx24").Result _
& " - " & ActiveDocument.FormFields("WFPNTx25").Result _
& " - " & ActiveDocument.FormFields("WFPNTx26").Result
outcomem = ActiveDocument.FormFields("WFPNTx27").Result _
& " - " & ActiveDocument.FormFields("WFPNTx28").Result _
& " - " & ActiveDocument.FormFields("WFPNTx29").Result _
& " / " & ActiveDocument.FormFields("WFPNTx30").Result _
& " - " & ActiveDocument.FormFields("WFPNTx31").Result _
& " - " & ActiveDocument.FormFields("WFPNTx32").Result _
& " / " & ActiveDocument.FormFields("WFPNTx33").Result _
& " - " & ActiveDocument.FormFields("WFPNTx34").Result _
& " - " & ActiveDocument.FormFields("WFPNTx35").Result _
& " / " & ActiveDocument.FormFields("WFPNTx36").Result _
& " - " & ActiveDocument.FormFields("WFPNTx37").Result _
& " - " & ActiveDocument.FormFields("WFPNTx38").Result _
& " / " & ActiveDocument.FormFields("WFPNTx39").Result _
& " - " & ActiveDocument.FormFields("WFPNTx40").Result _
& " - " & ActiveDocument.FormFields("WFPNTx41").Result
ActiveDocument.Close
appAccess.OpenCurrentDatabase ("C:\Documents and Settings\ceyhorn\Desktop\gtsinfo.mdb")
Set objRecordSet = appAccess.DBEngine(0)(0).OpenRecordset("Table1")
With objRecordSet
.AddNew
!ApplicationNumber = appnum ' or variable name
!OrganizationName = orgname ' or variable name
!projecttitle = protitle
!ProjectSummary = goalstate
!TargetPopulation = targetgrou
!HowProgramWorks = proact
!EvaluationMeasure = projectobj
!EvaluationMeasureO = outcomem
On Error Resume Next
.Update
If Err.Number = 3022 Then
Else
End If
.Close
End With
Set objRecordSet = Nothing
appAccess.Quit
Set appAccess = Nothing
Myfile = Dir
Loop
wrdApp.Visible = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Thanks in advance for any suggestions,
Chris
1) opens up word files
2) copies bookmarked fields
3) opens up access database
4) copies the data to the table
Now, i need some help with the error trapping. When the code runs and finds a word doc that does not contain bookmarks, it gives me a error 5941 saying that the item doesn't exist. As you notice there is another error trapping in the bottom of my code dealing with the database and updating records. Question: what is the best way to deal with both of these error traps?
Code:
Private Sub vawadoc_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Dim appAccess As New Access.Application
Dim objRecordSet As DAO.Recordset
Dim targetgrou As String
Dim appnum As String
Dim orgname As String
Dim protitle As String
Dim goalstate As String
Dim projectobj As String
Dim proact As String
Dim outcomem As String
ChDir "C:\Documents and Settings\ceyhorn\Desktop\VOCA\"
Myfile = Dir("*.doc", vbNormal)
Do While Myfile <> ""
Documents.Open FileName:="C:\Documents and Settings\ceyhorn\Desktop\VOCA\" & Myfile
appnum = Myfile
orgname = ActiveDocument.FormFields("WFDETx29").Result
protitle = ActiveDocument.FormFields("WFDETx30").Result
goalstate = ActiveDocument.FormFields("WFPNTx5").Result
targetgrou = ActiveDocument.FormFields("WFPNTx6").Result _
& " - " & ActiveDocument.FormFields("WFPNTx7").Result _
& " - " & ActiveDocument.FormFields("WFPNTx8").Result _
& " - " & ActiveDocument.FormFields("WFPNTx9").Result _
& " - " & ActiveDocument.FormFields("WFPNTx10").Result
proact = ActiveDocument.FormFields("WFPNTx11").Result
projectobj = ActiveDocument.FormFields("WFPNTx12").Result _
& " - " & ActiveDocument.FormFields("WFPNTx13").Result _
& " - " & ActiveDocument.FormFields("WFPNTx14").Result _
& " / " & ActiveDocument.FormFields("WFPNTx15").Result _
& " - " & ActiveDocument.FormFields("WFPNTx16").Result _
& " - " & ActiveDocument.FormFields("WFPNTx17").Result _
& " / " & ActiveDocument.FormFields("WFPNTx18").Result _
& " - " & ActiveDocument.FormFields("WFPNTx19").Result _
& " - " & ActiveDocument.FormFields("WFPNTx20").Result _
& " / " & ActiveDocument.FormFields("WFPNTx21").Result _
& " - " & ActiveDocument.FormFields("WFPNTx22").Result _
& " - " & ActiveDocument.FormFields("WFPNTx23").Result _
& " / " & ActiveDocument.FormFields("WFPNTx24").Result _
& " - " & ActiveDocument.FormFields("WFPNTx25").Result _
& " - " & ActiveDocument.FormFields("WFPNTx26").Result
outcomem = ActiveDocument.FormFields("WFPNTx27").Result _
& " - " & ActiveDocument.FormFields("WFPNTx28").Result _
& " - " & ActiveDocument.FormFields("WFPNTx29").Result _
& " / " & ActiveDocument.FormFields("WFPNTx30").Result _
& " - " & ActiveDocument.FormFields("WFPNTx31").Result _
& " - " & ActiveDocument.FormFields("WFPNTx32").Result _
& " / " & ActiveDocument.FormFields("WFPNTx33").Result _
& " - " & ActiveDocument.FormFields("WFPNTx34").Result _
& " - " & ActiveDocument.FormFields("WFPNTx35").Result _
& " / " & ActiveDocument.FormFields("WFPNTx36").Result _
& " - " & ActiveDocument.FormFields("WFPNTx37").Result _
& " - " & ActiveDocument.FormFields("WFPNTx38").Result _
& " / " & ActiveDocument.FormFields("WFPNTx39").Result _
& " - " & ActiveDocument.FormFields("WFPNTx40").Result _
& " - " & ActiveDocument.FormFields("WFPNTx41").Result
ActiveDocument.Close
appAccess.OpenCurrentDatabase ("C:\Documents and Settings\ceyhorn\Desktop\gtsinfo.mdb")
Set objRecordSet = appAccess.DBEngine(0)(0).OpenRecordset("Table1")
With objRecordSet
.AddNew
!ApplicationNumber = appnum ' or variable name
!OrganizationName = orgname ' or variable name
!projecttitle = protitle
!ProjectSummary = goalstate
!TargetPopulation = targetgrou
!HowProgramWorks = proact
!EvaluationMeasure = projectobj
!EvaluationMeasureO = outcomem
On Error Resume Next
.Update
If Err.Number = 3022 Then
Else
End If
.Close
End With
Set objRecordSet = Nothing
appAccess.Quit
Set appAccess = Nothing
Myfile = Dir
Loop
wrdApp.Visible = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Thanks in advance for any suggestions,
Chris