以下第一个代码,是一个偿试,它解决了如何取得多个(N个)文字选定区域的所有内容。第二个代码,简洁明了,直接以粘贴方式进行另存为。 '* +++++++++++++++++++++++++++++
'* Created By 守柔(ShouRou)@ExcelHome 2005-2-23 5:37:54
'仅测试于System: Windows NT Word: 10.0 Language: 2052
'^The Code CopyIn [ThisDocument-ThisDocument]^'
'* -----------------------------'注意:运行此过程(ExampleToSaveAs)前必须引用MSForms
'即VBE/工具/引用:Microsoft Forms 2.0 Object Library (C:\WINNT\system32\FM20.DLL)
Sub ExampleToSaveAs()
Dim MyData As DataObject, MyString As String
Dim MyText() As String, aString As Variant, FileName As String
If Selection.Type = wdSelectionIP Then Exit Sub '如果未选中则退出程序
Set MyData = New DataObject '引用新的DataOject
Selection.Copy '选定内容复制
MyData.GetFromClipboard '从剪贴板复制数据到 DataObject
MyString = MyData.GetText(1) '获得DataObject的无格式文本
MyText = VBA.Split(MyString, vbCrLf) '取得一个以VBCRLF为分隔符的数组
For Each aString In MyText '在该数组中循环
FileName = FileName & aString '取得文件名
Next
MyData.Clear '清空DataObject
On Error Resume Next '设置错误陷阱
ActiveDocument.SaveAs FileName '另存文件
'如果不能正确另存为,提示错误!
If Err.Number <> 0 Then Err.Clear: MsgBox "非法文件名!", vbOKOnly + vbCritical, "Warning"
End Sub
'----------------------
Sub Example()
On Error Resume Next '忽略错误
'如果未选定内容则退出
If Selection.Type = wdSelectionIP Then Exit Sub
Selection.Copy '复制
SendKeys "^v{Enter}", False '预置粘贴
Application.Run "FileSaveAs" '运行另存为命令
End Sub
'---------------------- |