+ -
当前位置:首页 → 问答吧 → Word 中用VBA插入文本框的方法

Word 中用VBA插入文本框的方法

时间:2010-12-08

来源:互联网

请教:WORD中如何写“在选定的一段文字前插入文本框”的VBA代码。

谢谢了!

作者: alanxujian   发布时间: 2010-12-08

Selection.HeaderFooter.Shapes.AddTextbox(msoTextOrientationHorizontal, 750, 70.87, 35, 453.6).Select
    Selection.ShapeRange.TextFrame.TextRange.Select
    Selection.Collapse
    Selection.ShapeRange.Select
    Selection.ShapeRange.Fill.Visible = msoFalse
    Selection.ShapeRange.Fill.Solid
    Selection.ShapeRange.Fill.Transparency = 0#
    Selection.ShapeRange.Line.Weight = 0
    Selection.ShapeRange.Line.DashStyle = msoLineSolid
    Selection.ShapeRange.Line.Style = msoLineSingle
    Selection.ShapeRange.Line.Transparency = 0
    Selection.ShapeRange.Line.Visible = msoFalse
    Selection.ShapeRange.LockAspectRatio = msoFalse
    Selection.ShapeRange.Height = CentimetersToPoints(15.96)     '设置文本框高度
    Selection.ShapeRange.Width = CentimetersToPoints(20)     '设置文本框宽度
    Selection.ShapeRange.Left = CentimetersToPoints(25)       '设置文本框左边相对位置(厘米转为磅)
    Selection.ShapeRange.Top = CentimetersToPoints(0.7)       '设置文本框顶边相对位置
    Selection.ShapeRange.TextFrame.MarginLeft = 0
    Selection.ShapeRange.TextFrame.MarginRight = 0
    Selection.ShapeRange.TextFrame.MarginTop = 0
    Selection.ShapeRange.TextFrame.MarginBottom = 0
    Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
    Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
    Selection.ShapeRange.LockAnchor = False
    Selection.ShapeRange.LayoutInCell = True
    Selection.ShapeRange.WrapFormat.AllowOverlap = True
    Selection.ShapeRange.WrapFormat.Side = wdWrapBoth
    Selection.ShapeRange.WrapFormat.DistanceTop = CentimetersToPoints(0)
    Selection.ShapeRange.WrapFormat.DistanceBottom = CentimetersToPoints(0)
    Selection.ShapeRange.WrapFormat.DistanceLeft = CentimetersToPoints(0)
    Selection.ShapeRange.WrapFormat.DistanceRight = CentimetersToPoints(0)
    Selection.ShapeRange.WrapFormat.Type = 3
    Selection.ShapeRange.ZOrder 4
    Selection.ShapeRange.TextFrame.AutoSize = False
    Selection.ShapeRange.TextFrame.WordWrap = True
    Selection.ShapeRange.TextFrame.TextRange.Select                     '选中文本框内容
  Selection.Collapse
    Selection.Orientation = wdTextOrientationVerticalFarEast
   Selection.TypeText Text:=InputBox("输入页眉")

作者: dongfangeia   发布时间: 2010-12-10