Microsoft Office Tutorials and References
In Depth Information
Forcing the User to Enable Macros
The sample code demonstrates many of the basics: how to insert text, fields,
links to outside files, and how to apply formatting.
View the Appendix to learn how to store this procedure
in a Standard module.
Option explicit¶
' * * * * *¶
'Path to be used in the field code¶
'linking in the Excel file¶
'Field codes need double backslashes¶
Const ReportPath As String = "C:\\test\\WeeklyReport.xls"¶
' * * * * *¶
Sub GenerateDocument()¶
'Variable declaration¶
Dim doc As Word.Document¶
Dim rng As Word.Range¶
Dim fld As Word.Field¶
Set doc = ActiveDocument¶
Set rng = doc.Range¶
rng.Text = "Weekly Report" & vbCr¶
rng.Style = wdStyleTitle¶
rng.Collapse wdCollapseEnd¶
rng.Text = "Regional Sales (East coast)" & vbCr¶
rng.Style = wdStyleHeading1¶
rng.ParagraphFormat.PageBreakBefore = True¶
rng.Collapse wdCollapseEnd¶
rng.Style = wdStyleBodyText¶
Set fld = doc.Fields.Add(Range:=rng, _¶
Type:=wdFieldMacroButton, _¶
Text:="no macro " & _¶
"Click here and type East coast regional sales info", _¶
PreserveFormatting:=False)¶
rng.Start = fld.Result.End¶
rng.InsertAfter vbCr & vbCr¶
rng.Collapse wdCollapseEnd¶
Set fld = doc.Fields.Add(Range:=rng, _¶
Type:=wdFieldEmpty, _¶
Text:="LINK Excel.Sheet.8 " & Chr$(34) _¶
& ReportPath & Chr$(34) & " " & Chr$(34) _¶
& "EastCoast!EastCoast" & Chr$(34) _¶
& " \a \r", _¶
PreserveFormatting:=False)¶
fld.Result.Tables(1).Columns.AutoFit¶
Set rng = fld.Result¶
fld.Unlink¶
rng.Collapse wdCollapseEnd¶
End Sub¶
Wrd
Search JabSto ::




Custom Search