Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Word
application.errors
conversions
docmanagement
drawing.graphics
formatting.longdocs
international
internet.assistant
mail
mailmerge.fields
menustoolbars
newusers
numbering
oleinterop
pagelayout
printingfonts
setup.networking
spelling.grammar
tables
vba.addins
vba.beginners
vba.customization
vba.general
vba.userforms
web.authoring
word6-7macros
word97vba
  
 
date: Mon, 18 Aug 2008 16:09:01 -0700,    group: microsoft.public.word.vba.beginners        back       


How to delete a portion of doc defined by heading levels?   
Hello,

I've been struggling with how to delete a portion of a document by the 
heading levels. I can return a collection of all the instances of a 
particular style:

  ActiveDocument.Styles("Int Heading 1")

But what I need to do is delete everything from the first instance "Int 
Heading 1", up to, but not including, the second instance of "Int Heading 1". 

Is there a way to do this? Is it possible to index into a styles collection 
object and set a range (or some other way)?

Much appreciated,
SW
date: Mon, 18 Aug 2008 16:09:01 -0700   author:   sdjwalls

Re: How to delete a portion of doc defined by heading levels?   
Sub ScratchMacro()
Dim oRng As Range
Dim oRng2 As Range
Set oRng = ActiveDocument.Range
With oRng.Find
  'Find the first instance of the heading style
  .Style = "Heading 1"
  If .Execute Then
    'Wrap the text in a range object
    Set oRng2 = oRng.Duplicate
  End If
  'Fine the second instance of the heading style
  If .Execute Then
    'Extend the range object to include the text in between the two found 
headings.
    oRng2.End = oRng.Start
    oRng2.Delete
  End If
End With
End Sub


sdjwalls wrote:
> Hello,
>
> I've been struggling with how to delete a portion of a document by the
> heading levels. I can return a collection of all the instances of a
> particular style:
>
>  ActiveDocument.Styles("Int Heading 1")
>
> But what I need to do is delete everything from the first instance
> "Int Heading 1", up to, but not including, the second instance of
> "Int Heading 1".
>
> Is there a way to do this? Is it possible to index into a styles
> collection object and set a range (or some other way)?
>
> Much appreciated,
> SW

-- 
Greg Maxey -  Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org
date: Mon, 18 Aug 2008 19:36:42 -0400   author:   Greg Maxey RrOMEOgOLF

Re: How to delete a portion of doc defined by heading levels?   
Hi SW,

See also: http://www.vbaexpress.com/forum/showthread.php?t=21576

-- 
Cheers
macropod
[MVP - Microsoft Word]


"sdjwalls"  wrote in message news:80809C48-A93B-48E6-B911-1A89B887E8C6@microsoft.com...
> Hello,
> 
> I've been struggling with how to delete a portion of a document by the 
> heading levels. I can return a collection of all the instances of a 
> particular style:
> 
>  ActiveDocument.Styles("Int Heading 1")
> 
> But what I need to do is delete everything from the first instance "Int 
> Heading 1", up to, but not including, the second instance of "Int Heading 1". 
> 
> Is there a way to do this? Is it possible to index into a styles collection 
> object and set a range (or some other way)?
> 
> Much appreciated,
> SW
date: Tue, 19 Aug 2008 19:28:02 +1000   author:   macropod lid

Re: How to delete a portion of doc defined by heading levels?   
Hi Greg,

What happens if there's text with a different Style at the start or end of the document?

At http://www.vbaexpress.com/forum/showthread.php?t=21576, the OP wanted to delete all text except for that in the specified Style.

-- 
Cheers
macropod
[MVP - Microsoft Word]


"Greg Maxey" <gmaxey@mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote in message news:OhGu9sYAJHA.5792@TK2MSFTNGP04.phx.gbl...
> Sub ScratchMacro()
> Dim oRng As Range
> Dim oRng2 As Range
> Set oRng = ActiveDocument.Range
> With oRng.Find
>  'Find the first instance of the heading style
>  .Style = "Heading 1"
>  If .Execute Then
>    'Wrap the text in a range object
>    Set oRng2 = oRng.Duplicate
>  End If
>  'Fine the second instance of the heading style
>  If .Execute Then
>    'Extend the range object to include the text in between the two found 
> headings.
>    oRng2.End = oRng.Start
>    oRng2.Delete
>  End If
> End With
> End Sub
> 
> 
> sdjwalls wrote:
>> Hello,
>>
>> I've been struggling with how to delete a portion of a document by the
>> heading levels. I can return a collection of all the instances of a
>> particular style:
>>
>>  ActiveDocument.Styles("Int Heading 1")
>>
>> But what I need to do is delete everything from the first instance
>> "Int Heading 1", up to, but not including, the second instance of
>> "Int Heading 1".
>>
>> Is there a way to do this? Is it possible to index into a styles
>> collection object and set a range (or some other way)?
>>
>> Much appreciated,
>> SW
> 
> -- 
> Greg Maxey -  Word MVP
> 
> My web site http://gregmaxey.mvps.org
> Word MVP web site http://word.mvps.org 
> 
>
date: Tue, 19 Aug 2008 19:28:06 +1000   author:   macropod lid

