向word vba中包含特定字符串的表行添加边框
我有一个word文档,它是从我的程序生成的,目前有一个宏用换行符替换向word vba中包含特定字符串的表行添加边框,vba,ms-word,Vba,Ms Word,我有一个word文档,它是从我的程序生成的,目前有一个宏用换行符替换标记。这表示表中新部分的开始,因此我还想在出现标记的行顶部添加一个边框。我目前的代码是: With ActiveDocument.Content.Find .Text = "<BR/>" .Forward = True While .Execute .Parent.Text = Chr(10) .Parent.Collapse wdCollapseEnd
标记。这表示表中新部分的开始,因此我还想在出现
标记的行顶部添加一个边框。我目前的代码是:
With ActiveDocument.Content.Find
.Text = "<BR/>"
.Forward = True
While .Execute
.Parent.Text = Chr(10)
.Parent.Collapse wdCollapseEnd
Wend
End With
但这没有任何作用(也没有错误)。我认为我没有正确地抓住这一排,但我找不到任何其他方法。谢谢
编辑:
我现在在想也许我应该在换行部分之前做一个单独的检查。因此,在所有表行中添加for循环,检查是否存在
,以及是否在行的顶部添加边框
我不习惯在word中使用vba,通常使用excel,因此我认为我可能会将两者混合使用。以下是我到目前为止得到的信息:
Dim oTbl As Table
Set oTbl = ActiveDocument.Tables(1)
For Each oRow In oTbl.Rows
If InStr(1, oRow.Cell(1, 1), "<BR/>", vbBinaryCompare) > 0 Then
oRow.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
End If
Next
Dim oTbl作为表格
设置oTbl=ActiveDocument.Tables(1)
对于oTbl.行中的每个oRow
如果InStr(1,oRow.Cell(1,1),“
”,vbBinaryCompare)>0,则
oRow.Borders(wdBorderBottom).LineStyle=wdLineStyleSingle
如果结束
下一个
好的,我使用for循环方式让它工作。我最后的代码是:
Dim oTbl As Table
Dim oRow As Row
For Each oTbl In ActiveDocument.Tables
For Each oRow In oTbl.Rows
If InStr(1, oRow.Cells(1).Range.Text, "<BR/>", vbBinaryCompare) > 0 Then
oRow.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
End If
Next
Next
Dim oTbl作为表格
模糊的或像行一样的
对于ActiveDocument.Tables中的每个oTbl
对于oTbl.行中的每个oRow
如果InStr(1,oRow.Cells(1).Range.Text,“
”,vbBinaryCompare)>0,则
oRow.Borders(wdBorderTop).LineStyle=wdLineStyleSingle
如果结束
下一个
下一个
因此,这将遍历文档中的所有表,然后遍历每个表中的所有行,并在第一个单元格中包含
标记的每行上方添加一条边框线(这表示一个新的节)。我在代码的下一部分之前做了所有这些,该部分用新行字符替换
标记
希望这对将来的人有所帮助。为什么要投否决票?我充分地解释了我的问题,展示了我的尝试,甚至最终不得不回答我自己的问题。
Dim oTbl As Table
Dim oRow As Row
For Each oTbl In ActiveDocument.Tables
For Each oRow In oTbl.Rows
If InStr(1, oRow.Cells(1).Range.Text, "<BR/>", vbBinaryCompare) > 0 Then
oRow.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
End If
Next
Next