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 / November 2005

Tip: Looking for answers? Try searching our database.

Print a Section

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
achidsey - 17 Nov 2005 23:40 GMT
Word Experts,

I have written code in Excel VBA and now need to do a few tasks in Word VBA.
I could use some help figuring out a few basics.

Each day I have to print a section of a report that falls in the middle of
the report.

A simplified version of the report is provided below.  I have to print from
the line that begins with "ACCOUNT: X435" through the line "PREM POT    
9,051,419".

Using the macro recorder, I've tried to create code but I seem to have a few
things wrong.

<<Begin Report>>

THE FOREGOING INFORMATION IS
/PAGE    
ACCOUNT: X435
                                                                           
                                         
NET CAPITAL
ACCT       EQUITY    
---- ------------  ---
4AM0      -38,789    
4K3E     -463,046    

PREMIUM POTENTIAL  
PREM POT     9,051,419                                  
                                           
(1) DIVIDENDS PAYABLE OR
(2) IF POSITIONS ARE IN MORE

<<End Report>>

I've tried to write the code by:
1) searching for the first key word which marks the beginning of the section
I want to print,
2) assigning a variable to the result of the first search,
3) searching for the second key word,
4) assigning a variable to the result of the second search
5) selecting the text starting with the first variable and ending with the
second variable
6) printing the selection

It runs through the code until it gets to the "Application.PrintOut" line at
the bottom, although I'm don't think I was successful at assigning any of the
variables.

Your help would be appreciated.  Thanks, Alan

Sub PrintSection()

   Selection.Find.ClearFormatting
   With Selection.Find
       .Text = "ACCOUNT: X435"
       .Forward = True
   End With
   Selection.Find.Execute
   
   Set StartPrint = Selection
   
   Selection.HomeKey Unit:=wdLine

   Selection.Find.ClearFormatting
   With Selection.Find
       .Text = "prem pot"
       .Forward = True
   End With
   Selection.Find.Execute
   
   Set EndPrint = Selection
   
     
   Set rngPrint = ActiveDocument.Range(StartPrint.Words(1).Start,
EndPrint.Words(2).End)
   
   Application.PrintOut FileName:="", Range:=rngPrint

End Sub

Signature

achidsey

Doug Robbins - Word MVP - 18 Nov 2005 05:28 GMT
Dim prange As Range
Set prange = ActiveDocument.Range
prange.Start = prange.Start + InStr(prange, "ACCOUNT") - 1
prange.End = prange.Start + InStr(prange, "PREM POT") + 21
prange.Select
ActiveDocument.PrintOut Range:=wdPrintSelection

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Word Experts,
>
[quoted text clipped - 81 lines]
>
> End Sub
achidsey - 18 Nov 2005 21:31 GMT
Doug,

Thanks for your help.

Alan

Signature

achidsey

> Dim prange As Range
> Set prange = ActiveDocument.Range
[quoted text clipped - 88 lines]
> >
> > End Sub
 
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.