注册 登录
编程论坛 VB6论坛

vb6合并office2007中的word文档

二中仙神 发布于 2015-07-17 13:29, 1437 次点击
C:\abc\中有1.docx,2.docx,3.docx.....30.docx如何把它们合并到一个新的文档中?
6 回复
#2
lianyicq2015-07-17 14:44
在VB6中引用WORD没做过几次。
试试这个,两个文件合并为新文件。
程序代码:
Dim wApp As Word.Application

 Dim wDoc As Word.Document
Private Sub Command1_Click()
  Merge "c:\1.docx", "c:\2.docx", wApp
  Form1.Caption = "OK"
End Sub

Private Sub Form_Load()
  Set wApp = New Word.Application
End Sub


 Function Merge(ByVal sFirstFile As String, ByVal sSecondFile As String, ByRef wrdapp As Word.Application) As Boolean
    Dim docNew As Word.Document
   
    'open the main document
    Set docNew = wrdapp.Documents.Open(sSecondFile)
   
    'insert the coversheet
    wrdapp.Selection.InsertFile FileName:=sFirstFile, Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False
   
    'set position to first page/coversheet
    wrdapp.Selection.MoveUp Unit:=wdScreen, Count:=1
   
    'force header/footer to begin on 2nd page
    With wrdapp.Selection.PageSetup
        .DifferentFirstPageHeaderFooter = True
    End With
   
    'save the document
    docNew.SaveAs "C:\NewFile.docx"
End Function


 
#3
二中仙神2015-07-18 12:08
我可能没有说清楚:win7系统,office2007.如果是XP系统及office2003能够很好解决。
Dim wApp As Word.Application
Dim wDoc As Word.Document
运行后提示这两名错误
#4
lianyicq2015-07-20 09:47
回复 3楼 二中仙神
在工程中引用Microsoft Word *.* Object Library
我是在XP下的Word2007测试,Win7自己试.
#5
二中仙神2015-07-22 15:34
先谢谢您的指导,不过引用了Microsoft Word *.* Object Library,还是不行。另外找到一个拆分的
结果每页都是空白。如下:

Private Sub Form_Load()
Dim w As Object, d As Object, s As Object, page As Object, i As Integer
Set w = CreateObject("WORD.application")
w.Visible = True
Set d = w.Documents.Open("C:\d.docx")
'文档名相应改为你的文档名
For Each page In d.ActiveWindow.ActivePane.pages
i = i + 1

d.Range.Copy
'复制全部,不是每一页
'我想把每一页的内容作为一个新文件保存

Set s = w.Documents.Add
s.Content = page.Rectangles.Item(1).Range
s.Range(0, 0).Paste
s.SaveAs "C:\d" & i & "t.docx"
'文档名相应改为你的文档名
s.Close
Set s = Nothing

Next

Set w = Nothing
d.Close
Set d = Nothing
MsgBox "完毕"
End
#6
二中仙神2015-07-23 06:46
您好,经过测试应该这样写:

Dim wApp As Object
'Dim wDoc As Word.Document
 Private Sub Command1_Click()
   Merge "c:\1.docx", "c:\2.docx", wApp
   Form1.Caption = "OK"
 End Sub
 
Private Sub Form_Load()
   Set wApp = New Word.Application
 End Sub
 
Function Merge(ByVal sFirstFile As String, ByVal sSecondFile As String, ByRef wrdapp As Word.Application) As Boolean
     Dim docNew As Word.Document
   
     'open the main document
     Set docNew = wrdapp.Documents.Open(sSecondFile)
   
     'insert the coversheet
     wrdapp.Selection.InsertFile FileName:=sFirstFile, Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False
   
     'set position to first page/coversheet
     wrdapp.Selection.MoveUp Unit:=wdScreen, Count:=1
   
     'force header/footer to begin on 2nd page
     With wrdapp.Selection.PageSetup
         .DifferentFirstPageHeaderFooter = True
     End With
   
     'save the document
     docNew.SaveAs "C:\NewFile.docx"
    '''''''
     docNew.close
 End Function

 
#7
lianyicq2015-07-23 08:55
回复 6楼 二中仙神
在WIN7下就是改了Dim wApp As Object?感觉这不是XP和WIN7的区别。
1