Belch
04-05-2006, 01:55 AM
Hi all,
I have some code that adds a text box and then amends the text within it (font, size, etc).
I actually just recorded the action to a macro and then used that code. I have noticed the resulting code uses a lot of With statements and sets a lot of properties. I have also noticed that documents with this code in seem to take longer to load (the code is run when the doc opens).
Could the speed difference be due to the inefficiency of the code? If so, any suggestions on how to improve it would be appreciated.
The code at the moment:
With ActiveWindow.Selection
.GoTo What:=wdGoToPage, Which:=wdGoToFirst
End With
ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 175, _
850, 245, 31).Select
Selection.ShapeRange.TextFrame.TextRange.Select
With Selection
.Collapse
.ShapeRange.Select
With .Font
.Name = "Arial"
.Size = 12
.Bold = True
.Italic = False
End With
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.TypeText Text:="N O N E"
With .ShapeRange
.Select
.ScaleHeight 0.62, msoFalse, msoScaleFromTopLeft
.IncrementTop -149.8
.Fill.Visible = msoFalse
.Fill.Transparency = 0#
With .Line
.Weight = 0.75
.DashStyle = msoLineSolid
.Style = msoLineSingle
.Transparency = 0#
.Visible = msoFalse
End With
.LockAspectRatio = msoFalse
.Height = 19.45
.Width = 245.5
.TextFrame.MarginLeft = 7.2
.TextFrame.MarginRight = 7.2
.TextFrame.MarginTop = 3.6
.TextFrame.MarginBottom = 3.6
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
.LockAnchor = False
With .WrapFormat
.AllowOverlap = True
.Side = wdWrapBoth
.DistanceTop = InchesToPoints(0)
.DistanceBottom = InchesToPoints(0)
.DistanceLeft = InchesToPoints(0.13)
.DistanceRight = InchesToPoints(0.13)
.Type = 3
End With
.ZOrder 5
End With
End With
I have some code that adds a text box and then amends the text within it (font, size, etc).
I actually just recorded the action to a macro and then used that code. I have noticed the resulting code uses a lot of With statements and sets a lot of properties. I have also noticed that documents with this code in seem to take longer to load (the code is run when the doc opens).
Could the speed difference be due to the inefficiency of the code? If so, any suggestions on how to improve it would be appreciated.
The code at the moment:
With ActiveWindow.Selection
.GoTo What:=wdGoToPage, Which:=wdGoToFirst
End With
ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 175, _
850, 245, 31).Select
Selection.ShapeRange.TextFrame.TextRange.Select
With Selection
.Collapse
.ShapeRange.Select
With .Font
.Name = "Arial"
.Size = 12
.Bold = True
.Italic = False
End With
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.TypeText Text:="N O N E"
With .ShapeRange
.Select
.ScaleHeight 0.62, msoFalse, msoScaleFromTopLeft
.IncrementTop -149.8
.Fill.Visible = msoFalse
.Fill.Transparency = 0#
With .Line
.Weight = 0.75
.DashStyle = msoLineSolid
.Style = msoLineSingle
.Transparency = 0#
.Visible = msoFalse
End With
.LockAspectRatio = msoFalse
.Height = 19.45
.Width = 245.5
.TextFrame.MarginLeft = 7.2
.TextFrame.MarginRight = 7.2
.TextFrame.MarginTop = 3.6
.TextFrame.MarginBottom = 3.6
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
.LockAnchor = False
With .WrapFormat
.AllowOverlap = True
.Side = wdWrapBoth
.DistanceTop = InchesToPoints(0)
.DistanceBottom = InchesToPoints(0)
.DistanceLeft = InchesToPoints(0.13)
.DistanceRight = InchesToPoints(0.13)
.Type = 3
End With
.ZOrder 5
End With
End With