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

Re: Insert {XE "test"}{TC "Test" \l 1} by Jay

Jay
Thu Feb 21 10:02:54 PST 2008

Grenier wrote:
> 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

Dim Tbl As Table
Dim Rg As Word.Range
Do until rst.eof
Set Tbl = ActiveDocument.Tables.Add(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
Set Rg = .Cell(1, 2).Range
Rg.MoveEnd wdCharacter, -1 ' omit cell marker from range
Rg.Collapse wdCollapseEnd
With ActiveDocument.Fields
'insert {XE "test"}{TC "Test" \l 1} after the company name...
.Add Rg, wdFieldIndexEntry, rst!CompName
Rg.Collapse wdCollapseEnd
.Add Rg, wdFieldTOCEntry, rst!CompName & " \l 1"
End With
End With

Tbl.Select
With Selection
.Move wdCharacter, 1 ' get past table marker
.InsertBreak Type:=wdSectionBreakNextPage
End With

rst.movenext
Loop

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.



RE: Insert {XE "test"}{TC "Test" \l 1} by JeanGuyMarcil

JeanGuyMarcil
Thu Feb 21 10:31:02 PST 2008

"Grenier" wrote:

> 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...

RE: Insert {XE "test"}{TC "Test" \l 1} by JeanGuyMarcil

JeanGuyMarcil
Thu Feb 21 11:39:00 PST 2008

"Grenier" wrote:

> 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...
;-)

RE: Insert {XE "test"}{TC "Test" \l 1} by Grenier

Grenier
Thu Feb 21 10:55:02 PST 2008

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" wrote:

> "Grenier" wrote:
>
> > 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...

RE: Insert {XE "test"}{TC "Test" \l 1} by Grenier

Grenier
Thu Feb 21 17:42:00 PST 2008

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 !



"Jean-Guy Marcil" wrote:

> "Grenier" wrote:
>
> > 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...
> ;-)