Insert {XE "test"}{TC "Test" \l 1}

G

Grenier

Automation a word report from Access. At run time, I want to be able to
insert 2 field type to a company name. One type for a TOC and a second for an
index. I'am using a table for an uniform layout. How can I simply insert
those field right after the company name in the cell ? Maybe I started all
wrong ?

Do until rst.eof
Set Tbl = Wrd.ActiveDocument.Tables.Add(Wrd.Selection.Range, 8, 2)
with Tbl
.Cell(1, 1).Range.Text = "Company name :"
.Cell(1, 1).Range.Style = wdStyleStrong

.Cell(1, 2).Range.Text = rst!CompName
'insert {XE "test"}{TC "Test" \l 1} after the company name...
end with

wrd.selection.InsertBreak Type:=wdSectionBreakNextPage
rst.movenext
loop
 
G

Grenier

Jean-Guy and Jay , you guys were right on.
Thank's a million !
Always amaze by this community support
I'll try to "pay it foward"



Jean-Guy Marcil said:
Grenier said:
Automation a word report from Access. At run time, I want to be able to
insert 2 field type to a company name. One type for a TOC and a second for an
index. I'am using a table for an uniform layout. How can I simply insert
those field right after the company name in the cell ? Maybe I started all
wrong ?

Do until rst.eof
Set Tbl = Wrd.ActiveDocument.Tables.Add(Wrd.Selection.Range, 8, 2)
with Tbl
.Cell(1, 1).Range.Text = "Company name :"
.Cell(1, 1).Range.Style = wdStyleStrong

.Cell(1, 2).Range.Text = rst!CompName
'insert {XE "test"}{TC "Test" \l 1} after the company name...
end with

wrd.selection.InsertBreak Type:=wdSectionBreakNextPage
rst.movenext
loop

As I always say, do not use the Selection object, especially when automating
a document [Especially "heinous" is your usage of a Word Application obect
with the "ActiveDocument" object... I think people were stoned for less than
that in ancient Rome! ;-) ].
Here is a code sample that does what you want, you just need to adapt it to
your situation:

'Always use objects, it makes for sturdier code that is easy to read and easy
'to debug, not that I ever need to debug my code...(He says
"wishful-thinking-ly")
Dim objWord As Word.Application
Dim docNew As Word.Document
Dim tblText As Word.Table
Dim rgeCell As Word.Range

Set objWord = New Word.Application
With objWord
.Visible = True
Set docNew = .Documents.Add
With docNew
Set tblText = .Range.Tables.Add(.Range, 4, 3)
With tblText
Set rgeCell = .Cell(1, 1).Range
With rgeCell
.Collapse wdCollapseStart
.Text = "Company name :"
.Style = wdStyleStrong
End With
Set rgeCell = .Cell(1, 2).Range
With rgeCell
.Collapse wdCollapseStart
.Text = "IBM"
.Collapse wdCollapseEnd
.Fields.Add rgeCell, wdFieldTOCEntry, """TOC Test"" \l 1",
False
.Fields.Add rgeCell, wdFieldIndexEntry, "Index Test", False
End With
Set rgeCell = .Range
With rgeCell
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
End With
End With
End With
.Activate
End With


I hope this is what you were after...
 
J

Jean-Guy Marcil

Grenier said:
Jean-Guy and Jay , you guys were right on.
Thank's a million !
Always amaze by this community support
I'll try to "pay it foward"

I can send you my PayPal account number...
;-)
 
G

Grenier

whish I was Bill or Warren ...

Just a small ajustment...
Added chr(34) cause long company name like 'General Electrique Canada'
output just 'General' in TOC and Index

..Fields.Add rg, wdFieldTOCEntry, Chr(34) & rst!NomEntrep & Chr(34) & " \l
1", False
..Fields.Add rg, wdFieldIndexEntry, Chr(34) & rst!NomEntrep & Chr(34), False

Merci encore !
 

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