如何在表格单元格内添加文本框?
Sub t()
Dim File As String
File = "C:\Users\Tin\Desktop\a.docx"
Dim oWord As Word.Application
Set oWord = New Word.Application
Dim oDoc As Word.Document
Set oDoc = oWord.Documents.Open(File)
oWord.Visible = True
With oDoc
Dim Table1 As Word.Table
Set Table1 = .Tables.Add(Range:=.Range, NumRows:=2, NumColumns:=2)
Table1.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
Table1.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
Table1.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
Table1.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
Table1.Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle
Table1.Borders(wdBorderVertical).LineStyle = wdLineStyleSingle
Table1.Rows.Height = 50
Dim lLeft As Long
Dim lTop As Long
Dim Rng As Word.Range
Set Rng = .Tables(1).Cell(1, 1).Range
Dim Shp As Word.Shape
lLeft = .Tables(1).Cell(1, 1).Range.Information(wdHorizontalPositionRelativeToPage) + 50
lTop = .Tables(1).Cell(1, 1).Range.Information(wdVerticalPositionRelativeToPage) + 20
Debug.Print (lLeft)
Debug.Print (lTop)
Set Shp = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=lLeft, _
Top:=iTop, Width:=20, Height:=20, Anchor:=Rng)
End With
End Sub
我正在 Excel 中自动化 Word。 我尝试调整文本框位置但失败。它始终位于桌子之外。 我可以在 Word 本身中实现自动化,但在 Excel 中控制 Word 文本框在定位方面对我来说似乎“无法理解”。
Sub t()
Dim File As String
File = "C:\Users\Tin\Desktop\a.docx"
Dim oWord As Word.Application
Set oWord = New Word.Application
Dim oDoc As Word.Document
Set oDoc = oWord.Documents.Open(File)
oWord.Visible = True
With oDoc
Dim Table1 As Word.Table
Set Table1 = .Tables.Add(Range:=.Range, NumRows:=2, NumColumns:=2)
Table1.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
Table1.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
Table1.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
Table1.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
Table1.Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle
Table1.Borders(wdBorderVertical).LineStyle = wdLineStyleSingle
Table1.Rows.Height = 50
Dim lLeft As Long
Dim lTop As Long
Dim Rng As Word.Range
Set Rng = .Tables(1).Cell(1, 1).Range
Dim Shp As Word.Shape
lLeft = .Tables(1).Cell(1, 1).Range.Information(wdHorizontalPositionRelativeToPage) + 50
lTop = .Tables(1).Cell(1, 1).Range.Information(wdVerticalPositionRelativeToPage) + 20
Debug.Print (lLeft)
Debug.Print (lTop)
Set Shp = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=lLeft, _
Top:=iTop, Width:=20, Height:=20, Anchor:=Rng)
End With
End Sub
I am automating Word in Excel.
I have tried to adjust the textbox location but failed. It always stays outside of the table.
I can automate it in Word itself but in Excel controlling Word Textbox seems "not understandable" to me when it comes to positioning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使这是一个老问题,我还是想调查一下。我尝试了您的代码(添加了对 Word 对象库的引用的 Excel 宏),但没有发现任何问题。 您的代码按预期工作,第一个单元格内呈正方形。
我使用的是 Office 2010(意大利语)。
Even if this is an old question I wanted anyway to investigate it. I tried your code (Excel macro with an added reference to the Word Object Library) and couldn't find something wrong with it. Your code works as expected, a square shape inside the first cell.
I'm using Office 2010 (italian language).