automating Word using VB.NET please help!! - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

automating Word using VB.NET please help!!

VB NewbieVB Newbie Posts: 206Member
I'm struggling with the following:

I'm using VB.NET and I want to make a program that put some text from a textbox at a certain place in a Word document.

So I used the following code to start Word with a new document and a bookmark were I want to have the text from the textbox:

***********************************************************************
Dim wordapp As Word.Application
Dim letter As Word.Document

wordapp = CType(CreateObject("Word.Application"),_
Word.Application)
brief = CType(wordapp.Documents.Add, Word.Document)
brief.Bookmarks.Add("mark1")
brief.Application.Visible = True
***********************************************************************
I have two questions:

1)First of all this code creates a NEW word document, BUT I want to use an existing Word document(with already some text and pictures in it).

2)I don't know how to put the text from the textbox at the place of the bookmark!

I'm very new to this, so I would appreciate it you can give my the correct code for this, not only some hints.

Hope someone can help me!

Thanks,

VB Newbie

Comments

  • fischwarefischware Posts: 2Member
    : I'm struggling with the following:
    :
    : I'm using VB.NET and I want to make a program that put some text from a textbox at a certain place in a Word document.
    :
    : So I used the following code to start Word with a new document and a bookmark were I want to have the text from the textbox:
    :
    : ***********************************************************************
    : Dim wordapp As Word.Application
    : Dim letter As Word.Document
    :
    : wordapp = CType(CreateObject("Word.Application"),_
    : Word.Application)
    : brief = CType(wordapp.Documents.Add, Word.Document)
    : brief.Bookmarks.Add("mark1")
    : brief.Application.Visible = True
    : ***********************************************************************
    : I have two questions:
    :
    : 1)First of all this code creates a NEW word document, BUT I want to use an existing Word document(with already some text and pictures in it).
    :
    : 2)I don't know how to put the text from the textbox at the place of the bookmark!
    :
    : I'm very new to this, so I would appreciate it you can give my the correct code for this, not only some hints.
    :
    : Hope someone can help me!
    :
    : Thanks,
    :
    : VB Newbie
    :
    :



  • fischwarefischware Posts: 2Member
    : : I'm struggling with the following:
    : :
    : : I'm using VB.NET and I want to make a program that put some text from a textbox at a certain place in a Word document.
    : :
    : : So I used the following code to start Word with a new document and a bookmark were I want to have the text from the textbox:
    : :
    : : ***********************************************************************
    : : Dim wordapp As Word.Application
    : : Dim letter As Word.Document
    : :
    : : wordapp = CType(CreateObject("Word.Application"),_
    : : Word.Application)
    : : brief = CType(wordapp.Documents.Add, Word.Document)
    : : brief.Bookmarks.Add("mark1")
    : : brief.Application.Visible = True
    : : ***********************************************************************
    : : I have two questions:
    : :
    : : 1)First of all this code creates a NEW word document, BUT I want to use an existing Word document(with already some text and pictures in it).
    : :
    : : 2)I don't know how to put the text from the textbox at the place of the bookmark!
    : :
    : : I'm very new to this, so I would appreciate it you can give my the correct code for this, not only some hints.
    : :
    : : Hope someone can help me!
    : :
    : : Thanks,
    : :
    : : VB Newbie
    : :
    : :
    :
    :
    :
    :
    This routine starts MS Word. If its already going, then it sets it up for a new object (Document) to be opened.
    _______________________________________________________________
    Public Sub GtWord()
    On Error Resume Next
    Dim wdApp As Word.Application
    ' MsgBox "Start MS Word and Click OK", vbOKOnly, "Start MS Word"
    Set wdApp = GetObject(, "Word.Application")
    If wdApp Is Nothing Then
    Set wdApp = CreateObject("Word.Application")
    If wdApp Is Nothing Then
    MsgBox "Error starting MS Word - Click OK", vbOKOnly, "Start MS Word"
    End If
    End If
    wdApp.Visible = True
    End Sub
    _______________________________________________________________

    This module processes an Access MDB and replaces strings in the Document with the data from the MDB. The big trick to using this is having a reference (Project, References). Choose the following 2 items to process word docs:
    1. Microsoft Word 9.0 Object Library
    2. Microsoft ActiveX Data Objects Recordset 2.6 Library

    _______________________________________________________________
    Public Sub Proc1( )
    Dim strFN1, strFN2, strFN3 As String
    MousePointer = vbHourglass
    Call GtWord
    Do While Not dtaTEXARK.Recordset.EOF
    txtInput.Text = txtInput.Text + 1
    Call TheNext
    For intX = 0 To 7
    With ActiveDocument.Content.Find
    ' With ActiveDocument.Content.Find
    .Text = strArray(intX)
    .Replacement.Text = strRep(intX)
    .Forward = 1
    .Wrap = wdFindStop
    .Execute Replace:=wdReplaceAll
    End With
    Next
    ' ActiveDocument.SaveAs Mid$(strFileName, 1, 9) _
    ' & "1099_ck" & strRep(7) & ".DOC"
    strFN1 = "D:TX_AR1099_ck" & strRep(7) & ".DOC"
    strFN2 = "D:TX_AR1099_ck" & strRep(7) & "a.DOC"
    strFN3 = "D:TX_AR1099_ck" & strRep(7) & "b.DOC"
    ' strFN1 doesn't exist, then go on
    If Dir(strFN1) = "" Then
    Else
    If Dir(strFN2) = "" Then ' strFN1 exist but strFN2 doesn't
    strFN1 = strFN2
    Else
    If Dir(strFN3) = "" Then ' strFN1 and strFN2 exist but strFN3 doesn't
    strFN1 = strFN3
    Else
    ' strFN1, strFN2, and strFN3 exist
    MsgBox "More than 3 Forms for SSN = " & strRep(7), vbOKOnly, "Multiple Forms"
    End If
    End If
    End If
    txtOutput.Text = txtOutput.Text + 1
    ActiveDocument.SaveAs (strFN1)
    ' ActiveDocument.PrintOut
    ActiveDocument.Close
    dtaTEXARK.Recordset.MoveNext
    Loop
    MousePointer = vbArrow
    dtaTEXARK.Recordset.Close
    End Sub
    _______________________________________________________________

    This module determines which Document to open (STPOLICE, JUDICIAL, PUBLIC Employees) and retrieves the Data from the MDB.
    _______________________________________________________________
    Public Sub TheNext()
    If dtaTEXARK.Recordset!Sys = "PER" Then
    strRep(1) = "Public Employees Retirement System"
    strRep(0) = "71-0385993"
    Else
    If dtaTEXARK.Recordset!Sys = "JUD" Then
    strRep(1) = "Judicial Retirement System "
    strRep(0) = "71-0578996"
    Else
    If dtaTEXARK.Recordset!Sys = "STP" Then
    strRep(1) = "State Police Retirement System "
    strRep(0) = "71-6060127"
    End If
    End If
    End If
    If Len(dtaTEXARK.Recordset!Name) < 65 Then
    intZ = 65 - Len(dtaTEXARK.Recordset!Name)
    strRep(2) = dtaTEXARK.Recordset!Name & Space(intZ)
    Else
    strRep(2) = dtaTEXARK.Recordset!Name
    End If
    If Len(dtaTEXARK.Recordset!Address) < 46 Then
    intZ = 46 - Len(dtaTEXARK.Recordset!Address)
    strRep(3) = dtaTEXARK.Recordset!Address & Space(intZ)
    Else
    strRep(3) = dtaTEXARK.Recordset!Address
    End If
    strRep(4) = dtaTEXARK.Recordset!City & Space(10)
    strRep(5) = dtaTEXARK.Recordset!Zip
    strRep(6) = Format(dtaTEXARK.Recordset!Gross, "###,##0.00")
    If Len(strRep(6)) < 10 Then
    intZ = 10 - Len(strRep(6))
    strRep(6) = Space(intZ) & strRep(6)
    End If
    strRep(7) = Mid$(dtaTEXARK.Recordset!SSN, 1, 3) & "-" & _
    Mid$(dtaTEXARK.Recordset!SSN, 4, 2) & "-" & _
    Mid$(dtaTEXARK.Recordset!SSN, 6, 4)
    If dtaTEXARK.Recordset!State = "AR" Then
    strFileName = Mid$(strFileName, 1, 19) & "AR.DOC"
    Else
    strFileName = Mid$(strFileName, 1, 19) & "TX.DOC"
    End If
    If Dir(strFileName) <> "" Then
    Else
    MsgBox "doesn't exist", vbOKOnly, "File Check Status"
    Exit Sub
    End If
    Documents.Open strFileName
    End Sub
    ======================================================================
    The Documents.Open is the syntax to open the Document. I open a standard DOC (Texarkana_AR.DOC), replace the strings, then do a save as, Print the DOC (mine is commented out in this code), close the DOC.

    Hope this helps.

    Bob Fischer
    DP Manager, APERS




Sign In or Register to comment.