Automation: Help! Access to Word Table

wlgzhang

Registered User.
Local time
Today, 05:31
Joined
Aug 16, 2004
Messages
19
Hi All,
I am developing a BDMS by using Access, have been asked to publish data to a word table, I inserted some bookmarks into this table. I try to pull data form access and insert these data onto mapped bookmarks. When I run the code, I get a runtime error: You entered multiple destinations for a page, line, footnode, endnote or document. I do not have much experience with word, and I may not use right methods to do what I needs. I am stuck here, could anybody help!

the following are some of my code:

Sub ProduceSheetBody(wordApp As Word.Application, DesiredSubject As String, SpecifiedSession As String)
Dim ExamInfoRst As Recordset
Dim ExamInfoStr As String

ExamInfoStr = GetExamMarkingInfo(DesiredSubject, SpecifiedSession)

Set ExamInfoRst = CurrentDb.OpenRecordset(ExamInfoStr)

' MsgBox "Get Exam Marking App Info: " & CStr(ExamInfoRst!FirstName) & " " & CStr(ExamInfoRst!LastName) _
& " " & CStr(ExamInfoRst!ExamCode)

With wordApp.Selection
For Num = 1 To 20
If Not ExamInfoRst.EOF Then
.Goto what:=wdGoToBookmark, Name:="ApplicantName" & Num
.TypeText ExamInfoRst!FirstName & " " & ExamInfoRst!LastName
.Goto what:=wdGoToBookmark, Name:="Code" & Num
.TypeText ExamInfoRst!ExamCode
ExamInfoRst.MoveNext
If Not ExamInfoRst.EOF Then
' MsgBox "Get Exam Marking App Info: " & CStr(ExamInfoRst!FirstName) & " " & CStr(ExamInfoRst!LastName) _
& " " & CStr(ExamInfoRst!ExamCode)
End If
Else
.Goto what:=wdGoToBookmark, Name:="ApplicantName" & Num
.TypeBackspace
.Goto what:=wdGoToBookmark, Name:="Code" & Num
.TypeBackspace
End If
MsgBox "in the loop " & CStr(Num)
Next Num

End With

ExamInfoRst.close
Set ExamInfoRst = Nothing

End Sub

In Loop 2, I got this Error message: You entered multiple destinations for a page, line, footnode, endnote or document.

And tha following is a incomplete out put to the word template (I highlighted the 2 bookMarks )

Examiner: Mullan Subject: Administrative Law

Applicants’ Name Code Pass Fail Mark*

wafik Abadir Adm02
ApplicantNam2 Code2
ApplicantName3 Code3
ApplicantName4 Code4

......

ApplicantName19 Code19
ApplicantName20 Code20

Note: when I paste the table, its borders are disappeared.
Any help is much appreciated.
Wei Zhang
 
Last edited:
Hmmm would be nice, but when I click on your link..the sample code jumps from 1 to 4 to 10...if you've downloaded it, maybe you could share it here. ;)
 
Thanks! Looks like some of the stuff that's posted out here, but with a few additional bells and whistles. I'm actually trying to dump entire recordsets at bookmark points. I can't imagine writing the code to individually populate 27-28 different recordsets (comprised of queries). Granted the queries making the recordsets are smaller then the tables because they are rollups, but that's a lot of typing and prevents me from looping though the different queries easily.
 
Hi Gizmo & Adrianna,

I go to Microsoft website to read the online version of programmer guide. I delete all the book marks in the table, and work with objects of rows Columns cells in the table directly. Now I solve my problem and get the output right the way to the document by change my original code to:

Sub ProduceSheetBody(wordApp As Word.Application, DesiredSubject As String, SpecifiedSession As String)
Dim ExamInfoRst As Recordset
Dim ExamInfoStr As String
Dim myCell As Word.Cell
Dim myRow As Integer
Dim myCol As Integer

ExamInfoStr = GetExamMarkingInfo(DesiredSubject, SpecifiedSession)

Set ExamInfoRst = CurrentDb.OpenRecordset(ExamInfoStr)

' MsgBox "Get Exam Marking App Info: " & CStr(ExamInfoRst!FirstName) & " " & CStr(ExamInfoRst!LastName) _
& " " & CStr(ExamInfoRst!ExamCode)
ExamInfoRst.MoveFirst
With wordApp.ActiveDocument
For myRow = 2 To 23

For myCol = 1 To 2
Set myCell = ActiveDocument.Tables(1).Cell(myRow, myCol)

If Not ExamInfoRst.EOF Then

'myCell.Select
'With Selection.Font
'.Size = 14
'.Bold = True
If myCol = 1 Then
myCell.Range.Text = CStr(ExamInfoRst!FirstName & " " & ExamInfoRst!LastName)

Else
myCell.Range.Text = Right(ExamInfoRst!ExamCode, 2)
ExamInfoRst.MoveNext
End If
'End With
Else

myCell.Range.Text = " "
End If

Next myCol
'MsgBox "in the loop " & CStr(myRow)
Next myRow


End With
If ExamInfoRst.EOF Then
ExamInfoRst.close
Set ExamInfoRst = Nothing

End If
End Sub

However, when i try to control the format of the out put, such assign font size, font weight to the data, I get the runtime error. You can see common out code.

Further, I should make the operation more effective by check if the data record set is EOF then exit for loop immediately.

Anyway, I am happy that I get some progress in my work.

I will do some further study on how to control the document format to get more professional look.



Nice to have your help.

Wei
 

Users who are viewing this thread

Back
Top Bottom