2012-02-06 83 views
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中自動執行它,但在控制Word文本框的Excel中,對於定位而言,似乎「不可理解」。如何在表格單元格內添加文本框?

回答

0

即使這是一個老問題,我仍然想要調查它。我嘗試了你的代碼(Excel宏,增加了對Word對象庫的引用),並且找不到任何問題。 您的代碼按預期方式工作,第一個單元格內爲正方形。

我正在使用Office 2010(意大利語言)。

相關問題