Re: How to delete a portion of doc defined by heading levels?   
Macropod,

In my test that text isn't affected. I created a new blank document, typed 
in some normal style text, typed in Heading 1 text, more normal text, more 
Heading 1 text, more normal text and ran the code.  After running the code 
the only the text between  the first two intances of Heading 1 and the first 
Heading 1 text is deleted.  The leading and trailing normal text is not 
affected.

Are you seeing differenct results?



-- 
Greg Maxey -  Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org
"macropod" <macropod@invalid.invalid> wrote in message 
news:O2XJ84dAJHA.3756@TK2MSFTNGP02.phx.gbl...
> Hi Greg,
>
> What happens if there's text with a different Style at the start or end of 
> the document?
>
> At http://www.vbaexpress.com/forum/showthread.php?t=21576, the OP wanted 
> to delete all text except for that in the specified Style.
>
> -- 
> Cheers
> macropod
> [MVP - Microsoft Word]
>
>
> "Greg Maxey" <gmaxey@mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote in message 
> news:OhGu9sYAJHA.5792@TK2MSFTNGP04.phx.gbl...
>> Sub ScratchMacro()
>> Dim oRng As Range
>> Dim oRng2 As Range
>> Set oRng = ActiveDocument.Range
>> With oRng.Find
>>  'Find the first instance of the heading style
>>  .Style = "Heading 1"
>>  If .Execute Then
>>    'Wrap the text in a range object
>>    Set oRng2 = oRng.Duplicate
>>  End If
>>  'Fine the second instance of the heading style
>>  If .Execute Then
>>    'Extend the range object to include the text in between the two found 
>> headings.
>>    oRng2.End = oRng.Start
>>    oRng2.Delete
>>  End If
>> End With
>> End Sub
>>
>>
>> sdjwalls wrote:
>>> Hello,
>>>
>>> I've been struggling with how to delete a portion of a document by the
>>> heading levels. I can return a collection of all the instances of a
>>> particular style:
>>>
>>>  ActiveDocument.Styles("Int Heading 1")
>>>
>>> But what I need to do is delete everything from the first instance
>>> "Int Heading 1", up to, but not including, the second instance of
>>> "Int Heading 1".
>>>
>>> Is there a way to do this? Is it possible to index into a styles
>>> collection object and set a range (or some other way)?
>>>
>>> Much appreciated,
>>> SW
>>
>> -- 
>> Greg Maxey -  Word MVP
>>
>> My web site http://gregmaxey.mvps.org
>> Word MVP web site http://word.mvps.org
date: Tue, 19 Aug 2008 06:56:11 -0400   author:   Greg Maxey RrOMEOgOLF

Re: How to delete a portion of doc defined by heading levels?   
Hi Greg,

That's as I expected.

I think there's an issue with how the OP is specifying the requirements. At VBA express they were specified somewhat differently.

-- 
Cheers
macropod
[MVP - Microsoft Word]


