Removing content from a Word table cell using Excel VBA

I am currently working on a project and I am looking for some help. To give you guys a mockup of what's going on, I will run the script step by step.

1) I currently have a string array called "AnimalNamesToRemove" (for this example, the array contains the following words) that contains the words that are used as bookmarks in the text document that I am looking to remove from the word table below:

AnimalNamesToRemove

AnimalCat, AnimalDog, AnimalBird

2) In addition to the array, there is a table in a text document that has the name of the animal in the first column, as well as some information about the animal (the only information that matters is the name of the animal):

Word table

enter image description here

3) excel, , , , . , Excel, ( "myRangeRef" ):

enter image description here

4) , , , , , (, "AnimalDog" ) (, "" ), (, "" ) - , ​​, "AnimalNamesToRemove" (, Cat, Dog, Bird), "AnimalsToDelete". , , , "AnimalsToDelete", (, Cat, Dog Bird) ( ), , , (. , )

enter image description here

Dim wdTable As Object
Dim myRangeRef As Range
Dim AnimalNamesToRemove As Variant
Dim AnimalsToDelete As Variant
Dim wdDoc As Object

Set myRangeRef = ThisWorkbook.Sheets("Bookmark References").Range("B1:B6")

Set wdTable = wdDoc.Tables(1)
For i = LBound(AnimalNamesToRemove) To UBound(AnimalNamesToRemove)
    For Each cell In myRangeRef
        If InStr(1, cell.Value, AnimalNamesToRemove(i), vbTextCompare) Then
            aCell = cell.Offset(, -1).Value
            stTemp = stTemp & "," & aCell
        End If
    Next cell
Next i

stTemp = Mid(stTemp, 2)
If Not Len(Trim(stTemp)) = 0 Then
    AnimalsToDelete = Split(stTemp, ",")
    For i = LBound(AnimalsToDelete) To UBound(AnimalsToDelete)
        For j = wdTable.Rows.Count To 2 Step -1
            If wdTable.cell(j, 1).Range.Text = AnimalsToDelete(i) Then wdTable.Rows(j).Delete
        Next j
    Next i
End If

- / , .

.. (.. "set wdTable =" "next i" ), , . ,

+4
1

, , Microsoft Word 16.0 Object Library Excel VBE ( - , ), Word. :

Sub Test()
Dim BookMarksToDelete() As String
Dim ReturnsToDelete() As String
Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim wdTable As Word.Table
Dim myRangeRef As Range
Dim cel As Range
Dim aCell As String

Set wApp = New Word.Application
Set wDoc = wApp.Documents.Open("C:\Temp\Col1.docx")
Set wdTable = wDoc.Tables(1)

ReDim BookMarksToDelete(0 To 1)
    BookMarksToDelete(0) = "BlahOne"
    BookMarksToDelete(1) = "BlahThree"

Set myRangeRef = Worksheets("Sheet1").Range("B1:B5")

For i = LBound(BookMarksToDelete) To UBound(BookMarksToDelete)
    For Each cel In myRangeRef
        If InStr(1, cel.Value, BookMarksToDelete(i), vbTextCompare) Then
            aCell = cel.Offset(0, -1).Value
            stTemp = stTemp & "," & aCell
        End If
    Next cel
Next i

stTemp = Mid(stTemp, 2)
If Not Len(Trim(stTemp)) = 0 Then
    ReturnsToDelete = Split(stTemp, ",")
    For i = LBound(ReturnsToDelete) To UBound(ReturnsToDelete)
        For j = wdTable.Rows.Count To 2 Step -1
             If Left(wdTable.cell(j, 1).Range.Text, Len(wdTable.cell(j, 1).Range.Text) - 2) = ReturnsToDelete(i) Then
                wdTable.Rows(j).Delete
            End If
        Next j
    Next i
End If

wDoc.Save
wDoc.Close
wApp.Quit

Set wdTable = Nothing
Set wDoc = Nothing
Set wApp = Nothing
Set myRangeRef = Nothing
End Sub

, .

( doc, ) , 2 . - " ", , GUI - " ".

.

EDIT "BlahOne" "NameOne", , , ...

+2

Source: https://habr.com/ru/post/1680114/


All Articles