By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,781 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,781 IT Pros & Developers. It's quick & easy.

Word Interop: Replacing text with table

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.