Results 1 to 10 of 10
  1. #1
    New Lounger
    Join Date
    Feb 2015
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Red face Change first row´s fill of each table, if the first row is numbered

    Hy!First of all, congratulations for your website. I´m working on heavy word documents that i exported from another software. The exported files come with a lot of tables and I already did some macros to format them.Now i´m a stucked, cause i need to fill the first row of each table with grey everytime that first row is numered ("#*-#*-#*":#*)I have small part of the document in attachments so you can understand it better.Is there a chance to do this by macro?Best regards
    Attached Files Attached Files

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    The pattern you suggested won't match any of the tables in your sample so I changed it to something that does match some of the tables you provided.
    Code:
    Sub temp()
      Dim aTbl As Table
      Dim str As String
      For Each aTbl In ActiveDocument.Tables
        str = aTbl.Cell(1, 1).Range.Text
        If str Like "*-#*-#*:*" Then
          'aTbl.ApplyStyleHeadingRows = True  'this would work if your chosen table style had a first row shading
          aTbl.Rows(1).Shading.ForegroundPatternColor = RGB(118, 146, 60)
        End If
      Next aTbl
    End Sub
    I provided an alternative line of code that would work if your tables all used a table style that had a first row shading setting. This would be a 'better' way of doing the task.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    JMMCR (2015-02-11)

  4. #3
    New Lounger
    Join Date
    Feb 2015
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks so much Andrew!

    Its perfect!

    I see you are really good with this, maybe you can tell me if its possible to have a macro to delete all the text between the tables except:

    "Descrição das funções:" and "Descrição dos elementos:" and delete the 1 paragraph after each table?

    Thanks so much!

  5. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Looking at the content of your sample document, it appears that the Headings are to be retained and the 'Normal' paragraphs outside of tables need to be deleted.

    It is certainly possible to do what you ask but we need to be careful to retain at least one paragraph between the tables to avoid them merging together into one table.

    The macro might work along the following lines ...
    Code:
    Sub RemoveUnwantedParagraphs()
      Dim iPar As Integer
      Dim oPar As Paragraph
      For iPar = ActiveDocument.Paragraphs.Count To 2 Step -1
        Set oPar = ActiveDocument.Paragraphs(iPar)
        If Not oPar.Range.Information(wdWithInTable) And _
              oPar.OutlineLevel = wdOutlineLevelBodyText And _
              oPar.Range.InlineShapes.Count = 0 And _
              Not oPar.Previous.Range.Information(wdWithInTable) Then
          oPar.Range.Delete
        End If
      Next iPar
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    JMMCR (2015-02-12)

  7. #5
    New Lounger
    Join Date
    Feb 2015
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    You were right, that´s exactly what i needed. Thanks so much!

    I got an error at the end, after running sucessfully the macro at all the document: Run-time error `5904´: Cannot edit the range.

    You know why?

  8. #6
    New Lounger
    Join Date
    Feb 2015
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I have one limitation on this macro. I need to keep the paragraph after every picture, is it possible to have a "rule" that give another style (not "normal") to the first paragraph after every picture?

    Thanks

  9. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Try the following
    Code:
    Sub RemoveUnwantedParagraphs()
      Dim iPar As Integer
      Dim oPar As Paragraph
      Dim aShp As InlineShape
      
      ActiveDocument.Styles("Caption").ParagraphFormat.OutlineLevel = wdOutlineLevel9
      For Each aShp In ActiveDocument.InlineShapes
        aShp.Range.Paragraphs(1).Next.Style = "Caption"
      Next aShp
      
      For iPar = ActiveDocument.Paragraphs.Count To 2 Step -1
        Set oPar = ActiveDocument.Paragraphs(iPar)
        If Not oPar.Range.Information(wdWithInTable) And _
              oPar.OutlineLevel = wdOutlineLevelBodyText And _
              oPar.Range.InlineShapes.Count = 0 And _
              Not oPar.Previous.Range.Information(wdWithInTable) Then
          oPar.Range.Delete
        End If
      Next iPar
      
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  10. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    JMMCR (2015-02-13)

  11. #8
    New Lounger
    Join Date
    Feb 2015
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi Andrew,

    This works really fine, thank you so much. I still got an error at the end, after running sucessfully the macro at all the document: Run-time error `5904´: Cannot edit the range. The macro does everything i need but at the end, freeze for 2 minutes and then give that error. I wish i could help, but its too much for my macro knowledge. Once again, thank you very much. Best Regards

  12. #9
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    I don't get an error on the document you posted. Are you testing the macro on a different document?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  13. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    JMMCR (2015-02-23)

  14. #10
    New Lounger
    Join Date
    Feb 2015
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Good afternoon Andrew,

    I apologize for the delay but I was on vacation this week without internet access.

    Regarding the document, its an adaptation of the original (is confidential can not publish here).

    You can tell me your email?

    Best regards,

    José Rocha

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
  •