如何用VBA删除Word中的单元格内容?

发布于 2024-11-04 14:12:57 字数 156 浏览 3 评论 0原文

我查看了 VBA 中表格单元格对象和选择对象的文档,但没有看到任何方法可以在保留单元格本身的同时删除 Word 中的单元格内容。看起来在 Excel 中这样做很容易,但在 Word 中几乎不可能。

我需要执行此操作的某些单元格将包含文本,其他单元格将包含文本表单字段。有什么想法吗?

I've looked at the documentation for table cell objects and selection objects in VBA, and I didn't see any way to delete cell contents in Word while retaining the cell itself. It looks like doing so is easy in Excel, and next to impossible in Word.

Some cells I need to do this for will contain text, others will contain text form fields. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南街九尾狐 2024-11-11 14:12:57

这是有效的:

ActiveDocument.Tables(1).Cell(1, 2).Select
Selection.Delete

这会删除单元格内容,但留下空单元格。

操作不一样

ActiveDocument.Tables(1).Cell(1, 2).Delete

我理解您的沮丧,因为奇怪的是,上面的操作与删除整个单元格的

!前者相当于选择一个单元格并按Delete键(这会清除内容但将单元格保留在原位)。后者相当于右键单击单元格并选择“删除单元格...”(这将删除单元格)。

This works:

ActiveDocument.Tables(1).Cell(1, 2).Select
Selection.Delete

This deletes the cell contents but leaves the empty cell behind.

I understand your dismay, because oddly, the above does not do the same as

ActiveDocument.Tables(1).Cell(1, 2).Delete

which deletes the entire cell!

The former is the equivalent of selecting a cell and pressing the Delete key (which clears the contents but leaves the cell in place). The latter is the equivalent of right-clicking a cell and choosing "Delete cells..." (which deletes the cell).

世界和平 2024-11-11 14:12:57

很抱歉挖掘出这样一个老问题,但希望有人会发现这很有用。如果您希望避免使用 Select 方法,那么您需要的是以下内容:

ActiveDocument.Tables(1).Cell(1, 1).Range.Text = ""

它还会删除图像和内容控件。

Sorry for digging up such an old question, but hopefully someone will find this useful. If you prefer to avoid the Select method, the following is what you're looking for:

ActiveDocument.Tables(1).Cell(1, 1).Range.Text = ""

It deletes images and content controls as well.

╭ゆ眷念 2024-11-11 14:12:57

我从互联网的各个部分拼凑了这个......包括 VBA Express 的 Fumei。运作良好。选择表格中的任何单元格并运行宏 deleteTableCells

Sub deleteTableCells()
Dim selectedRange As Range

On Error GoTo Errorhandler
Set selectedRange = SelectionInfo

selectedRange.Delete

Errorhandler:
Exit Sub
End Sub

Function SelectionInfo() As Range
     '
    Dim iSelectionRowEnd As Integer
    Dim iSelectionRowStart As Integer
    Dim iSelectionColumnEnd As Integer
    Dim iSelectionColumnStart As Integer
    Dim lngStart As Long
    Dim lngEnd As Long
     
     ' Check if Selection IS in a table
     ' if not, exit Sub after message
    If Selection.Information(wdWithInTable) = False Then
        Err.Raise (2022)
    Else
        lngStart = Selection.Range.Start
        lngEnd = Selection.Range.End
         
         ' get the numbers for the END of the selection range
        iSelectionRowEnd = Selection.Information(wdEndOfRangeRowNumber)
        iSelectionColumnEnd = Selection.Information(wdEndOfRangeColumnNumber)
         
         ' collapse the selection range
        Selection.Collapse Direction:=wdCollapseStart
         
         ' get the numbers for the END of the selection range
         ' now of course the START of the previous selection
        iSelectionRowStart = Selection.Information(wdEndOfRangeRowNumber)
        iSelectionColumnStart = Selection.Information(wdEndOfRangeColumnNumber)
         
         ' RESELECT the same range
        Selection.MoveEnd Unit:=wdCharacter, Count:=lngEnd - lngStart
         
         ' set the range of cells for consumption
        With ActiveDocument
            Set SelectionInfo = .Range(Start:=.Tables(1).cell(iSelectionRowStart, iSelectionColumnStart).Range.Start, _
                            End:=.Tables(1).cell(iSelectionRowEnd, iSelectionColumnEnd).Range.End)
        
        End With
    End If
