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 / July 2007

Tip: Looking for answers? Try searching our database.

Interact with multiple Word Objects/Documents VB6/VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jCheah@iso.com - 18 Jul 2007 04:09 GMT
Hi everyone,

FYI,  I am working on a VB6/VBA program that can reformat word
documents like replace certain words with certain characters.  Here's
the little detail about part of my program. When the program read the
selected document, it uses find/replace to replace certain words. In
order to use this, I have to select a portion of the document and then
use the following codes

objWord.Selection.Find

The problem with that is i want to allow the user to work on some
other word documents or outlook emails when my program is running.
When the user selects another document, I get an error, because it is
trying to perform the actions on their selection on new word document
instead of the selection found by my app.

And also i believe the code "objWord.Application.ActiveWindow" is
causing problem too as when the user open another new word document to
work with when the program is running, the new word document will
become the "ActiveWindow".

So is there anyway to let the user to work on some other Word-related
works when my program is running? Because when i run my program, the
process will take about 20 mins to finish, it's illogical to have the
user waited for 20mins without working on other Word-related works. I
want the user to be able to multitasking.

The following is small part of my code

   Dim objWord As New Word.Application

   Dim objDoc As New Word.Document

   Dim formName as String

   'Open the Word Doc based on the file path set on the textbox

   Set objDoc = objWord.Documents.Open(filePath)

   With objDoc.Application


'----------------------------------------------------------------------------------------------------------------

   '*#1 Get the title name from the header of word document


'----------------------------------------------------------------------------------------------------------------

       If .ActiveWindow.View.SplitSpecial <> wdPaneNone Then

           .ActiveWindow.Panes(2).Close

       End If

       If .ActiveWindow.ActivePane.View.Type = wdNormalView
Or .ActiveWindow.ActivePane.View.Type = wdOutlineView Then

           .ActiveWindow.ActivePane.View.Type = wdPrintView

       End If

       .ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageHeader

       .Selection.MoveRight unit:=wdCell

       .Selection.MoveRight unit:=wdCell

       .Selection.MoveRight unit:=wdCell

       formName = .Selection.Text

       .ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument


'----------------------------------------------------------------------------------------------------------------

   '*#2 Find and Replacement Procedure (Mark Heading)


'----------------------------------------------------------------------------------------------------------------

       .Selection.HomeKey unit:=wdStory

      'set the find criteria based on the following font and
paragraph format

       .Selection.Find.ClearFormatting

       With .Selection.Find.Font

           .Size = 10

           .Bold = True

       End With

       With .Selection.Find.ParagraphFormat

           .SpaceBeforeAuto = False

           .SpaceAfterAuto = False

           .Alignment = wdAlignParagraphLeft

       End With

       'set the replacement criteria where all the matches will be
marked wtih Underlines and Strikethrough

       .Selection.Find.Replacement.ClearFormatting

       With .Selection.Find.Replacement.Font

           .Underline = wdUnderlineDouble

           .UnderlineColor = wdColorAutomatic

           .Strikethrough = True

       End With

       With .Selection.Find

           .Text = ""

           .Replacement.Text = ""

           .Forward = True

           .Wrap = wdFindAsk

           .Format = True

       End With

       'execute the find and replacement on all matches

       .Selection.Find.Execute Replace:=wdReplaceAll

  End With

Any help would be deeply appreciated. thanks..
Jonathan West - 18 Jul 2007 13:20 GMT
If you want the user to continue working on other documents, you mist
religiously avoid using any of the following

Selection
ActiveDocument
ActiveWindow

To avoid using the ActiveDocument object, always open a document using
syntax like this

Dim oDoc as Document
Set oDoc = Documents.Add(Filename:="my filename here")

That gives you a handle to the document you want to work on. Once you have
set oDoc, you can use it exactly as you would use ActiveDocument.

To avoid using the Selection object, always define a Range object from
within the specified document object. You can do almost anything with a
Range that you can do with the Selection, and you can have as many of them
as you want (whereas there is only one Selection)

To avoid using ActiveWindow, define a Window object based on oDoc in the
same way.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

> Hi everyone,
>
[quoted text clipped - 137 lines]
>
> Any help would be deeply appreciated. thanks..
jCheah@iso.com - 19 Jul 2007 18:45 GMT
Hi Jonathan,

Can you show me how to define a range object to used as Selection?
FYI, my program is doing find and replace on the document, can i still
use range object?

And also, please kindly explain what did you mean by "To avoid using
ActiveWindow, define a Window object based on oDoc "? Can you show me
example?

Your help will be deep appreciated. Thanks.....
Jonathan West - 20 Jul 2007 10:50 GMT
> Hi Jonathan,
>
> Can you show me how to define a range object to used as Selection?
> FYI, my program is doing find and replace on the document, can i still
> use range object?

Do something like this (where you have defined oDoc as I described in my
previous post)

Dim oRange as Range
Set oRange = oDoc.Range 'Range object now marks the whole of Doc2
With oRange.Find
 'set up your find code here as if you used "With Selection.Find"

End With

Having defined a Range object variable, you can do almost anything with it
that you can do with the Selection. There are a few exceptions, for instance
you can't use a Range to select a column of a table. But there are
workarounds for those cases if and when you come up against them.

> And also, please kindly explain what did you mean by "To avoid using
> ActiveWindow, define a Window object based on oDoc "? Can you show me
> example?

Dim oWindow As Window
Set oWindow = oDoc.Windows(1)

Then you can use oWindow in the same way that you used ActiveWindow. oWindow
will remain the window associated with oDoc even if the user changes which
document is active and therefore changes which window is the ActiveWindow.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

jCheah@iso.com - 20 Jul 2007 20:28 GMT
Hi Jonathan,

Can Range object do some simple tasks like the following? as I couln't
find the MoveRight, MoveLeft, MoveUp or MoveDown properties under
Range Object.

.Selection.MoveRight unit:=wdCell
.Selection.MoveLeft unit:=wdCharacter, Count:=2, Extend:=wdExtend

Please advise.
Russ - 21 Jul 2007 12:16 GMT
.move for range
See VBA help for move

Some things only work with selection, but you can use myRange.Select to make
myRange the selection.

> Hi Jonathan,
>
[quoted text clipped - 7 lines]
>
> Please advise.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

 
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.