שתף באמצעות


VB.net Word how to select the entire document to copy and paste into another document

Question

Saturday, July 6, 2019 4:19 AM

Ive inherited the following code and need to amend and add new code.

Once  the details are completed writing in the word document.

Select the whole document and copy

switch to a different word document, goto the end of the document and paste

then switch back to the original document and repeat (reiterate) until all documents are done.

Any ideas

TIA

' create new certificate summary
Dim newDoc As Microsoft.Office.Interop.Word.Document = objWordApp.Documents.Add(Application.StartupPath & "\Templates\Certificate Summary.docx")

' create new certificate 
Dim objDoc As Microsoft.Office.Interop.Word.Document = objWordApp.Documents.Add(Application.StartupPath & "\Templates\Certificate.docx")

objDoc = objWordApp.ActiveDocument
objDoc.Content.Find.Execute(FindText:="%NAME%", ReplaceWith:="" & dgImportedData.Item("dgName", i).Value.ToString.ToUpper & "", Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
objDoc.Content.Find.Execute(FindText:="%COURSENAME%", ReplaceWith:="" & dgImportedData.Item("dgCourseName", i).Value & "", Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
objDoc.Content.Find.Execute(FindText:="%ENDDATE%", ReplaceWith:="" & Format(CDate(dgImportedData.Item("dgCompletedDate", i).Value.ToString), "d MMMM yyyy") & "", Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
objDoc.Content.Find.Execute(FindText:="%INSTRUCTOR%", ReplaceWith:="" & objDTInstructor.Rows(Params(2)).Item("FullName") & " " & objDTInstructor.Rows(Params(2)).Item("RegisteredNo"), Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)

' goto  certificate summary doc at end
‘ and paste all certificate contents

All replies (1)

Monday, July 8, 2019 4:42 AM ✅Answered | 1 vote

Hi,

try the method:

  Private Shared Sub MsWordCopy()
        Dim App = New MsWord.Application()
        Dim documentFrom As MsWord.Document = Nothing
        Dim documentTo As MsWord.Document = Nothing
        Try
            Dim fileNameFrom = "D:\test.docx"
            App.Visible = True
            documentFrom = App.Documents.Open(fileNameFrom)
            Dim oRange As MsWord.Range = documentFrom.Content
            oRange.Copy()
            Dim fileNameTo = "D:\result.docx"
            documentTo = App.Documents.Add()
            documentTo.Content.PasteSpecial(DataType:=MsWord.WdPasteOptions.wdKeepSourceFormatting)
            documentTo.SaveAs(fileNameTo)
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            documentFrom.Close(False)
            documentTo.Close()
            App.Quit()
            Marshal.ReleaseComObject(app)
            Marshal.ReleaseComObject(documentFrom)
            Marshal.ReleaseComObject(documentTo)
        End Try
    End Sub

Best Regards,

Alex

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.