1
votes

I am trying to manipulate Word tables from Excel. The general problem I am encountering has already been addressed by Microsoft here:

https://docs.microsoft.com/en-us/office/vba/word/concepts/customizing-word/error-accessing-a-table-row-or-column

tldr: Merged Cells in Word Tables require a work around to select entire rows to delete.

I used and customized the following code snippet:

Sub RemoveTableBorders() 
    ActiveDocument.Tables(1).Cell(1, 1).Select 
    With Selection 
        .SelectRow 
        .Cells.Borders.Enable = False 
    End With 
End Sub

However I get an 438 Error, pointing to the .SelectRow command. VBA tells me that this is not a valid command for an object. It feels like when I use the With Selection command, Excel does not recognize that I am trying to write to word, as .selectrow is a Word command. Therefore I am trying to include another pointer in the selection like this:

Sub RemoveTableBorders() 
    ActiveDocument.Tables(1).Cell(1, 1).Select 
    With ActiveDocument.Selection 
       .SelectRow 
       .Cells.Borders.Enable = False 
    End With 
End Sub

I can not provide reproducible code.

I think the answer to the following question also solves my problem: If I have two open instances of Office programs, how can I tell which selection (Excel or Word) should be manipulated?

1
Selection exists in the Excel object model, and would be fully-qualified as With Excel.Application.Selection, since Excel is your host application: you have to qualify it if you need it to refer to anything in the Word object model. .SelectRow is documented to throw an error if the selection isn't in a table, are you sure the preceding .Select call is putting the selection where you need it to be?Mathieu Guindon
How or where do you open your word instance? Can you provide this code as well? Assign this creation or opening of word to an variable. On this instance variable you should now be able to execute word commands.Bruno Bieri
Yes Mathieu, I have other tables which do not have merged cells, and they work just fine with direct selection (tables(xy).rows(x) ...). With Word, would the correct selection call be With Word.application.selection?IschaIschratioh

1 Answers

0
votes

The Mistake was indeed that VBA did not regonize the Word Selection. With the following adjustment in the With call the code works:

Sub RemoveTableBorders() 
    ActiveDocument.Tables(1).Cell(1, 1).Select 
    With Word.application.selection
        .SelectRow 
        .Cells.Borders.Enable = False 
    End With 
End Sub

Thanks for your help.