Inserting text into a specific document?

V

Vincent Verheul

Hello,

I have a question about insering text: I want to create a new Word document
using VBA and insert text into that document uring VBA. Since the text is
quite large, it may take some time. During that time, users may want to read
their email (using Word) or edit text in another Word document.

Currently that is not possible with my code: the text inserted by VBA is
always inserted at the current active cursor: resulting in the text being
inserted into the wrong document!

Below an example of the code. I am using the Documents's ActiveWindow
Selection (cursor):

Set WordApp = GetObject(, "Word.Application") ' Assumes Word is running
Set Doc = WordApp.Documents.Add(Template:=Template, NewTemplate:=False,
_
DocumentType:=wdNewBlankDocument)
With Doc.ActiveWindow.Selection
.TypeText "This text line."
.TypeParagraph
End With
Doc.close

What can I do to make sure that text is inserted only into the specific
document that I create through VBA?

Thanks in advance!
Vincent
 
J

Jonathan West

Hi Vincent

This is the bit you need to change

With Doc.ActiveWindow.Selection
.TypeText "This text line."
.TypeParagraph
End With

Do this instead

Dim oRange as Object
Set oRange = Doc.Range
With oRange
.InsertAfter "This text line."
.InsertParagraphAfter
End With

If you are using early binding, then you could Dim oRange as Word.Range
instead, which might speed things up a bit

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
V

Vincent Verheul

Hi Jonathan,

Thanks for your quick response! Yes I do use early binding. I have to look
into this, quite some restructuring to be done... It looks like I have to
pass the oRange variable along to all subs and functions that I use to make
sure they all access the very same object. So far I have not mastered this
restructuring (of the VBA in my MsAccess project), but I suppose it will
take some focused effort to get it done...

Thanks again,
Vincent
 
J

Jonathan West

Vincent Verheul said:
Hi Jonathan,

Thanks for your quick response! Yes I do use early binding. I have to look
into this, quite some restructuring to be done... It looks like I have to
pass the oRange variable along to all subs and functions that I use to
make sure they all access the very same object.

Alternatively make it a Public or module-level variable
So far I have not mastered this restructuring (of the VBA in my MsAccess
project), but I suppose it will take some focused effort to get it done...

'Twas ever thus!

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

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