Word view does not change when I populate Range object

S

Sukhi

Hello to good people out there!!
I am using range object and book marks to populate various parts of large
document---

Set rBMRange = .Bookmarks("SomeValue").Range
rBMRange.Text = strPara & vbCrLf
this works fine. but how bring parts of document being populated into view,
so user can see it happening.

When I use selection object ---
objWord.Selection.TypeText straPara
Then I can see word moving pages where its being populating.

Could you also please explain, whats best to use, selection or range, what
are the pros and cons.
Document I am working on is quite complicated using table and so on. When
using selection, I can record a macro and paste into my vb project, and code
is written for me, can I do the similar thing for range object.

Thanks
Sukhi
 
D

Doug Robbins - Word MVP

If you really want to do it (I wouldn't), use

With ActiveDocument.Bookmarks("SomeValue").Range
.Select
.Text = strPara & vbCrLf
End With
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
H

Helmut Weber

Hi Sukhi,

the behaviour you decribe is part of the very nature of ranges.
Showing on the user interface what's going on needs time.
Ranges don't do that and are therefore much faster.
Which isn't the whole truth, by far.

To list up all the differences between range and selection
seems asking for too much, IMHO.

There are a few things that work with ranges,
but not with the selection and vice versa.

I'd google for "selection" and "range",
where both words appear in the same posting.

Maybe you've noticed the short thread
"Loop to insert rows in a word table".

Try both approaches with large tables
and have a look at what's going on.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
S

Sukhi

Thanks Helmut
I am beginning to enjoy working with Ranges.
Can you insert table using range, format it, populate it, and build it as I
go along.

Thats my next task

Sukhi
 
H

Helmut Weber

Hi Sukhi,

yes.

Record inserting a table
and try to replace "selection" by "range"
like this:

Sub Macro14()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
rDcm.Start = rDcm.Paragraphs(2).Range.Start
rDcm.End = rDcm.Start

ActiveDocument.Tables.Add Range:=rDcm, _
NumRows:=4, NumColumns:= 4, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:= wdAutoFitFixed
With rDcm.Tables(1)
.Columns.PreferredWidth = InchesToPoints(0.3)
End With
End Sub

Beware of linesbreaks by the newsreader.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg Maxey

For viewing what happened, you might consider scrollintoview:

Sub Test()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
oRng.Collapse wdCollapseEnd
ActiveDocument.Range(0, 0).Select
oRng.InsertAfter "Hello Sukhi"
ActiveWindow.ScrollIntoView oRng, True
End Sub
 
S

Sukhi

Thanks Helmut

I do the following

.Tables.Add rBMRange, 3, 4
rBMRange.Tables(1).Rows(1).Cells(1).Range.Text = "Value1"
rBMRange.Tables(1).Rows(1).Cells(2).Range.Text = "Value2"
rBMRange.Tables(1).Rows(1).Cells(3).Range.Text = "Value3"
rBMRange.Tables(1).Rows(1).Cells(4).Range.Text = "Value14
rBMRange.Tables(1).Rows(1).Select

first, could I have populated entrie row in one go with an arrwy? like
rBMRange.Tables(1).Rows(1).range.text =split("Value1,Value2,..",",")

second, I want to select a row
rBMRange.Tables(1).Rows(1).Select
then format it, shade it, change font and so on
How do i do that

Sukhi
 
H

Helmut Weber

Hi Sukhi,

like this and in many other ways,
it's just an example, nothing perfect:

Sub Macro15()
Dim t As Table
Dim aStr(1 To 4) As String
Dim lCnt As Long
Set t = ActiveDocument.Tables(1)
For lCnt = 1 To 4
aStr(lCnt) = Format(lCnt, "0000")
Next
For lCnt = 1 To 4
t.Cell(1, lCnt).Range.Text = aStr(lCnt)
Next
End Sub

and

Sub Macro14()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
rDcm.Tables(1).Rows(1).Shading.BackgroundPatternColorIndex = wdYellow
End Sub

Again, just the principle.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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