Consulting

Results 1 to 3 of 3

Thread: How to skip tables in a macro designed to typemark paragraphs

  1. #1
    VBAX Regular
    Joined
    Sep 2017
    Posts
    11
    Location

    How to skip tables in a macro designed to typemark paragraphs

    I am trying to get a macro to typemark the beginning of each paragraph, but it keeps getting stuck because there are tables, which it wants to typemark but shouldn't. I read this thread: vbaexpress.com/forum/showthread.php?37455-Solved-Check-whether-a-loop-has-encountered-a-table-and-then-skip-it. I wasn't sure how to apply it to mine, so if anyone could help that would be appreciated. I am including my macro below and I have bolded where an error occurs.



    Selection.HomeKey Unit:=wdStory ' Start at the beginning of the word doc
    ' Double space the document
    ActiveDocument.Paragraphs.LineSpacingRule = wdLineSpaceDouble
    ' Change font to 12 pt times new roman
    ActiveDocument.content.Font.Size = 12
    ActiveDocument.content.Font.Name = "Times New Roman"

    Dim curTypemark As String ' Variable for a typemark
    curTypemark = "" ' Initial instantiation is the empty string

    ' This sub's code largely copies that of CheckTypemarks, so more verbose
    ' comments are in that sub
    Dim oPrg As Paragraph ' placeholder for a paragraph

    ' Loop through each paragraph
    For Each oPrg In ActiveDocument.Paragraphs
    Dim paraRng As Range
    Set paraRng = oPrg.Range

    If (oPrg.Style = "tx" Or oPrg.Style = "sb1tx") Then
    paraRng.MoveEnd Unit:=wdCharacter, Count:=-1
    paraRng.InsertAfter (vbCr)
    End If

    If (Not (curTypemark = oPrg.Style)) Then
    curTypemark = oPrg.Style
    paraRng.InsertBefore ("<" + curTypemark + ">")

    If (Not (oPrg.Style = "cn" Or oPrg.Style = "tx" Or oPrg.Style = "ins-art" Or oPrg.Style = "ins-photo" Or oPrg.Style = "a" Or oPrg.Style = "b" Or oPrg.Style = "c" Or oPrg.Style = "d")) Then
    paraRng.MoveEnd Unit:=wdCharacter, Count:=-1
    paraRng.InsertAfter (vbCr)
    End If

    If (oPrg.Style = "a" Or oPrg.Style = "b" Or oPrg.Style = "c" Or oPrg.Style = "d") Then
    paraRng.MoveEnd Unit:=wdCharacter, Count:=-1
    paraRng.InsertBefore (vbCr)
    End If

    End If
    Next

    End Sub

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Try:

    Sub Test()
    Dim curTypemark As String
    Dim oPar As Paragraph ' placeholder for a paragraph
    Dim oRng As Range
      curTypemark = vbNullString
      With ActiveDocument
        .Paragraphs.LineSpacingRule = wdLineSpaceDouble
        .Content.Font.Size = 12
        .Content.Font.Name = "Times New Roman"
        For Each oPar In .Paragraphs
          Set oRng = oPar.Range
          oRng.Select
          If Not oRng.Information(wdWithInTable) Then
            If (oPar.Style = "tx" Or oPar.Style = "sb1tx") Then
              oRng.MoveEnd Unit:=wdCharacter, Count:=-1
              oRng.InsertAfter (vbCr)
            End If
            If (Not (curTypemark = oPar.Style)) Then
              curTypemark = oPar.Style
              oRng.InsertBefore ("<" + curTypemark + ">")
              If (Not (oPar.Style = "cn" Or oPar.Style = "tx" Or oPar.Style = "ins-art" Or oPar.Style = "ins-photo" Or oPar.Style = "a" Or oPar.Style = "b" Or oPar.Style = "c" Or oPar.Style = "d")) Then
                oRng.MoveEnd Unit:=wdCharacter, Count:=-1
                oRng.InsertAfter (vbCr)
              End If
              If (oPar.Style = "a" Or oPar.Style = "b" Or oPar.Style = "c" Or oPar.Style = "d") Then
                oRng.MoveEnd Unit:=wdCharacter, Count:=-1
                oRng.InsertBefore (vbCr)
              End If
            End If
          End If
        Next
      End With
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Sep 2017
    Posts
    11
    Location
    Thank you so much. I think that will work.

    Do I need to insert this string anywhere or it doesn't matter?: Selection.HomeKey Unit:=wdStory ' Start at the beginning of the word doc

Tags for this Thread

Posting Permissions

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