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 2005

Tip: Looking for answers? Try searching our database.

active window macros

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
schafer - 11 Jul 2005 06:15 GMT
How to get (recorded) macros to run in active window only? Macros run in a
previously active window, sometimes.
Anne Troy - 11 Jul 2005 06:36 GMT
Where's the cursor when the macro is run? What's the macro?
*******************
~Anne Troy

www.OfficeArticles.com
www.MyExpertsOnline.com

> How to get (recorded) macros to run in active window only? Macros run in a
> previously active window, sometimes.
schafer - 12 Jul 2005 03:46 GMT
> 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
Chuck - 11 Jul 2005 11:04 GMT
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

> How to get (recorded) macros to run in active window only? Macros run in a
> previously active window, sometimes.
schafer - 12 Jul 2005 04:00 GMT
> 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
[quoted text clipped - 6 lines]
> 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
schafer - 12 Jul 2005 05:58 GMT
This did it!  Thanks, 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
[quoted text clipped - 18 lines]
> fixes.  But this one stumped me.
> Lenny
Jezebel - 11 Jul 2005 12:47 GMT
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.

> How to get (recorded) macros to run in active window only? Macros run in a
> previously active window, sometimes.
schafer - 12 Jul 2005 04:02 GMT
> 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
 
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.