编程之路

——火地晋

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  291 随笔 :: 2 文章 :: 297 评论 :: 134万 阅读

http://zhidao.baidu.com/question/143381141.html

打开要处理的WORD文档,然后ALT+F8调出宏处理窗口,新输入宏名称为MyPg。
点击右侧的编辑按钮,输入如下内容,完成后键盘F5即可。

但完成后最后2个文件也会会有问题,手动修改一下就行了。


Sub MyPg()

    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Dim oNewDoc As Word.Document
    Dim oRange As Word.Range
    Dim lCurrentStart As Long
    Dim lCurrentEnd As Long
    Dim lDocumentEnd As Long
    Dim lOutputCount As Long
   
    lOutputCount = 0
   
    Set oWord = GetObject(, "Word.Application")
    Set oDoc = ActiveDocument
   
    oDoc.Select
    lCurrentStart = oWord.Selection.Start
    lCurrentEnd = lCurrentStart
    lDocumentEnd = oWord.Selection.End
   
    oWord.Selection.Collapse wdCollapseStart
   
    Do While (lCurrentEnd < lDocumentEnd)
        oWord.Browser.Target = wdBrowsePage
        oWord.Browser.Next
        oWord.Browser.Next
        oWord.Browser.Next
        oWord.Browser.Next
        lCurrentEnd = oWord.Selection.End
       
        If (lCurrentStart = lCurrentEnd) Then
            lCurrentEnd = lDocumentEnd
        End If
       
        Set oRange = oDoc.Range(lCurrentStart, lCurrentEnd)
       
        Set oNewDoc = oWord.Documents.Add
        oRange.Copy
        oNewDoc.Range(0, 0).Paste
       
        lOutputCount = lOutputCount + 1
        oNewDoc.SaveAs FileName:="d:\" & lOutputCount & ".doc"
        oNewDoc.Close
       
        lCurrentStart = lCurrentEnd
    Loop


End Sub

posted on   火地晋  阅读(5099)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示