wrote :: 2007.05.03
Sub Sample1()
Dim VBC
Const Path As String = "C:\Work\"
With ActiveWorkbook.VBProject
For Each VBC In .VBComponents
If VBC.Type = 1 And _
VBC.CodeModule.CountOfDeclarationLines <> VBC.CodeModule.CountOfLines Then
VBC.Export Path & VBC.Name & ".bas"
End If
Next VBC
End With
End Sub
Sub Sample2()
Dim Target As Workbook, buf As String, VBC, cnt As Long
Const Path As String = "C:\Tmp\"
buf = Dir(Path & "*.xls")
Do While buf <> ""
Set Target = Workbooks.Open(Path & buf)
With Target.VBProject
cnt = 0
For Each VBC In .VBComponents
If VBC.Type = 1 Then cnt = cnt + 1
Next VBC
If cnt = 0 Then
.VBComponents.Add 1
.VBComponents("Module1").CodeModule.AddFromFile "C:\Work\Macro.txt"
End If
End With
Target.Save
Target.Close
buf = Dir()
Loop
End Sub
Sub Sample3()
Dim Target As Workbook, buf As String, VBC, i As Long
Const Path As String = "C:\Tmp\"
buf = Dir(Path & "*.xls")
Do While buf <> ""
Set Target = Workbooks.Open(Path & buf)
With Target.VBProject.VBComponents("Module1").CodeModule
For i = 1 To .CountOfLines
If .Lines(i, 1) = " Const StartDay As Date = #4/1/2006#" Then
.ReplaceLine i, " Const StartDay As Date = #9/1/2007#"
End If
Next i
End With
Target.Save
Target.Close
buf = Dir()
Loop
End Sub
Sub Sample4()
Dim VBP, Code As String
With Workbooks("Book1").VBProject.VBComponents("Module1").CodeModule
Code = .Lines(1, .CountOfLines)
End With
With Workbooks("Book2").VBProject.VBComponents.Add(1)
.CodeModule.AddFromString Code
End With
End Sub
Sub Sample4()
Dim VBP, Code As String
With Workbooks("Book1").VBProject.VBComponents("Module1").CodeModule
Code = .Lines(.CountOfDeclarationLines + 1, .CountOfLines - .CountOfDeclarationLines + 1)
End With
With Workbooks("Book2").VBProject.VBComponents.Add(1)
.CodeModule.AddFromString Code
End With
End Sub
Sub Sample5()
Dim myNewForm, myControl, n As Long
Set myNewForm = ActiveWorkbook.VBProject.VBComponents.Add(ComponentType:=3)
With myNewForm
.Properties("Height") = 180
.Properties("Width") = 240
.Properties("Caption") = "ユーザー設定"
End With
Set myControl = myNewForm.Designer.Controls.Add("Forms.ListBox.1")
With myControl
.Left = 10
.Top = 10
.Height = 130
.Width = 100
End With
Set myControl = myNewForm.Designer.Controls.Add("Forms.TextBox.1")
With myControl
.Left = 120
.Top = 10
.Height = 15
.Width = 100
End With
Set myControl = myNewForm.Designer.Controls.Add("Forms.CheckBox.1")
With myControl
.Left = 120
.Top = 36
.Caption = "チェック1"
.AutoSize = True
.Value = True
End With
Set myControl = myNewForm.Designer.Controls.Add("Forms.CommandButton.1")
With myControl
.Left = 160
.Top = 120
.Height = 18
.Width = 60
.Caption = "閉じる"
End With
n = myNewForm.CodeModule.CreateEventProc("Click", myControl.Name)
myNewForm.CodeModule.ReplaceLine n + 1, vbTab & "Unload Me"
End Sub