"Greg Maxey" <gmaxey@mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote in message news:eJshyoeAJHA.1748@TK2MSFTNGP02.phx.gbl...
> Macropod,
> 
> In my test that text isn't affected. I created a new blank document, typed 
> in some normal style text, typed in Heading 1 text, more normal text, more 
> Heading 1 text, more normal text and ran the code.  After running the code 
> the only the text between  the first two intances of Heading 1 and the first 
> Heading 1 text is deleted.  The leading and trailing normal text is not 
> affected.
> 
> Are you seeing differenct results?
> 
> 
> 
> -- 
> Greg Maxey -  Word MVP
> 
> My web site http://gregmaxey.mvps.org
> Word MVP web site http://word.mvps.org
> "macropod" <macropod@invalid.invalid> wrote in message 
> news:O2XJ84dAJHA.3756@TK2MSFTNGP02.phx.gbl...
>> Hi Greg,
>>
>> What happens if there's text with a different Style at the start or end of 
>> the document?
>>
>> At http://www.vbaexpress.com/forum/showthread.php?t=21576, the OP wanted 
>> to delete all text except for that in the specified Style.
>>
>> -- 
>> Cheers
>> macropod
>> [MVP - Microsoft Word]
>>
>>
>> "Greg Maxey" <gmaxey@mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote in message 
>> news:OhGu9sYAJHA.5792@TK2MSFTNGP04.phx.gbl...
>>> Sub ScratchMacro()
>>> Dim oRng As Range
>>> Dim oRng2 As Range
>>> Set oRng = ActiveDocument.Range
>>> With oRng.Find
>>>  'Find the first instance of the heading style
>>>  .Style = "Heading 1"
>>>  If .Execute Then
>>>    'Wrap the text in a range object
>>>    Set oRng2 = oRng.Duplicate
>>>  End If
>>>  'Fine the second instance of the heading style
>>>  If .Execute Then
>>>    'Extend the range object to include the text in between the two found 
>>> headings.
>>>    oRng2.End = oRng.Start
>>>    oRng2.Delete
>>>  End If
>>> End With
>>> End Sub
>>>
>>>
>>> sdjwalls wrote:
>>>> Hello,
>>>>
>>>> I've been struggling with how to delete a portion of a document by the
>>>> heading levels. I can return a collection of all the instances of a
>>>> particular style:
>>>>
>>>>  ActiveDocument.Styles("Int Heading 1")
>>>>
>>>> But what I need to do is delete everything from the first instance
>>>> "Int Heading 1", up to, but not including, the second instance of
>>>> "Int Heading 1".
>>>>
>>>> Is there a way to do this? Is it possible to index into a styles
>>>> collection object and set a range (or some other way)?
>>>>
>>>> Much appreciated,
>>>> SW
>>>
>>> -- 
>>> Greg Maxey -  Word MVP
>>>
>>> My web site http://gregmaxey.mvps.org
>>> Word MVP web site http://word.mvps.org 
> 
>
date: Tue, 19 Aug 2008 21:03:16 +1000   author:   macropod lid

Re: How to delete a portion of doc defined by heading levels?   
Hi Greg,

Sorry for the late reply...I didn't think my post actually had made it in 
for some reason.

Anyway, many thanks...your example worked brilliantly. I hadn't realized you 
can (sort of) index into a collection of styles (it wasn't clear from the 
online help).

