如何把Word中的VB代码复制到另一Word文件中
编号:QA003995
建立日期: 2001年3月15日 最后修改日期:2001年3月15日
所属类别:
Makoto.g:
操作系统:Win9X
编程工具:VB & Word VBA
问题:如何把Word中的VB代码!复制到另一Word文件中!我是指通过WORD自身的VB代码实现!谢谢!
水平: 中级
回答:
'可以使用VBProject对象.参考下面这段代码:
Sub CopyCodetoDOC(ByVal docPath As String)
Dim doc As Document, linecount As Long
Dim cms
Set cms = VBE.ActiveVBProject.VBComponents
linecount = cms.Item(1).CodeModule.CountOfLines
if dir(docPath)<>"" then
Set doc = Documents.Open(docPath, , False, False)
else
set doc=Documents.Add
doc.SaveAs docPath
end if
With doc.VBProject.VBComponents.Item(1).CodeModule
.DeleteLines 1, .CountOfLines
If linecount > 0 Then
.AddFromString cms.Item(1).CodeModule.Lines(1, linecount)
End If
End With
Dim m
For Each m In doc.VBProject.VBComponents
If m.Type <> 100 Then'Type:1:标准模块;2:类模块;3:窗体;100:Document
doc.VBProject.VBComponents.Remove m
End If
Next
For Each m In cms
If m.Type <> 100 Then
With doc.VBProject.VBComponents.Add(m.Type)
.Name = m.Name
.CodeModule.AddFromString m.CodeModule.Lines(1, m.CodeModule.CountOfLines)
End With
End If
Next
Set m = Nothing
Set cms = Nothing
doc.Close True
End Sub
'另外也可以看看梅丽莎病毒的源码。
此问题由Oscar回答。
| |
|
|
| |
|
|