将光标位置移动/更改到表格后的新行

huangapple go评论93阅读模式
英文:

Placing/changing cursor position to a new line after the table

问题

以下是代码的翻译部分:

Sub test_table()
    Dim objWord
    Dim objDoc
    Dim objSelection
    
    Dim i As Integer
    Dim j As Integer
    
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
    Set objSelection = objWord.Selection
    
    objWord.Visible = True
    objWord.Activate
    
    objSelection.Font.Size = 12
    objSelection.Font.Name = "Arial"
    objSelection.Font.Color = RGB(0, 0, 0)
    objSelection.TypeText "相应地,"
    
    Set country_table = objDoc.Tables.Add(objSelection.Range, 4, 5)
    
    With country_table
        
        With .Borders
            .Enable = True
            .OutsideColor = RGB(0, 0, 0)
            .InsideColor = RGB(0, 0, 0)
        End With
       
        .Rows(1).Shading.BackgroundPatternColor = RGB(230, 230, 230)
        .Cell(1, 1).Range.InsertAfter "零件编号"
        .Cell(1, 2).Range.InsertAfter "物品描述"
        .Cell(1, 3).Range.InsertAfter "MRP 变化百分比"
        .Cell(1, 4).Range.InsertAfter "报价率变化百分比"
        .Cell(1, 5).Range.InsertAfter "折扣"
        For i = 1 To 3
            .Cell(i + 1, 1).Range.InsertAfter ThisWorkbook.Sheets("概览").Cells(12 + i, 1).Text
            .Cell(i + 1, 2).Range.InsertAfter ThisWorkbook.Sheets("概览").Cells(12 + i, 2).Text & ThisWorkbook.Sheets("概览").Cells(12 + i, 4).Text
            If IsError(ThisWorkbook.Sheets("协商").Cells(16, (5 * i) - 2).Value) Then
                If ThisWorkbook.Sheets("协商").Cells(16, (5 * i) - 2).Value = CVErr(xlErrDiv0) Then
                    .Cell(i + 1, 3).Range.InsertAfter "NA"
                End If
            Else
                .Cell(i + 1, 3).Range.InsertAfter ThisWorkbook.Sheets("协商").Cells(16, (5 * i) - 2).Text
            End If
                
            If IsError(ThisWorkbook.Sheets("协商").Cells(17, (5 * i) - 2).Value) Then
                If ThisWorkbook.Sheets("协商").Cells(17, (5 * i) - 2).Value = CVErr(xlErrDiv0) Then
                    .Cell(i + 1, 4).Range.InsertAfter "NA"
                End If
            Else
                .Cell(i + 1, 4).Range.InsertAfter ThisWorkbook.Sheets("协商").Cells(17, (5 * i) - 2).Text
            End If
            .Cell(i + 1, 5).Range.InsertAfter ThisWorkbook.Sheets("协商").Cells(18, (5 * i) - 2).Text
        Next i
        
    End With
    
End Sub

在子程序执行结束后,光标位于单元格 (1,1) 的开头。但是,我希望将光标放在表格后的新行中。

英文:

The following code picks and places Excel data to the table in a Word file.

Sub test_table()
    Dim objWord
    Dim objDoc
    Dim objSelection
    
    Dim i As Integer
    Dim j As Integer
    
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
    Set objSelection = objWord.Selection
    
    objWord.Visible = True
    objWord.Activate
    
    objSelection.Font.Size = 12
    objSelection.Font.Name = "Arial"
    objSelection.Font.Color = RGB(0, 0, 0)
    objSelection.TypeText "Accordingly, the " & vbLf
    
    Set country_table = objDoc.Tables.Add(objSelection.Range, 4, 5)
    
    With country_table
        
        With .Borders
            .enable = True
            .outsidecolor = RGB(0, 0, 0)
            .insidecolor = RGB(0, 0, 0)
        End With
       
        .Rows(1).shading.backgroundpatterncolor = RGB(230, 230, 230)
        .cell(1, 1).Range.InsertAfter "Part Number"
        .cell(1, 2).Range.InsertAfter "Item Description"
        .cell(1, 3).Range.InsertAfter "% of change in MRP"
        .cell(1, 4).Range.InsertAfter "% of change in offered rate"
        .cell(1, 5).Range.InsertAfter "Discount"
        For i = 1 To 3
            'For j = 1 To 3
                .cell(i + 1, 1).Range.InsertAfter ThisWorkbook.Sheets("Overview").Cells(12 + i, 1).Text
                .cell(i + 1, 2).Range.InsertAfter ThisWorkbook.Sheets("Overview").Cells(12 + i, 2).Text & ThisWorkbook.Sheets("Overview").Cells(12 + i, 4).Text
                If IsError(ThisWorkbook.Sheets("Negotiation").Cells(16, (5 * i) - 2).Value) Then
                    If ThisWorkbook.Sheets("Negotiation").Cells(16, (5 * i) - 2).Value = CVErr(xlErrDiv0) Then
                        .cell(i + 1, 3).Range.InsertAfter "NA"
                    End If
                Else
                    .cell(i + 1, 3).Range.InsertAfter ThisWorkbook.Sheets("Negotiation").Cells(16, (5 * i) - 2).Text
                    
                End If
                
                '.cell(i + 1, 3).Range.InsertAfter ThisWorkbook.Sheets("Negotiation").Cells(16, (5 * i) - 2).Text
                If IsError(ThisWorkbook.Sheets("Negotiation").Cells(17, (5 * i) - 2).Value) Then
                    If ThisWorkbook.Sheets("Negotiation").Cells(17, (5 * i) - 2).Value = CVErr(xlErrDiv0) Then
                        .cell(i + 1, 4).Range.InsertAfter "NA"
                    End If
                Else
                    .cell(i + 1, 4).Range.InsertAfter ThisWorkbook.Sheets("Negotiation").Cells(17, (5 * i) - 2).Text
                    
                End If
                '.cell(i + 1, 4).Range.InsertAfter ThisWorkbook.Sheets("Negotiation").Cells(17, (5 * i) - 2).Text
                .cell(i + 1, 5).Range.InsertAfter ThisWorkbook.Sheets("Negotiation").Cells(18, (5 * i) - 2).Text
            'Next j
        Next i
        
    End With
    
