Results 1 to 2 of 2

Thread: Automatic editing

  1. #1
    VBAX Newbie
    Feb 2020

    Automatic editing

    Hi, this mounth I'll Have to edit some text and I Would like not to do this manually so i decided to write a vba code to do the job but i had some trobles.
    What i would like to have is :

    Dim shp As Shape
    For Each shp In ActiveDocument.Shapes
    Next shp

    I'm importing a pdf in word so some figure will be imported as a lot of text boxes. so i would like all the figure and text boxes to be remouved. But the code doesn't do this action always

    Dim numPara As Long
    numPara = ActiveDocument.Paragraphs.Count
    Dim aRange As Range
    For i = numPara To 1 Step -1
    Set aRange = ActiveDocument.Paragraphs(1).Range
    If Len(aRange) <= 1 Then oPara.Delete
    End If

    This code wold have to remouve the paragraph if i have something like

    But i think there is a problem with the use of len applied to a paragraph

    Dim numPara As Integer
    numPara = ActiveDocument.Paragraphs.Count
    For i =numPara to 1 Step -1
    If (ActiveDocument.Paragraphs(i).Format.Style Like "List Paragraph") Then
    End If
    Next i

    I wold like to obtain at the end something like

    1 Heading 1
    1.1 Heading 2
    1.1.1 Heading 3 heading 4 Normal, or body text
    For me it is important that even if i modefy the tytle structure, the number like won't change
    Actually this the only part that function

    Dim numPara As Integer
    Dim final As Integer
    Dim str As String
    Dim cond1 As Boolean
    Dim cond2 As Boolean
    Dim head1 As Style, head2 As Style, head3 As Style, head4 As Style
    numPara = ActiveDocument.Paragraphs.Count
    final = numPara - 500
    For i = numPara To final Step -1
    str = ActiveDocument.Paragraphs(i).Range.ListFormat.ListString
    Set head1 = ActiveDocument.Styles("Heading 1")
    Set head2 = ActiveDocument.Styles("Heading 2")
    Set head3 = ActiveDocument.Styles("Heading 3")
    Set head4 = ActiveDocument.Styles("Heading 4")
    cond1 = ActiveDocument.Paragraphs(i).Format.Style Like "Normal"
    cond2 = ActiveDocument.Paragraphs(i).Format.Style Like "Body Text"
    cond3 = Not cond1
    cond4 = Not cond2
    If cond1 Or cond2 Then
    Debug.Print i
    If str Like "#[.]#" Then ActiveDocument.Paragraphs(i).Style = wdStyleHeading2
    If str Like "#[.]#[.]#" Then ActiveDocument.Paragraphs(i).Style = wdStyleHeading3
    If str Like "#[.]#[.]#[.]#" Then ActiveDocument.Paragraphs(i).Style = wdStyleHeading4
    End If
    Next i

    When the pdf is impoted it may happen that the heading level are matched to the wrong Heading so i want to check the type of index and change the heading.
    Do you have and idea of what to do if i find a document where the title are not even heading ?
    Thanks for your help

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Sep 2005
    Here is a start.

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oPar As Paragraph
    Dim lngIndex As Long
    Dim oILS As InlineShape
    Dim oShp As Shape
      For lngIndex = ActiveDocument.Paragraphs.Count - 1 To 1 Step -1
        Set oPar = ActiveDocument.Paragraphs(lngIndex)
        If Len(oPar.Range.Text) < 2 Then oPar.Range.Delete
      Next lngIndex
      For lngIndex = ActiveDocument.InlineShapes.Count To 1 Step -1
      Next lngIndex
      For lngIndex = ActiveDocument.Range.ShapeRange.Count To 1 Step -1
      Next lngIndex
      Exit Sub
    End Sub
    I have no idea what you are talking about after that point.

    Now, I am going to go here: and warn anyone who might see your cross post not to waste their time trying to answer something already partially answered here.

    Visit my website:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts