|
在VB中引用Microsoft Word 9.0 Object Library及
Microsoft Visual Basic For Application Extensibility 5.3
新建一按钮,输入如下代码:
Private Sub Command1_Click()
Dim app As New Word.Application
app.Visible = True
app.Documents.Add
Dim uf3 As VBComponent
Dim ufwin As Variant
Dim bt As Variant
Set uf3 = NormalTemplate.VBProject.VBComponents.Add(vbext_ct_MSForm)
uf3.Name = "NewForm1"
Set ufwin = NormalTemplate.VBProject.VBComponents(uf3.Name).Designer
Set bt = ufwin.Controls.Add("Forms.CommandButton.1")
bt.Caption = "New Button"
bt.Name = "Button1"
bt.Top = 30
bt.Left = 30
bt.Visible = True
l = NormalTemplate.VBProject.VBComponents(uf3.Name).CodeModule.CreateEventProc("Click", bt.Name)
NormalTemplate.VBProject.VBComponents(uf3.Name).CodeModule.InsertLines l + 1, "msgbox""Hei,Man!"
Set uf3 = Nothing
Set ufwin = Nothing
Set bt = Nothing
End Sub |
|