Foxtable(狐表)用户栏目专家坐堂 → [求助]合并Word


  共有3452人关注过本帖树形打印复制链接

主题:[求助]合并Word

帅哥哟,离线,有人找我吗?
天一生水
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1141 积分:11272 威望:0 精华:0 注册:2017/9/26 16:30:00
[求助]合并Word  发帖心情 Post By:2019/10/20 21:12:00 [只看该作者]

蓝老师好!
我把选中的多个Word文件路径放入目录树中,然后新建一个Word文档接受合并的文件,在合并时,每一个文件后面插入分页符进行分隔。
在运行中提示第一个文件被占用,还有一些其他的问题,请老师帮助看看是哪里的问题?
谢谢!


图片点击可在新窗口打开查看此主题相关图片如下:截屏图片.jpg
图片点击可在新窗口打开查看

Dim wapp1 As New MSWord.Application
Dim wapp2 As New MSWord.Application
try
    '新建一个Word接收文档
    Dim app As New MSWord.Application
    Dim missing = System.Reflection.Missing.Value
    Dim nDoc = App.Documents.Add(missing, missing, missing, missing)
    Dim nm As String = Format(Date.Now, "yyyyMMddhhmmss")
    nDoc.SaveAs(ProjectPath & "Word合并\" & nm & ".doc")
    app.quit
    'msgbox("新建word成功")    
    Dim doc1 = wapp1.Documents.Open(ProjectPath & "Word合并\" & nm & ".doc")

    Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")    '    
    Dim nn As WinForm.TreeNode
    Dim ere As Integer = tr.Nodes.Count - 1
    Dim bb As Integer
    For bb = 0 To ere
        nn = tr.Nodes(bb)
        
        Dim doc2 = wapp2.Documents.Open(nn.Text)
        wapp2.ActiveWindow.Selection.WholeStory
        wapp2.ActiveWindow.Selection.copy
        wapp1.ActiveWindow.Selection.WholeStory
        wapp1.ActiveWindow.Selection.MoveRight(Unit:=1, Count:=1)
        '在文件后插入分页符
        If bb > 1 Then         
            Dim pBreak = MSWord.WdBreakType.wdSectionBreakNextPage
            wapp1.ActiveWindow.Selection.InsertBreak(pBreak)
        End If
        
        wapp1.ActiveWindow.Selection.paste        
    Next
    wapp2.Quit
    wapp1.Visible = True
catch ex As exception
    msgbox(ex.message)
    wapp1.Quit
    wapp2.Quit
finally
End try


 回到顶部
帅哥,在线噢!
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110649 积分:563159 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2019/10/20 21:19:00 [只看该作者]

Dim wapp1 As New MSWord.Application
Dim wapp2 As New MSWord.Application
try
    '新建一个Word接收文档
    Dim missing = System.Reflection.Missing.Value
    Dim doc1 = wapp1.Documents.Add(missing, missing, missing, missing)

    Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")    '    
    Dim nn As WinForm.TreeNode
    Dim ere As Integer = tr.Nodes.Count - 1
    Dim bb As Integer
    For bb = 0 To ere
        nn = tr.Nodes(bb)
        
        Dim doc2 = wapp2.Documents.Open(nn.Text)
        wapp2.ActiveWindow.Selection.WholeStory
        wapp2.ActiveWindow.Selection.copy
        wapp1.ActiveWindow.Selection.WholeStory
        wapp1.ActiveWindow.Selection.MoveRight(Unit:=1, Count:=1)
        '在文件后插入分页符
        If bb > 1 Then         
            Dim pBreak = MSWord.WdBreakType.wdSectionBreakNextPage
            wapp1.ActiveWindow.Selection.InsertBreak(pBreak)
        End If
        
        wapp1.ActiveWindow.Selection.paste        
    Next
    wapp2.Quit

    Dim nm As String = Format(Date.Now, "yyyyMMddhhmmss")
    doc1.SaveAs(ProjectPath & "Word合并\" & nm & ".doc")
    wapp1.Visible = True
catch ex As exception
    msgbox(ex.message)

finally
    wapp1.Quit
    wapp2.Quit
End try

 回到顶部
帅哥哟,离线,有人找我吗?
天一生水
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1141 积分:11272 威望:0 精华:0 注册:2017/9/26 16:30:00
  发帖心情 Post By:2019/10/20 21:39:00 [只看该作者]

谢谢老师!
提示“命令失败”

 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:合并word测试.rar



 回到顶部
帅哥哟,离线,有人找我吗?
天一生水
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1141 积分:11272 威望:0 精华:0 注册:2017/9/26 16:30:00
  发帖心情 Post By:2019/10/21 11:25:00 [只看该作者]

麻烦蓝老师看看是什么原因?

谢谢!


 回到顶部
帅哥,在线噢!
有点蓝
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110649 积分:563159 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2019/10/21 13:41:00 [只看该作者]

Dim wapp1 As New MSWord.Application
Dim wapp2 As New MSWord.Application
try
    '新建一个Word接收文档
    Dim missing = System.Reflection.Missing.Value
    Dim doc1 = wapp1.Documents.Add(missing, missing, missing, missing)

    Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")    '    
    Dim nn As WinForm.TreeNode
    Dim ere As Integer = tr.Nodes.Count - 1
    Dim bb As Integer
    For bb = 0 To ere
        nn = tr.Nodes(bb)
        wapp1.ActiveWindow.Selection.WholeStory
        wapp1.ActiveWindow.Selection.MoveRight(Unit:=1, Count:=1)
        '在文件后插入分页符
        If bb > 1 Then         
            Dim pBreak = MSWord.WdBreakType.wdSectionBreakNextPage
            wapp1.ActiveWindow.Selection.InsertBreak(pBreak)
        End If
                
        Dim doc2 = wapp2.Documents.Open(nn.Text)
        wapp2.ActiveWindow.Selection.WholeStory
        wapp2.ActiveWindow.Selection.copy

        wapp1.ActiveWindow.Selection.paste        
    Next
    Dim nm As String = Format(Date.Now, "yyyyMMddhhmmss")
    doc1.SaveAs(ProjectPath & "Word合并\" & nm & ".doc")
    wapp1.Visible = True
catch ex As exception
    msgbox(ex.message)
finally
    wapp2.Quit
End try

 回到顶部
帅哥哟,离线,有人找我吗?
天一生水
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1141 积分:11272 威望:0 精华:0 注册:2017/9/26 16:30:00
  发帖心情 Post By:2019/10/21 16:49:00 [只看该作者]

可以了。谢谢蓝老师!

 

我想增加一种功能,就是把Word、execl文件混合添加到目录树,然后按照目录树中的排序合并到一个Word文档中,每个文件后面添加分页符。

里面的关系有点分辨不清,请蓝老师帮助指导。谢谢!

 

Dim wapp1 As New MSWord.Application
Dim wapp2 As New MSWord.Application
Dim wapp3 As New MSExcel.Application        ’execl文档
try
    '新建一个Word接收文档
    Dim missing = System.Reflection.Missing.Value
    Dim doc1 = wapp1.Documents.Add(missing, missing, missing, missing)   
   
    Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")    '
    Dim nn As WinForm.TreeNode
    Dim ere As Integer = tr.Nodes.Count - 1
    Dim bb As Integer
    For bb = 0 To ere
        nn = tr.Nodes(bb)
       
        If FileSys.GetName(nn.Text).split(".")(0) = "doc" Then    '如果是doc文件
            wapp1.ActiveWindow.Selection.WholeStory
            wapp1.ActiveWindow.Selection.MoveRight(Unit:=1, Count:=1)
            '在文件后插入分页符
            If bb > 0 Then
                Dim pBreak = MSWord.WdBreakType.wdSectionBreakNextPage
                wapp1.ActiveWindow.Selection.InsertBreak(pBreak)
            End If
           
            Dim doc2 = wapp2.Documents.Open(nn.Text)
            wapp2.ActiveWindow.Selection.WholeStory
            wapp2.ActiveWindow.Selection.copy
           
            wapp1.ActiveWindow.Selection.paste
        End If

        If FileSys.GetName(nn.Text).split(".")(0) = "xls" Then    '如果是xls文件
            Dim Wb As MSExcel.WorkBook = App2.WorkBooks.Open(nn.Text)
            Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1) '指定要复制的工作表
            Ws.UsedRange.Copy
            wb.saved = True
            wapp3.DisplayAlerts = False
           
            wapp.ActiveWindow.Selection.WholeStory
            wapp.Selection.MoveRight(Unit:=1)
            wapp.Selection.TypeText(Text:=vbcrlf)
            wapp.ActiveWindow.Selection.paste
           
            wapp3.Quit            
            .........


        End If       
    Next
    Dim nm As String = Format(Date.Now, "yyyyMMddhhmmss")
    doc1.SaveAs(ProjectPath & "Word合并\" & nm & ".doc")
    wapp1.Visible = True
catch ex As exception
    msgbox(ex.message)
finally
    wapp2.Quit
End try

 


 回到顶部
帅哥,在线噢!
有点蓝
  7楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110649 积分:563159 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2019/10/21 17:11:00 [只看该作者]

For bb = 0 To ere
        nn = tr.Nodes(bb)
      
            wapp1.ActiveWindow.Selection.WholeStory

            wapp1.ActiveWindow.Selection.MoveRight(Unit:=1, Count:=1)
            '在文件后插入分页符
            If bb > 0 Then
                Dim pBreak = MSWord.WdBreakType.wdSectionBreakNextPage
                wapp1.ActiveWindow.Selection.InsertBreak(pBreak)

            End If  
        If FileSys.GetName(nn.Text).split(".")(0) = "doc" Then    '如果是doc文件

            Dim doc2 = wapp2.Documents.Open(nn.Text)
            wapp2.ActiveWindow.Selection.WholeStory
            wapp2.ActiveWindow.Selection.copy
            
            wapp1.ActiveWindow.Selection.paste
        End If

If FileSys.GetName(nn.Text).split(".")(0) = "xls" Then    '如果是xls文件
            Dim Wb As MSExcel.WorkBook = wapp3 .WorkBooks.Open(nn.Text)
            Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1) '指定要复制的工作表
            Ws.UsedRange.Copy
            wb.saved = True
            wapp3.DisplayAlerts = False
            wapp1.ActiveWindow.Selection.paste       

        End If        


……

finally
    wapp2.Quit

wapp3.Quit


 回到顶部
帅哥哟,离线,有人找我吗?
天一生水
  8楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1141 积分:11272 威望:0 精华:0 注册:2017/9/26 16:30:00
  发帖心情 Post By:2019/10/21 19:08:00 [只看该作者]

提示如下,请老师指教。
谢谢!

图片点击可在新窗口打开查看此主题相关图片如下:截屏图片 (1).jpg
图片点击可在新窗口打开查看

 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:合并word文档测试.rar


代码如下:
Dim wapp1 As New MSWord.Application
Dim wapp2 As New MSWord.Application
Dim wapp3 As New MSExcel.Application
try
    '新建一个Word接收文档
    Dim missing = System.Reflection.Missing.Value
    Dim doc1 = wapp1.Documents.Add(missing, missing, missing, missing)
    
    Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")    '
    Dim nn As WinForm.TreeNode
    Dim ere As Integer = tr.Nodes.Count - 1
    Dim bb As Integer
    For bb = 0 To ere
        nn = tr.Nodes(bb)
        wapp1.ActiveWindow.Selection.WholeStory
        wapp1.ActiveWindow.Selection.MoveRight(Unit:=1, Count:=1)
        
        '在文件后插入分页符
        If bb > 0 Then
            Dim pBreak = MSWord.WdBreakType.wdSectionBreakNextPage
            wapp1.ActiveWindow.Selection.InsertBreak(pBreak)
        End If
        
        If FileSys.GetName(nn.Text).split(".")(0) = "doc" Or FileSys.GetName(nn.Text).split(".")(0) = "docx" Then    '如果是doc文件
            Dim doc2 = wapp2.Documents.Open(nn.Text)
            wapp2.ActiveWindow.Selection.WholeStory
            wapp2.ActiveWindow.Selection.copy
            wapp1.ActiveWindow.Selection.paste
        End If
        
        If FileSys.GetName(nn.Text).split(".")(0) = "xls" Or FileSys.GetName(nn.Text).split(".")(0) = "xlsx" Then    '如果是xls文件
            Dim Wb As MSExcel.WorkBook = wapp3 .WorkBooks.Open(nn.Text)
            Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1) '指定要复制的工作表
            Ws.UsedRange.Copy
            wb.saved = True
            wapp3.DisplayAlerts = False
            wapp1.ActiveWindow.Selection.paste
        End If
    Next
    
    Dim nm As String = Format(Date.Now, "yyyyMMddhhmmss")
    doc1.SaveAs(ProjectPath & "Word合并\" & nm & ".docx")
    wapp1.Visible = True
catch ex As exception
    msgbox(ex.message)
finally
    wapp2.Quit
    wapp3.Quit
End try

 回到顶部
帅哥,在线噢!
有点蓝
  9楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110649 积分:563159 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2019/10/21 20:44:00 [只看该作者]

试试

If FileSys.GetName(nn.Text).split(".")(0) = "xls" Or FileSys.GetName(nn.Text).split(".")(0) = "xlsx" Then    '如果是xls文件
            Dim Wb As MSExcel.WorkBook = wapp3 .WorkBooks.Open(nn.Text)
            Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1) '指定要复制的工作表
            Ws.UsedRange.Copy
wapp1.ActiveWindow.Selection.TypeParagraph
            wapp1.ActiveWindow.Selection.paste
        End If

 回到顶部
帅哥哟,离线,有人找我吗?
天一生水
  10楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1141 积分:11272 威望:0 精华:0 注册:2017/9/26 16:30:00
  发帖心情 Post By:2019/10/21 20:59:00 [只看该作者]

还是一样的提示

 回到顶部
总数 14 1 2 下一页