英文:
Placing/changing cursor position to a new line after the table
问题
以下代码将Excel数据选择并放置到Word文件中的表格中。
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
在子程序执行结束时,光标会停在单元格(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套件中使用后期绑定(将Word声明为未类型化对象)也是不良实践,因为这种做法在跨Office套件工作时没有任何优势。相反,你应该设置对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
'.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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论