active window macros

S

schafer

How to get (recorded) macros to run in active window only? Macros run in a
previously active window, sometimes.
 
C

Chuck

You might want to look up the "Activewindow" object in VBE help.
Alternatively create an object that points to the document you want to work
on, rather than the "Activedocument" for instance,

Dim objDoc as document

Set objDoc = Documents.Add 'additional parameters if necessary

With objDoc
'work magic here
end With
 
J

Jezebel

The better approach is not to use the windows at all. It's much simpler if
your macros work directly with the relevant storyrange objects.
 
S

schafer

Anne Troy said:
Where's the cursor when the macro is run? What's the macro?
*******************

The cursor starts out in the active window Document 2. As the macro
progresses, it jumps to window Document 1. However Document 2 remains the
active window.

The macro does some simple plain text reformatting. It is created by the
record function. This strange behavior is not limited only to this macro,
however. Is there a way to assign the macro to the active window logical
address?

Here is the macro:

Sub aP()
'
' aDot.P Macro
' Macro recorded 8/9/2002 by Lenny Schafer
'
Selection.WholeStory
Selection.Range.Style = ActiveDocument.Styles(wdStyleNormal)
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = "^l"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " {1,}^l"
.Replacement.Text = "^l"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^l"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.WholeStory
Selection.Range.Style = ActiveDocument.Styles(wdStyleNormal)
Selection.Font.Name = "Courier New"
Selection.Font.Size = 9
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ".^p"
.Replacement.Text = ".^l "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = """^p"
.Replacement.Text = """^l "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
S

schafer

Chuck said:
You might want to look up the "Activewindow" object in VBE help.
Alternatively create an object that points to the document you want to work
on, rather than the "Activedocument" for instance,

Dim objDoc as document

Set objDoc = Documents.Add 'additional parameters if necessary

With objDoc
'work magic here
-snip-

Thanks Chuck,
I'll give it a try. My document is actually a scratch buffer -- the macro
formatted text gets picked up and pasted into another document manually. I
assume that 'work magic here is where I nestle my macro. I have some
limited experience and skills with programming, and I would like to keep it
that way. I can tweak a simple line or two, but to learn VBE proper is too
great a learning curve for my simple needs. I love the macro record function,
needless to say. Usually, with a little reverse engineering, I can make minor
fixes. But this one stumped me.
Lenny
 
S

schafer

Jezebel said:
The better approach is not to use the windows at all. It's much simpler if
your macros work directly with the relevant storyrange objects.

Is it possible to do this with the record macro function? My programming
skills do not extend much far beyond that.
Lenny
 
S

schafer

This did it! Thanks, Chuck.

schafer said:
-snip-

Thanks Chuck,
I'll give it a try. My document is actually a scratch buffer -- the macro
formatted text gets picked up and pasted into another document manually. I
assume that 'work magic here is where I nestle my macro. I have some
limited experience and skills with programming, and I would like to keep it
that way. I can tweak a simple line or two, but to learn VBE proper is too
great a learning curve for my simple needs. I love the macro record function,
needless to say. Usually, with a little reverse engineering, I can make minor
fixes. But this one stumped me.
Lenny
 

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