2017-03-05 12 views
1

私はいくつかの単語文書を1つの文書にまとめて、各文書の書式を維持しておくプログラムを書いています。ウェブ上でいくつかの研究は、以下で動作するようになっているバージョンを、書いた後:VB NETでフォーマットを保持しているWord文書をマージする

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文書を1つの統合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

を貼り付ける前に、元の文書を閉じていない、単一のWord文書に複数のWord文書を追加は、それは動作しません –

+0

MS Wordで実行! Wordを開き、Alt + F11キーを押して、開いているウィンドウにそのコードを貼り付けます。もちろん、すべてのWord文書が置かれているパスを変更する必要があります... – ryguy72

関連する問題