Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2006

Tip: Looking for answers? Try searching our database.

Inserting text into a specific document?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vincent Verheul - 28 Apr 2006 13:49 GMT
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
Jonathan West - 28 Apr 2006 14:37 GMT
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

Signature

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

> Hello,
>
[quoted text clipped - 27 lines]
> Thanks in advance!
> Vincent
Vincent Verheul - 28 Apr 2006 16:25 GMT
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

> Hi Vincent
>
[quoted text clipped - 48 lines]
>> Thanks in advance!
>> Vincent
Jonathan West - 28 Apr 2006 17:33 GMT
> 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!

Signature

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 


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.