To tweak it a bit (and I should've been clear on this in the beginning), how 
could I set oRng2.start to be at a different style? I should've originally 
stated:

> > But what I need to do is delete everything from the first instance
> > "Int Heading 1", up to, but not including, the second instance of
> > "Heading 1" (or some other different type of heading).

I think my problem lies somewhere in not fully understanding how the Execute 
works in the With/End With.

Thanks again,
SW

"Greg Maxey" wrote:

> Sub ScratchMacro()
> Dim oRng As Range
> Dim oRng2 As Range
> Set oRng = ActiveDocument.Range
> With oRng.Find
>   'Find the first instance of the heading style
>   .Style = "Heading 1"
>   If .Execute Then
>     'Wrap the text in a range object
>     Set oRng2 = oRng.Duplicate
>   End If
>   'Fine the second instance of the heading style
>   If .Execute Then
>     'Extend the range object to include the text in between the two found 
> headings.
>     oRng2.End = oRng.Start
>     oRng2.Delete
>   End If
> End With
> End Sub
> 
> 
> sdjwalls wrote:
> > Hello,
> >
> > I've been struggling with how to delete a portion of a document by the
> > heading levels. I can return a collection of all the instances of a
> > particular style:
> >
> >  ActiveDocument.Styles("Int Heading 1")
> >
> > But what I need to do is delete everything from the first instance
> > "Int Heading 1", up to, but not including, the second instance of
> > "Int Heading 1".
> >
> > Is there a way to do this? Is it possible to index into a styles
> > collection object and set a range (or some other way)?
> >
> > Much appreciated,
> > SW
> 
> -- 
> Greg Maxey -  Word MVP
> 
> My web site http://gregmaxey.mvps.org
> Word MVP web site http://word.mvps.org 
> 
> 
>
date: Wed, 20 Aug 2008 17:10:06 -0700   author:   sdjwalls

Re: How to delete a portion of doc defined by heading levels?   
Hmm.  This is a little trickier as you want to find any "Heading" style 
following the first instance of Heading 1.

AFAIK you can't use .Find to look for "some" variety of a Heading style, so 
we will have to evaluate each paragraph following the initial Heading 1 
style.  Something like this:

 Sub ScratchMacroII()
 Dim oRng As Range
 Dim oRng2 As Range
 Dim bBingo As Boolean
 Set oRng = ActiveDocument.Range
 bBingo = False
 With oRng.Find
   'Find the first instance of the heading style
   .Style = "Heading 1"
   If .Execute Then
     'Wrap the text in a range object
     Set oRng2 = oRng.Duplicate
   End If
   'Fine the second instance of a the heading style
   Set oRng = oRng2.Duplicate
   Do
    oRng.Move wdParagraph, 1
    If InStr(oRng.Style, "Heading") > 0 Then
      bBingo = True
    End If
    Loop Until bBingo
    'Extend the range object to include the text in between the two found 
headings.
     oRng2.End = oRng.Start
     oRng2.Delete
  End With
 End Sub


-- 
Greg Maxey -  Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org
"sdjwalls"  wrote in message 
news:675CFAA7-F34F-4B3F-ADE9-6FAB7CC9E92C@microsoft.com...
> Hi Greg,
>
> Sorry for the late reply...I didn't think my post actually had made it in
> for some reason.
>
> Anyway, many thanks...your example worked brilliantly. I hadn't realized 
> you
> can (sort of) index into a collection of styles (it wasn't clear from the
> online help).
>
> To tweak it a bit (and I should've been clear on this in the beginning), 
> how
> could I set oRng2.start to be at a different style? I should've originally
> stated:
>
>> > But what I need to do is delete everything from the first instance
>> > "Int Heading 1", up to, but not including, the second instance of
>> > "Heading 1" (or some other different type of heading).
>
> I think my problem lies somewhere in not fully understanding how the 
> Execute
> works in the With/End With.
>
> Thanks again,
> SW
>
> "Greg Maxey" wrote:
>
>> Sub ScratchMacro()
>> Dim oRng As Range
>> Dim oRng2 As Range
>> Set oRng = ActiveDocument.Range
>> With oRng.Find
>>   'Find the first instance of the heading style
>>   .Style = "Heading 1"
>>   If .Execute Then
>>     'Wrap the text in a range object
>>     Set oRng2 = oRng.Duplicate
>>   End If
>>   'Fine the second instance of the heading style
>>   If .Execute Then
>>     'Extend the range object to include the text in between the two found
>> headings.
>>     oRng2.End = oRng.Start
>>     oRng2.Delete
>>   End If
>> End With
>> End Sub
>>
>>
>> sdjwalls wrote:
>> > Hello,
>> >
>> > I've been struggling with how to delete a portion of a document by the
>> > heading levels. I can return a collection of all the instances of a
>> > particular style:
>> >
>> >  ActiveDocument.Styles("Int Heading 1")
>> >
>> > But what I need to do is delete everything from the first instance
>> > "Int Heading 1", up to, but not including, the second instance of
>> > "Int Heading 1".
>> >
>> > Is there a way to do this? Is it possible to index into a styles
>> > collection object and set a range (or some other way)?
>> >
>> > Much appreciated,
>> > SW
>>
>> -- 
>> Greg Maxey -  Word MVP
>>
>> My web site http://gregmaxey.mvps.org
>> Word MVP web site http://word.mvps.org
>>
>>
>>
date: Thu, 21 Aug 2008 03:35:20 -0400   author:   Greg Maxey RrOMEOgOLF

Re: How to delete a portion of doc defined by heading levels?   
Hi Greg,

Many thanks. With a few tweaks to what you have below, I've got my code to 
do exactly what it needs to do.

The tweaks I added were to solve the problems of having it remove every 
instance of Int Heading #, and also to remove it if was the last chapter 
before the appendix (or could've done end-of-document, but my attempts didn't 
work...anyway this works).

What I ended up with (a portion of it):

'
    ' Int Heading 1 instances
    '
    Set oRng = ActiveDocument.Range
    Do
    
        bBingo = False
        bNotFound = True
    
        ' Eliminate "Int Heading 1" instances
        With oRng.Find
        
            'Find the first instance of the heading style
            .Style = "Int Heading 1"
            
            If .Execute Then
                'Wrap the text in a range object
                Set oRng2 = oRng.Duplicate
                bNotFound = False
            End If
            
            If bNotFound = False Then
                'Find the second instance of a heading style
                Set oRng = oRng2.Duplicate
                Do
                    oRng.Move wdParagraph, 1
                    If (StrComp(oRng.Style, "Heading 1", 1) = 0) Or _
                       (StrComp(oRng.Style, "Appendix 1", 1) = 0) Then
                        bBingo = True
                    End If
                Loop Until bBingo
                
                'Extend the range object to include the text in between the 
two found headings.
                oRng2.End = oRng.Start
                oRng2.Delete
            End If
            
        End With
        
    Loop Until bNotFound

Many thanks!!
SW

"Greg Maxey" wrote:

> Hmm.  This is a little trickier as you want to find any "Heading" style 
> following the first instance of Heading 1.
> 
> AFAIK you can't use .Find to look for "some" variety of a Heading style, so 
> we will have to evaluate each paragraph following the initial Heading 1 
> style.  Something like this:
> 
>  Sub ScratchMacroII()
>  Dim oRng As Range
>  Dim oRng2 As Range
>  Dim bBingo As Boolean
>  Set oRng = ActiveDocument.Range
>  bBingo = False
>  With oRng.Find
>    'Find the first instance of the heading style
>    .Style = "Heading 1"
>    If .Execute Then
>      'Wrap the text in a range object
>      Set oRng2 = oRng.Duplicate
>    End If
>    'Fine the second instance of a the heading style
>    Set oRng = oRng2.Duplicate
>    Do
>     oRng.Move wdParagraph, 1
>     If InStr(oRng.Style, "Heading") > 0 Then
>       bBingo = True
>     End If
>     Loop Until bBingo
>     'Extend the range object to include the text in between the two found 
> headings.
>      oRng2.End = oRng.Start
>      oRng2.Delete
>   End With
>  End Sub
> 
> 
> -- 
> Greg Maxey -  Word MVP
> 
> My web site http://gregmaxey.mvps.org
> Word MVP web site http://word.mvps.org
> "sdjwalls"  wrote in message 
> news:675CFAA7-F34F-4B3F-ADE9-6FAB7CC9E92C@microsoft.com...
> > Hi Greg,
> >
> > Sorry for the late reply...I didn't think my post actually had made it in
> > for some reason.
> >
> > Anyway, many thanks...your example worked brilliantly. I hadn't realized 
> > you
> > can (sort of) index into a collection of styles (it wasn't clear from the
> > online help).
> >
> > To tweak it a bit (and I should've been clear on this in the beginning), 
> > how
> > could I set oRng2.start to be at a different style? I should've originally
> > stated:
> >
> >> > But what I need to do is delete everything from the first instance
> >> > "Int Heading 1", up to, but not including, the second instance of
> >> > "Heading 1" (or some other different type of heading).
> >
> > I think my problem lies somewhere in not fully understanding how the 
> > Execute
> > works in the With/End With.
> >
> > Thanks again,
> > SW
> >
> > "Greg Maxey" wrote:
> >
> >> Sub ScratchMacro()
> >> Dim oRng As Range
> >> Dim oRng2 As Range
> >> Set oRng = ActiveDocument.Range
> >> With oRng.Find
> >>   'Find the first instance of the heading style
> >>   .Style = "Heading 1"
> >>   If .Execute Then
> >>     'Wrap the text in a range object
> >>     Set oRng2 = oRng.Duplicate
> >>   End If
> >>   'Fine the second instance of the heading style
> >>   If .Execute Then
> >>     'Extend the range object to include the text in between the two found
> >> headings.
> >>     oRng2.End = oRng.Start
> >>     oRng2.Delete
> >>   End If
> >> End With
> >> End Sub
> >>
> >>
> >> sdjwalls wrote:
> >> > Hello,
> >> >
> >> > I've been struggling with how to delete a portion of a document by the
> >> > heading levels. I can return a collection of all the instances of a
> >> > particular style:
> >> >
> >> >  ActiveDocument.Styles("Int Heading 1")
> >> >
> >> > But what I need to do is delete everything from the first instance
> >> > "Int Heading 1", up to, but not including, the second instance of
> >> > "Int Heading 1".
> >> >
> >> > Is there a way to do this? Is it possible to index into a styles
> >> > collection object and set a range (or some other way)?
> >> >
> >> > Much appreciated,
> >> > SW
> >>
> >> -- 
> >> Greg Maxey -  Word MVP
> >>
> >> My web site http://gregmaxey.mvps.org
> >> Word MVP web site http://word.mvps.org
> >>
> >>
> >> 
> 
> 
>
date: Thu, 21 Aug 2008 13:37:00 -0700   author:   sdjwalls

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us