如何在表格单元格内添加文本框?

发布于 2025-01-02 23:03:00 字数 1346 浏览 1 评论 0原文

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 技术交流群。

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

发布评论

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

评论(1

み青杉依旧 2025-01-09 23:03:00

即使这是一个老问题,我还是想调查一下。我尝试了您的代码(添加了对 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).

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