End Function

I cobbled this together from various parts of the interwebs... including Fumei from VBA Express. It's working well. Select any cells in your table and run the macro deleteTableCells

Sub deleteTableCells()
Dim selectedRange As Range

On Error GoTo Errorhandler
Set selectedRange = SelectionInfo

selectedRange.Delete

Errorhandler:
Exit Sub
End Sub

Function SelectionInfo() As Range
     '
    Dim iSelectionRowEnd As Integer
    Dim iSelectionRowStart As Integer
    Dim iSelectionColumnEnd As Integer
    Dim iSelectionColumnStart As Integer
    Dim lngStart As Long
    Dim lngEnd As Long
     
     ' Check if Selection IS in a table
     ' if not, exit Sub after message
    If Selection.Information(wdWithInTable) = False Then
        Err.Raise (2022)
    Else
        lngStart = Selection.Range.Start
        lngEnd = Selection.Range.End
         
         ' get the numbers for the END of the selection range
        iSelectionRowEnd = Selection.Information(wdEndOfRangeRowNumber)
        iSelectionColumnEnd = Selection.Information(wdEndOfRangeColumnNumber)
         
         ' collapse the selection range
        Selection.Collapse Direction:=wdCollapseStart
         
         ' get the numbers for the END of the selection range
         ' now of course the START of the previous selection
        iSelectionRowStart = Selection.Information(wdEndOfRangeRowNumber)
        iSelectionColumnStart = Selection.Information(wdEndOfRangeColumnNumber)
         
         ' RESELECT the same range
        Selection.MoveEnd Unit:=wdCharacter, Count:=lngEnd - lngStart
         
         ' set the range of cells for consumption
        With ActiveDocument
            Set SelectionInfo = .Range(Start:=.Tables(1).cell(iSelectionRowStart, iSelectionColumnStart).Range.Start, _
                            End:=.Tables(1).cell(iSelectionRowEnd, iSelectionColumnEnd).Range.End)
        
        End With
    End If
End Function
迟到的我 2024-11-11 14:12:57
Private Sub cbClearTable_Click()  
'mouse cursor must be in the table for clearing  
Dim cell_BhBp As Cell  
For Each cell_BhBp In Selection.Tables(1).Range.Cells  
cell_BhBp.Range = ""  
Next  
End Sub

上面的代码清除当前表格/鼠标光标所在的表格中的所有单元格中的内容

另一种清除文档中第一个表格的所有表格单元格的方法是

ActiveDocument.Tables(1).Range.Delete

或者对于当前表格/光标所在的位置/

Selection.Tables(1).Range.Delete

    Private Sub CommandButton40_Click()
    Application.Activate
    SendKeys ("{DEL}")
    End Sub

代码上面清除了所有选定单元格的内容。在这种情况下,所选单元格可能不相邻。单击用户表单的按钮时会触发此代码。

Private Sub cbClearTable_Click()  
'mouse cursor must be in the table for clearing  
Dim cell_BhBp As Cell  
For Each cell_BhBp In Selection.Tables(1).Range.Cells  
cell_BhBp.Range = ""  
Next  
End Sub

The code above clears the contents in all cells in the current table /the table, where the mouse cursor is/

One other way to clear all table cells of first table in document is

ActiveDocument.Tables(1).Range.Delete

Or for current table /where the cursor is in/

Selection.Tables(1).Range.Delete

    Private Sub CommandButton40_Click()
    Application.Activate
    SendKeys ("{DEL}")
    End Sub

The code above clears contents of all selected cells. In this case, the selected cells may not be adjacent. This code is fired when button of user form is clicked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文