2017-03-05 1 views
1

여러 단어 문서를 하나의 문서로 병합하여 각 문서의 서식을 유지하는 프로그램을 작성 중입니다. 웹에 대한 몇 가지 연구는 다음과 같다 작동하도록되어 버전을 쓴 후 :VB NET에서 형식을 유지하는 단어 문서 병합

Public Sub processmodulestest(ByVal id As Integer) 
    Dim oMissing = System.Reflection.Missing.Value 
    Dim oFalse = False 
    Dim oTrue = True 
    Dim fileDirectory = "C:\<file-path>\MOD-TEST\" 

    Dim wrdApp As New Word.Application 
    Dim destDoc As Word.Document 'destination doc 
    Dim docfile As Word.Document 'tmp doc to paste 
    destDoc = wrdApp.Documents.Add 

    'docNew.PageSetup.TopMargin = wrdApp.InchesToPoints(1.0F) 
    'docNew.PageSetup.BottomMargin = wrdApp.InchesToPoints(0.0F) 
    Dim wordFiles() As String = Directory.GetFiles(fileDirectory, "*.doc") 

    wrdApp.Options.Pagination = False 
    wrdApp.ActiveWindow.View.ShowAll = True 

    For Each el As String In wordFiles 
     docfile = wrdApp.Documents.Open(el, False, False) 
     wrdApp.Selection.WholeStory() 
     wrdApp.Selection.Copy() 
     wrdApp.ActiveWindow.Close(Word.WdSaveOptions.wdDoNotSaveChanges) 
     destDoc.Activate() 
     wrdApp.Selection.PasteAndFormat(Word.WdRecoveryType.wdFormatOriginalFormatting) 
     wrdApp.Selection.InsertBreak(Word.WdBreakType.wdPageBreak) 
    Next 

    wrdApp.Visible = True 
End Sub 

나는 다음과 같은 오류 얻을 :

An unhandled exception of type'System.Runtime.InteropServices.COMException' 
HRESULT: 0x80010108 (RPC_E_DISCONNECTED)) The object invoked has disconnected from its clients. 

다음 줄 참조 :

destDoc.Activate() 

코드가 끝난 Office 인스턴스에서 정규화되지 않은 메서드를 사용하기 때문에 코드를 수정하는 방법을 이해할 수 없기 때문에이 내용을 읽어야합니다.

+0

가이 코드를 보았다하지만 난 VB .NET으로 번역 할 수없는이기 때문에 어쩌면 그것의 그것을 – Slai

답변

1

VB.NET에서는이 작업을 수행하는 방법을 모르지만 아래의 VBA 코드는 모든 Word 문서를 하나의 통합 된 Word 문서로 병합합니다.

Sub Foo() 
Dim i As Long 
Dim MyName As String, MyPath As String 
Application.ScreenUpdating = False 
Documents.Add 

MyPath = "C:\Documents and Settings\Excel\Desktop\Word Files\" ' <= change this as necessary 

MyName = Dir$(MyPath & "*.doc") ' not *.* if you just want doc files 

Do While MyName <> "" 
If InStr(MyName, "~") = 0 Then 
Selection.InsertFile _ 
FileName:="""" & MyPath & MyName & """", _ 
ConfirmConversions:=False, Link:=False, _ 
Attachment:=False 
Selection.InsertBreak Type:=wdPageBreak 
End If 

MyName = Dir ' gets the next doc file in the directory 
Loop 

End Sub 
+0

붙여 넣기 전에 소스 문서를 닫지 마십시오 하나의 워드 문서에 여러 워드 문서 첨부하기, 하지만 작동하지 않습니다 –

+0

MS Word에서 실행! Word를 열고 Alt + F11을 누른 다음 열리는 창에 해당 코드를 붙여 넣으십시오. 물론 모든 Word 문서가있는 경로를 변경해야합니다. – ryguy72