Automate Word from Access

P

Pat Hartman

I am attempting to create some complex engineering
documents. Included is a sample of one small part of the
document. The code creates a table at a bookmark. I
would then like to select the first cell of the table and
set the option to repeat the heading row should the table
have a page break. The code actually succeeds in setting
the property. However, the header does not repeat on the
second page. When I set the property manually it works.
I created a macro to do the job and in the recording mode,
the property is properly set but when I reset the property
and run the macro, the property gets set but the header
does not repeat. The 2 lines of code marked with *** are
the ones in question.
'MAX NORMAL CONDITIONS
Set rs = Application.CurrentProject.Connection.Execute
("SELECT * FROM MaxNormalConditions WHERE ModelNumber = '"
& strModel & "'")
'Loop through Items & fill table with detail
With rs
strTable = "Location" & vbTab & "Max Normal
Pressure (psig)" & vbTab & "Temp (F)" & vbTab & "Altitude
(ft)"
strTable = strTable & vbTab & "CaseID" & vbTab &
vbTab & "Max Normal Temp(F)" & vbTab & "Pressure (psig)" &
vbTab & "Altitude (ft)" & vbTab & "Case ID" & vbCr
Do Until .EOF
strTable = strTable & rs!Location & vbTab
strTable = strTable & rs!MaximumPressure &
vbTab
strTable = strTable & rs!Temp & vbTab
strTable = strTable & rs!Altitude & vbTab
strTable = strTable & rs!CaseID & vbTab & vbTab

strTable = strTable & rs!MaximumTemp & vbTab
strTable = strTable & rs!Pressure & vbTab
strTable = strTable & rs!Altitude1 & vbTab
strTable = strTable & rs!CaseID1 & vbCr

.MoveNext
Loop
InsertTextAtBookMark "MaxNormalConditions",
strTable
Set objTable = WordApp.Selection.ConvertToTable
(Separator:=vbTab)
objTable.AutoFormat
Format:=wdTableFormatProfessional ', applyshading:=True,
applyHeadingrows:=True, AutoFit:=True
*** WordApp.Selection.MoveRight Unit:=wdCell
*** WordApp.Selection.Tables(1).Rows.HeadingFormat
= True
Set objTable = Nothing
End With

I have another question and that is can I reference a
table by name rather than by index number. Each table is
inserted at a bookmark.
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Pat,

Replace the code that you have identified with the *** by

objTable.Rows(1).HeadingFormat = True

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

JGM

Hi Pat,

Pat Hartman said:
I am attempting to create some complex engineering
documents. Included is a sample of one small part of the
document. The code creates a table at a bookmark. I
would then like to select the first cell of the table and
set the option to repeat the heading row should the table
have a page break. The code actually succeeds in setting
the property. However, the header does not repeat on the
second page. When I set the property manually it works.
I created a macro to do the job and in the recording mode,
the property is properly set but when I reset the property
and run the macro, the property gets set but the header
does not repeat. The 2 lines of code marked with *** are
the ones in question.
'MAX NORMAL CONDITIONS
Set rs = Application.CurrentProject.Connection.Execute
("SELECT * FROM MaxNormalConditions WHERE ModelNumber = '"
& strModel & "'")
'Loop through Items & fill table with detail
With rs
strTable = "Location" & vbTab & "Max Normal
Pressure (psig)" & vbTab & "Temp (F)" & vbTab & "Altitude
(ft)"
strTable = strTable & vbTab & "CaseID" & vbTab &
vbTab & "Max Normal Temp(F)" & vbTab & "Pressure (psig)" &
vbTab & "Altitude (ft)" & vbTab & "Case ID" & vbCr
Do Until .EOF
strTable = strTable & rs!Location & vbTab
strTable = strTable & rs!MaximumPressure &
vbTab
strTable = strTable & rs!Temp & vbTab
strTable = strTable & rs!Altitude & vbTab
strTable = strTable & rs!CaseID & vbTab & vbTab

strTable = strTable & rs!MaximumTemp & vbTab
strTable = strTable & rs!Pressure & vbTab
strTable = strTable & rs!Altitude1 & vbTab
strTable = strTable & rs!CaseID1 & vbCr

.MoveNext
Loop
InsertTextAtBookMark "MaxNormalConditions",
strTable
Set objTable = WordApp.Selection.ConvertToTable
(Separator:=vbTab)
objTable.AutoFormat
Format:=wdTableFormatProfessional ', applyshading:=True,
applyHeadingrows:=True, AutoFit:=True
*** WordApp.Selection.MoveRight Unit:=wdCell
*** WordApp.Selection.Tables(1).Rows.HeadingFormat
= True
Set objTable = Nothing
End With

I have another question and that is can I reference a
table by name rather than by index number. Each table is
inserted at a bookmark.

"Name" is not a "Table" property, but you may want to investigate the "ID"
property... It was designed for interacting with HTML, but it may suit your
purpose here...

Cheers!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top