Code:
Sub TypeTextTypeUnderline()
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
oDoc.Activate
Dim tbl As Word.Table
Dim Shp As Shape, Rng As Range
With ActiveDocument
Set tbl = .Tables.Add(Range:=.Range, _
NumRows:=2, _
NumColumns:=2, _
DefaultTableBehavior:=wdWord8TableBehavior)
With tbl
.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
.Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle
.Borders(wdBorderVertical).LineStyle = wdLineStyleSingle
End With
''' Runtime Error 13
''' Error: Type Mismatch
Set Rng = .Tables(1).Cell(2, 2).Range
''' It seems Table.Cell.Range cannot be set to Range
Rng.Collapse wdCollapseStart
Set Shp = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
Left:=0, Top:=0, Width:=72, Height:=12, Anchor:=Rng)
With Shp
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelaitveVerticalPosition = wdRelativeVerticalPositionPage
End With
End With
End Sub
I type the VBA codes in Excel.
It shows error in Word (please see the comments above within the codes).