End Sub

At the end of the execution of the subroutine, the cursor is landed the beginning of cell (1,1). Whereas, I wanted to place the cursor just after the table in a new line.

答案1

得分: 0

你可以使用这段代码将光标定位在表格之后的第一个段落:

country_table.Range.Next(4, 1).Select 'wdParagraph = 4
objWord.Selection.Collapse 1 'wdCollapseStart

Range.Next 根据起始点(表格的范围)和单位(wdParagraph)来识别下一个范围。

英文:

You can use this code to place the cursor in the first paragraph after the table:

country_table.Range.Next(4, 1).Select 'wdParagraph = 4
objWord.Selection.Collapse 1  'wdCollapseStart

Range.Next identifies the next range based on the starting point (range of your table) and the unit (wdParagraph).

答案2

得分: 0

InsertAfter 方法不会重新定位 Word 中的光标。要额外移动光标,您可以使用其他方法,比如:

选项 1

country_table.Select
Selection.MoveDown Unit:=5, Count:=1

选项 2

objDoc.Content.MoveStart Unit:=1, Count:=country_table.Range.End
objDoc.Content.Collapse Direction:=1
objDoc.Content.Select

提供的代码片段应该在 End Sub 之前集成。

英文:

The InsertAfter method does NOT reposition the cursor in Word. To additionally move the cursor, you can use other approaches such as:

> Option 1

    country_table.Select
    Selection.MoveDown Unit:=5, Count:=1

> Option 2

    objDoc.Content.MoveStart Unit:=1, Count:=country_table.Range.End
    objDoc.Content.Collapse Direction:=1
    objDoc.Content.Select

The provided code snippet should be integrated right before End Sub.

答案3

得分: 0

最简单的方法是在 End With 后面添加 objDoc.Characters.Last.Select

然而,更好的做法是完全避免使用 Selection。在整个 Office 套件中工作时,使用 Late Binding(将 Word 声明为无类型对象)也是不好的做法,因为这种做法没有任何优势。相反,你应该设置对 Word 库的引用。

