467,168 Members | 986 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,168 developers. It's quick & easy.

Word Interop: Replacing text with table

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
Aug 27 '08 #1
  • viewed: 6948
Share:
1 Reply
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:
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
Aug 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Nitin | last post: by
5 posts views Thread by Webbyz | last post: by
2 posts views Thread by Ola K | last post: by
6 posts views Thread by nagar@community.nospam | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.