Interact with multiple Word Objects/Documents VB6/VBA

J

jCheah

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..
 
J

Jonathan West

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.
 
J

jCheah

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.....
 
J

Jonathan West

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.
 
J

jCheah

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.
 
R

Russ

..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.
 

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