Sub test_table()
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    
    Dim i As Integer
    Dim j As Integer
    
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
    
    objWord.Visible = True
    objWord.Activate
    
    With objDoc.Content
        With .Font
            .Size = 12
            .Name = "Arial"
            .Color = RGB(0, 0, 0)
        End With
        .Text "Accordingly, the " & vbCr
        
        Set country_table = .Tables.Add(.Characters.Last, 4, 5)
    End With
    
    With country_table
        
        With .Borders
            .enable = True
            .outsidecolor = RGB(0, 0, 0)
            .insidecolor = RGB(0, 0, 0)
        End With
        
        .Rows(1).shading.backgroundpatterncolor = RGB(230, 230, 230)
        .cell(1, 1).Range.InsertAfter "Part Number"
        .cell(1, 2).Range.InsertAfter "Item Description"
        .cell(1, 3).Range.InsertAfter "% of change in MRP"
        .cell(1, 4).Range.InsertAfter "% of change in offered rate"
        .cell(1, 5).Range.InsertAfter "Discount"
        For i = 1 To 3
            .cell(i + 1, 1).Range.InsertAfter ThisWorkbook.Sheets("Overview").Cells(12 + i, 1).Text
            .cell(i + 1, 2).Range.InsertAfter ThisWorkbook.Sheets("Overview").Cells(12 + i, 2).Text & ThisWorkbook.Sheets("Overview").Cells(12 + i, 4).Text
            If IsError(ThisWorkbook.Sheets("Negotiation").Cells(16, (5 * i) - 2).Value) Then
                If ThisWorkbook.Sheets("Negotiation").Cells(16, (5 * i) - 2).Value = CVErr(xlErrDiv0) Then
                    .cell(i + 1, 3).Range.InsertAfter "NA"
                End If
            Else
                .cell(i + 1, 3).Range.InsertAfter ThisWorkbook.Sheets("Negotiation").Cells(16, (5 * i) - 2).Text
            End If
            If IsError(ThisWorkbook.Sheets("Negotiation").Cells(17, (5 * i) - 2).Value) Then
                If ThisWorkbook.Sheets("Negotiation").Cells(17, (5 * i) - 2).Value = CVErr(xlErrDiv0) Then
                    .cell(i + 1, 4).Range.InsertAfter "NA"
                End If
            Else
                .cell(i + 1, 4).Range.InsertAfter ThisWorkbook.Sheets("Negotiation").Cells(17, (5 * i) - 2).Text
            End If
            .cell(i + 1, 5).Range.InsertAfter ThisWorkbook.Sheets("Negotiation").Cells(18, (5 * i) - 2).Text
        Next i
    End With
End Sub
英文:

The simplest way of doing that is to add objDoc.Characters.Last.Select after the End With.

However, it would be far better to avoid using Selection altogether. It is also poor practice to use Late Binding (declaring Word as an untyped object) as this practice has no advantages when working across the Office suite. Instead you should set a reference to the Word library.

Sub test_table()
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    
    Dim i As Integer
    Dim j As Integer
    
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
    
    objWord.Visible = True
    objWord.Activate
    
    With objDoc.Content
        With .Font
            .Size = 12
            .Name = "Arial"
            .Color = RGB(0, 0, 0)
        End With
        .Text "Accordingly, the " & vbCr
    
        Set country_table = .Tables.Add(.Characters.Last, 4, 5)
    End With
    
    With country_table
        
        With .Borders
            .enable = True
            .outsidecolor = RGB(0, 0, 0)
            .insidecolor = RGB(0, 0, 0)
        End With
       
        .Rows(1).shading.backgroundpatterncolor = RGB(230, 230, 230)
        .cell(1, 1).Range.InsertAfter "Part Number"
        .cell(1, 2).Range.InsertAfter "Item Description"
        .cell(1, 3).Range.InsertAfter "% of change in MRP"
        .cell(1, 4).Range.InsertAfter "% of change in offered rate"
        .cell(1, 5).Range.InsertAfter "Discount"
        For i = 1 To 3
            'For j = 1 To 3
            .cell(i + 1, 1).Range.InsertAfter ThisWorkbook.Sheets("Overview").Cells(12 + i, 1).Text
            .cell(i + 1, 2).Range.InsertAfter ThisWorkbook.Sheets("Overview").Cells(12 + i, 2).Text & ThisWorkbook.Sheets("Overview").Cells(12 + i, 4).Text
            If IsError(ThisWorkbook.Sheets("Negotiation").Cells(16, (5 * i) - 2).Value) Then
                If ThisWorkbook.Sheets("Negotiation").Cells(16, (5 * i) - 2).Value = CVErr(xlErrDiv0) Then
                    .cell(i + 1, 3).Range.InsertAfter "NA"
                End If
            Else
                .cell(i + 1, 3).Range.InsertAfter ThisWorkbook.Sheets("Negotiation").Cells(16, (5 * i) - 2).Text
                    
            End If
                
            '.cell(i + 1, 3).Range.InsertAfter ThisWorkbook.Sheets("Negotiation").Cells(16, (5 * i) - 2).Text
            If IsError(ThisWorkbook.Sheets("Negotiation").Cells(17, (5 * i) - 2).Value) Then
                If ThisWorkbook.Sheets("Negotiation").Cells(17, (5 * i) - 2).Value = CVErr(xlErrDiv0) Then
                    .cell(i + 1, 4).Range.InsertAfter "NA"
                End If
            Else
                .cell(i + 1, 4).Range.InsertAfter ThisWorkbook.Sheets("Negotiation").Cells(17, (5 * i) - 2).Text
                    
            End If
            '.cell(i + 1, 4).Range.InsertAfter ThisWorkbook.Sheets("Negotiation").Cells(17, (5 * i) - 2).Text
            .cell(i + 1, 5).Range.InsertAfter ThisWorkbook.Sheets("Negotiation").Cells(18, (5 * i) - 2).Text
            'Next j
        Next i
        
    End With
    
End Sub

huangapple
  • 本文由 发表于 2023年8月9日 13:39:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864868-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定