We help IT Professionals succeed at work.
Get Started

Inserting another document into Word doc with vb.net

Petermcg001 asked
Last Modified: 2015-07-09

I am creating Word documents via Vb.net, adding the name and address details from a contact database automatically.  I now need to be able to insert another document.  My existing code is:-

  Dim oWord As Object

        Dim oDoc As Object
        Dim oPara1 As Object

        Dim objDoc As Object
        Dim objDocs As Object
        Dim range As Object

        oWord = CreateObject("Word.Application")
        objDocs = oWord.Documents
        '  oDoc = objDocs.Add("c:\data\mytemplate.doct")
        oDoc = objDocs.Add

        oDoc.Paragraphs.SpaceAfter = 0

        oDoc.Paragraphs.SpaceBefore = 0

        oWord.Visible = True

        oPara1 = oDoc.Content.Paragraphs.Add
        '  oPara1.Style = oDoc.Styles("No Spacing")
        oPara1.Range.Text = Trim(cmbTitle.Text) & " " & txtForename.Text.Substring(0, 1) & " " & txtSurname.Text & vbCrLf & Trim(txtContactAddress.Text) & vbCrLf & Trim(cmbContactTown.Text) & vbCr & Trim(cmbContactCounty.Text) & " " & Group.FGPostcode & vbCrLf

        '    oPara1.Range.Font.Name = "Calibri"
        oPara1.Range.Font.Bold = False
        '   oPara1.Format.SpaceAfter = 0

        oPara1 = oDoc.Content.Paragraphs.Add
        oPara1.Range.Text = "Dear " & txtSalutation.Text & vbCrLf
        '    oPara1.Range.Font.Name = "Calibri"
        oPara1.Range.Font.Bold = False




However if I use this all the text in the document is replaced by the contents of "C:\data\myadd.doc" rather than the text being added at the end of the name and address details.

Watch Question
Top Expert 2016
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE