Word Interop: Replacing text with table

S

Søren

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

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

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


... which opens a Word file, locates <CHANGES> and 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
 
S

Søren

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:
 

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