Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete cells in word table

Status
Not open for further replies.

Queryman

Programmer
Nov 4, 2002
243
US
I need to use VBA to delete a couple of cells in a word table. The first cell I need to delete is the last cell in the 1st column and the 2nd cell I need to delete is the last populated cell in the table. This is the only table in the document. I found how to delete a couple of other cells that were at the beginning of the table I had by using this command
TableDeleteCells .ShiftCells = 0
I can't figure out how to do it for the last cell in the 1st column and the last populated cell in the table.
Thanks,



Michael

 
Hi,

Here's some code that seems to work...
Code:
Sub cellsdel()
    With ActiveDocument.Tables(1)
        .Cell(.Columns(1).Cells.Count, 1).Delete ShiftCells:=wdDeleteCellsShiftUp
    End With
    For Each c In ActiveDocument.Tables(1).Range.Cells
        If Left(c.Range.Text, Len(c.Range.Text) - 1) >= " " Then
            r1 = c.Range.Rows(1).Index
            c1 = c.Range.Columns(1).Index
        End If
    Next
    With ActiveDocument.Tables(1)
        .Cell(r1, c1).Delete ShiftCells:=wdDeleteCellsShiftUp
    End With
End Sub
Hope this helps :)

Skip,
Skip@TheOfficeExperts.com
 
I ran this & got an error. It's says I can't reference individual columns in this collection because the table have mixed cell widths.
Thanks,



Michael

 
Sorry Skip,
I don't understand VBA and didn't think that was relevant.
Thanks,




Michael

 
Try this...
Code:
Sub cellsdel()
    With ActiveDocument.Tables(1)
        r1 = .Rows.Count
        .Cell(r1, 1).Delete ShiftCells:=wdDeleteCellsShiftUp
    End With
    For Each c In ActiveDocument.Tables(1).Range.Cells
        If Left(c.Range.Text, Len(c.Range.Text) - 1) >= " " Then
            Set rng = c.Range
        End If
    Next
    With ActiveDocument.Tables(1)
        rng.Delete
    End With
End Sub

Skip,
Skip@TheOfficeExperts.com
 
Actually this is better...
Code:
Sub cellsdel()
    With ActiveDocument.Tables(1)
        r1 = .Rows.Count
        .Cell(r1, 1).Delete ShiftCells:=wdDeleteCellsShiftUp
    End With
    For Each c In ActiveDocument.Tables(1).Range.Cells
        With c.Range
            If Left(.Text, Len(.Text) - 1) >= " " Then
                Set rng = c.Range
            End If
        End With
    Next
    rng.Delete
End Sub

Skip,
Skip@TheOfficeExperts.com
 
Thanks Skip, this does the job.

Let me explain what it is I am trying to do & you might have a more elegant solution.

I have an automated process using SAS that produces a list in Excel, this list needs to be in Excel initially since there is a lot of logic that determines when to move over to the next column and carrying over letters etc... The reports looked excatly lie what the user wanted but all of a sudden they are demanding that it needs to be in word but also editable. So copying the excel sheet as a picture to paste in word is not possible because they will not be able to edit it. Doing this manually is also not possible, since these are produced in massive quantities.

Presently I am copying and pasting the information from Excel to Word as a table to preserve the formatting. Do you have any suggestions on how I could do this easier?
Thanks,



Michael

 
Are you using SAS to produce a flat file that gets imported into a table?

I you're accessing a database from SAS, you would be lots better off to use DAO or ADO to access the database directly. Excel can be an embedded object and editable.

OR you could use MS Query.

Skip,
Skip@TheOfficeExperts.com
 
SAS produces a fully formatted excel file, I can't see how using DAO or ADO would improve this process, since the end data would still end up in Excel.



Michael

 
Well, why couldn't your table be a linked excel file?

BTW, when you asked about "deleting" a Word table cell, did you actually mean you wanted the cell removed from the table, or the contents of the cell deleted?

Skip,
Skip@TheOfficeExperts.com
 
There is something you can help me with here.
What would be the VBA code to open a excel worksheet from location c:\temp\chain_list.xls, copy the entire page and paste it in word & save that doc in location c:\temp\chain_list.doc and deleting that doc if it exists before.
Thanks,



Michael

 
How 'bout this...
Code:
Sub EmbedExcelAndSave()
    Dim xlBook As Excel.Workbook, xlApp As Excel.Application
    
    Application.DisplayAlerts = wdAlertsNone
    
    Set xlApp = CreateObject("Excel.Application")
    Set xlBook = CreateObject("c:\temp\chain_list.xls")
    
    xlBook.Worksheets(1).UsedRange.Copy
    
    With ActiveDocument
        .Range.Paste
        .SaveAs "c:\temp\chain_list.doc"
    End With
    xlApp.DisplayAlerts = False
    xlBook.Close
    Set xlBook = Nothing
    Set xlApp = Nothing
End Sub

Skip,
Skip@TheOfficeExperts.com
 
I tried this & it keeps failing at the paste command, says needs object
Sub EmbedExcelAndSave()
Dim xlBook As Excel.Workbook, xlApp As Excel.Application

Application.DisplayAlerts = wdAlertsNone

Set xlApp = CreateObject("Excel.Application")
Set xlBook = CreateObject("c:\temp\chain_list.xls")

xlBook.Worksheets(1).UsedRange.Copy

With ActiveDocument
.Range.Paste
.SaveAs "c:\temp\chain_list.doc"
End With
xlApp.DisplayAlerts = False
xlBook.Close
Set xlBook = Nothing
Set xlApp = Nothing
End Sub



Michael

 
I am a little confused, should I run this from word or excel. I tried both. When I run this from Excel, I get the paste error & when I run this from word it does not recognize
Dim xlBook As Excel.Workbook

It says "User Defined type not defined"
Thanks



Michael

 
We are almost there, the paste worked but it put the data on several pages, this needs to be pasted as a paste special formatted text(rtf), would you know how to do that?
Thanks



Michael

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top