Pasting Tables from Excel to Word
Ed,
Im extremely grateful for the trouble you have taken to explain stuff about
word. I would be going through your demonstration code in detail tomorrow.
Please note I have been able to get over the trouble of converting a code in
word -- Selection.TypeText Text:="$" -- to an equivalent code in excel to
control word as -- SDocument.ActiveWindow.Selection.TypeText Text:="$" .
Basically rather than using sdocument.selection I see that using
Sdocument.ActiveWindow.Selection overcomes all the compile errors etc.
Thanks a lot,
Hari
India
"Ed" wrote in message
...
Hari:
One of the difficulties of controlling one program through another
program's
VBA is getting the references set correctly. In an Excel macro which
controls Word, "Range" could refer to either an Excel or a Word range,
depending on how it's declared. "Dim oRange As Range" in an Excel macro
is
going be assumed to be an Excel range. To make it a range in your Word
document, it must be declared as "Dim oRange As Word.Range".
Unfortunately, I am not all that expert, and usually have to stumble
through
my mistakes to catch these things. And occasionally I inflict these
mistakes on others, too! Sorry. Apparently, Selection *almost always*
assumes itself to belong to the parent application - in this case, Excel.
That's not a bad thing, really. Range is much preferred over Selection.
Selection refers to what the insertion point has selected. If a few
letters
or an entire paragraph is selected, then that is "Selection". Any actions
taken on the Selection object will be performed on whatever is selected.
When the Selection is only a single insertion point, some actions are not
available, but that is still Selection.
Range is good because the insertion point doesn't move. It helps speed
things up considerably. And it is much easier to "put a handle on"
through
another program, allowing you to avoid "automatic assumptions" by the
parent
program.
Word has a built-in range called "Content". So if we declare a Word range
and set it to the document's Content range, we can use Range methods and
properties to do what we need. Collapse will take us to either end of the
range, depending on which direction we choose. Then one of the Insert
methods can be used.
Try this:
' Range is specific to the program
Dim oRange as Word.Range
' String is a string is a string
Dim strMsg As String
strMsg = "Hello!"
' "doc" is used to refer to the
' Word document object you are using
Set oRange = doc.Content
' oRange equals the entire scope of the doc
oRange .Collapse wdCollapseEnd
' oRange now equals only the
' insertion point at the end of the doc
oRange .InsertBreak Type:=wdPageBreak
' Since we've increased the content of the doc,
' we need to reset oRange to encompass it all
Set oRange = doc.Content
oRange .Collapse wdCollapseEnd
oRange .InsertAfter strMsg
You should have a new page at the end of "doc" with "Hello!" on it.
Regarding the differing syntaxes of Word, Excel, PPT, and other VBA
codings - yes, they all have different "dialects", as it were. Just as
people from different parts of a country often have different names for
objects and actions, the VBA of the various programs call things
differently.
HTH
Ed
|