Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

vba - Delete shaded rows before save?

I'm quite new to VBA so forgive if I don't explain this well but I've got a Word 2013 document with commands to shade specified rows based on dropdown selections within the document.

Currently, I have it set to delete all shaded rows from the document before printing but I want to have an additional sub to delete all shaded rows before using the "Save As" function as well - I just can't figure out where my command is going wrong.

Here's the print command that's working:

Public Sub myApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)

    Dim d1 As Document
    Set d1 = ActiveDocument
    
    Dim tbl As Table, r As Row, c As Cell, rng As Range, t As Integer
    Dim i As Integer, n As Integer
    
    'table counter
    t = 1
    
    'cycle through each table
    For Each tbl In d1.Tables
        'row counter
        n = tbl.Rows.Count
        'cycle through the rows in the selected table
        For i = 1 To n Step 1
            'compare pattern fill, select those that are shaded and delete
            If d1.Tables(t).Rows(i).Shading.Texture = wdTextureDarkDiagonalDown Then
                d1.Tables(t).Rows(i).Delete
            End If
        Next
        t = t + 1
    Next

Here's the Save As command that is not working:

Public Sub myApp_DocumentBeforeSaveAs(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
    
    Dim d1 As Document
    Set d1 = ActiveDocument
    
    Dim tbl As Table, r As Row, c As Cell, rng As Range, t As Integer
    Dim i As Integer, n As Integer
    
    'table counter
    t = 1
    
    'cycle through each table
    For Each tbl In d1.Tables
        'row counter
        n = tbl.Rows.Count
        'cycle through the rows in the selected table
        For i = 1 To n Step 1
            'compare pattern fill, select those that are shaded and delete
            If d1.Tables(t).Rows(i).Shading.Texture = wdTextureDarkDiagonalDown Then
                d1.Tables(t).Rows(i).Delete
            End If
        Next
        t = t + 1
    Next

I don't get any error codes, it just doesn't do anything to the shaded text. I appreciate anyone that can point me in the right direction!

question from:https://stackoverflow.com/questions/65904432/delete-shaded-rows-before-save

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code works kind of, but it does generate an error. The issue is on the

For i = 1 To n Step 1

line of code. To delete rows in a table you need to Step backwards, otherwise when you delete a row, the remaining row count doesn’t equal the number of rows in the table. Your command should be:

For i = n to 1 Step -1 

There is also an unnecessary line of code

t = t + 1

It’s not creating a problem but it’s unnecessary, at least based on what you have shown us.

Again your code works as long as you correct that Step 1 problem and you know for certain that the row shading is as you have specified being Textured Dark Diagonal Down.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...