Connecting Tech Pros Worldwide Forums | Help | Site Map

Word Interop: Replacing text with table

=?ISO-8859-1?Q?S=F8ren?=
Guest
 
Posts: n/a
#1: Aug 27 '08
Hi guys

I got the following code:

-------------------------------------------------------

Dim Word As New Microsoft.Office.Interop.Word.Application
Dim Document As Microsoft.Office.Interop.Word.Document
Document = Word.Documents.Open(Filename)


Dim myStoryRange As Microsoft.Office.Interop.Word.Range
For Each myStoryRange In Document.StoryRanges
With myStoryRange.Find
.Text = "<CHANGES>"
.Replacement.Text = "Here is the changes"
.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindCon tinue

..Execute(Replace:=Microsoft.Office.Interop.Word.W dReplace.wdReplaceAll)
End With
Next myStoryRange

-------------------------------------------------------


... which opens a Word file, locates <CHANGESand replaces it with "Here
is the changes". It works just fine, but instead of just replacing with
text, I would like to replace it with a table like this:


-------------------------------------------------------

With Document

Dim tableRange As Object
tableRange = Document.Range()

.Tables.Add(2, 2)
.Tables.Item(1).Range.Font.Size = 10
.Tables.Item(1).Range.Font.Name = "Verdana"
.Tables.Item(1).Range.Font.Bold = False
.Tables.Item(1).Style = "table grid"

.Tables.Item(1).Cell(1, 1).Range.Text = "Hey"
.Tables.Item(1).Cell(1, 1).Column.Width = 100
.Tables.Item(1).Cell(1, 1).Range.Font.Bold = True
.Tables.Item(1).Cell(1, 2).Range.Text = "Ho"
.Tables.Item(1).Cell(1, 2).Column.Width = 100
.Tables.Item(1).Cell(1, 2).Range.Font.Bold = True


End With

-------------------------------------------------------


Could anyone please give me hint on how to use a table as replacement
for the located text?

Regards Søren

=?ISO-8859-1?Q?S=F8ren?=
Guest
 
Posts: n/a
#2: Aug 27 '08

re: Word Interop: Replacing text with table


I figured it out .. and it was so easy:

Dim Range As Microsoft.Office.Interop.Word.Range = Document.Range()
If Range.Find.Execute("<CHANGES>") Then

With Document

.Tables.Add(Range, 2, 2)
.Tables.Item(1).Range.Font.Size = 10
.Tables.Item(1).Range

etc.






Søren skrev:
Quote:
Hi guys
>
I got the following code:
>
-------------------------------------------------------
>
Dim Word As New Microsoft.Office.Interop.Word.Application
Dim Document As Microsoft.Office.Interop.Word.Document
Document = Word.Documents.Open(Filename)
>
>
Dim myStoryRange As Microsoft.Office.Interop.Word.Range
For Each myStoryRange In Document.StoryRanges
With myStoryRange.Find
.Text = "<CHANGES>"
.Replacement.Text = "Here is the changes"
.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindCon tinue
>
.Execute(Replace:=Microsoft.Office.Interop.Word.Wd Replace.wdReplaceAll)
End With
Next myStoryRange
>
-------------------------------------------------------
>
>
.. which opens a Word file, locates <CHANGESand replaces it with "Here
is the changes". It works just fine, but instead of just replacing with
text, I would like to replace it with a table like this:
>
>
-------------------------------------------------------
>
With Document
>
Dim tableRange As Object
tableRange = Document.Range()
>
.Tables.Add(2, 2)
.Tables.Item(1).Range.Font.Size = 10
.Tables.Item(1).Range.Font.Name = "Verdana"
.Tables.Item(1).Range.Font.Bold = False
.Tables.Item(1).Style = "table grid"
>
.Tables.Item(1).Cell(1, 1).Range.Text = "Hey"
.Tables.Item(1).Cell(1, 1).Column.Width = 100
.Tables.Item(1).Cell(1, 1).Range.Font.Bold = True
.Tables.Item(1).Cell(1, 2).Range.Text = "Ho"
.Tables.Item(1).Cell(1, 2).Column.Width = 100
.Tables.Item(1).Cell(1, 2).Range.Font.Bold = True
>
>
End With
>
-------------------------------------------------------
>
>
Could anyone please give me hint on how to use a table as replacement
for the located text?
>
Regards Søren
Closed Thread