Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 5th, 2003, 07:23 PM
Registered User
 
Join Date: Jul 2003
Location: Cartersville, GA, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default putting data into a template

Hey,
New poster. Just learning VB6. I want to create a form that will take
inserted data from the user, easy to do with a form and some controls. What I want to do from there is place it onto multiple templates I have created and print them, then delete the new docs. I have not been able to figure out how to insert data into a particular area of a word document from a user interface.
Any Suggestions

Reply With Quote
  #2 (permalink)  
Old September 8th, 2003, 08:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: McPherson, Kansas, USA.
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In the intrest of time I am going to insert a function I use to extract data from a SQL server database and insert it to a MS Word document unmodified. That means you will have to take from the function what you need I can make myself available to answer some of your questions.

Some things you will need to do:

1. Create a reference to MS Word in your VB application.
2. Create a word document and insert bookmarks where you want your data to placed.

Here is the function:

Public Function SaveAsDOC(ByVal WorkOrder As String, Optional ByVal DestinationDir As String = "PrintOnly", _
Optional ByVal PrintDoc As Boolean = False, Optional ByVal AsRelease As Boolean = False) As Long

    Dim strLastChar As String
    Dim lError As Long
    Dim strError As String
    Dim rs As ADODB.Recordset
    Dim lx As Long
    Dim oWord As Word.Application
    Dim oWordDoc As Word.Document
    Dim oRng As Word.Range
    Dim oTable As Word.Table
    Dim oBookMark As Word.Bookmark

    On Error GoTo ErrorHandler

    ' Create new object instances
    Set oWord = New Word.Application
    Set oWordDoc = oWord.Documents.Add(GetVaultPath & "\Forms\DIS.doc")
    ' See if the connection is already established
    CheckGlobalDBConnections
    ' Define sproc
    With gCmd
        .CommandText = "bsp_GetDIS"
        .CommandType = adCmdStoredProc
        ' Set params
        .Parameters("@Workorder") = Trim(WorkOrder)
        ' Execute sproc
        Set rs = .Execute()
    End With
    ' Test for records
    If rs.BOF And rs.EOF Then
        ' There are no records
        ' Leave gracefully
        rs.Close
        Set rs = Nothing
        ' Raise error
        Err.Raise 1 + vbObjectError, "SaveAsDOC", "No records found for Work Order " & Trim(WorkOrder)
    End If
    ' Move to the first record in the recordset
    rs.MoveFirst
    ' Build the word document
    Set oRng = oWordDoc.Bookmarks("CustomerName").Range
    oRng.InsertAfter rs!CustomerName
    Set oRng = oWordDoc.Bookmarks("WorkOrder").Range
    oRng.InsertAfter rs!WorkOrder
    Set oRng = oWordDoc.Bookmarks("ReleasedBy").Range
    oRng.InsertAfter rs!FName & " " & rs!LName
    Set oRng = oWordDoc.Bookmarks("ReleaseDate").Range
    oRng.InsertAfter IIf(AsRelease = True, Format(Now(), "mmmm dd, yyyy"), "Pre-release copy")
    Set oRng = oWordDoc.Bookmarks("PreparedBy").Range
    oRng.InsertAfter rs!PreparedBy
    Set oRng = oWordDoc.Bookmarks("PrintedOn").Range
    oRng.InsertAfter Format(Now(), "mmmm dd, yyyy")
    Set oRng = oWordDoc.Bookmarks("Spacer").Range
    oRng.InsertAfter " "
    ' Create a table and add the data to the cells
    ' Add one to the number of rows to account for the header info
    Set oTable = oWordDoc.Tables.Add(oRng, rs.RecordCount + 1, 2)
    Set oRng = oWordDoc.Range
    oRng.Collapse wdCollapseEnd
    oTable.Columns.AutoFit ' Set auto fit to true
    ' Record the header info
    Set oRng = oTable.Cell(1, 1).Range
    oRng.Font.Bold = True
    oRng.Text = "Drawing File"
    Set oRng = oTable.Cell(1, 2).Range
    oRng.Font.Bold = True
    oRng.Text = "Description"
    ' Loop through each record
    ' Start at two because we already have entered the
    ' header information and want to start on the
    ' second row
    For lx = 2 To rs.RecordCount + 1
        ' Set word wrap to true
        oTable.Cell(lx, 1).WordWrap = True
        Set oRng = oTable.Cell(lx, 1).Range
        oRng.Text = rs!FullDocumentName
        ' Set word wrap to true
        oTable.Cell(lx, 2).WordWrap = True
        Set oRng = oTable.Cell(lx, 2).Range
        oRng.Text = rs!Description
        ' Advance to the next record
        rs.MoveNext
    Next lx
    ' Test to see if we are printing the document, call print preview
    If PrintDoc = True Then
        ' Print the document, set the background to false
        ' The print operation will run synchronusly
        oWordDoc.PrintOut False
    Else
        ' Test for a destination directory
        If DestinationDir = "PrintOnly" Then
            ' Error
            Err.Raise 1 + vbObjectError, "SaveAsDoc", "No destionation directory was entered."
        End If
        ' Test for a backlslash "\" at the end of the destination dir
        strLastChar = Mid(DestinationDir, Len(DestinationDir), Len(DestinationDir))
        If strLastChar <> "\" Then
            ' Not found append backslash
            DestinationDir = DestinationDir & "\"
        End If
        ' Build the drawing issue sheet file name according to the naming convention
        ' DIS & WorkOrder & .doc
        ' Save the document to the destination directory
        ' Set the read only property to false
        oWordDoc.SaveAs DestinationDir & "DIS" & WorkOrder & ".doc", , , , , , False
    End If
    ' Close the document
    oWordDoc.Close 0 ' Do not prompt for save dialog, already saved unless printing
    ' Quit word
    oWord.Quit
    ' Release objects
    rs.Close
    Set rs = Nothing
    Set oWord = Nothing
    Set oWordDoc = Nothing
    Set oRng = Nothing
    Set oTable = Nothing
    ' Return 0
    SaveAsDOC = 0

    Exit Function

ErrorHandler:

    ' Capture error
    lError = Err.Number
    strError = Err.Description
    On Error Resume Next
    ' Ensure the word app has quit
    oWord.Quit
    ' Release objects
    If Not rs Is Nothing Then
        rs.Close
        Set rs = Nothing
        Set oWord = Nothing
        Set oWordDoc = Nothing
        Set oRng = Nothing
        Set oTable = Nothing
    End If
    ' Return error
    SaveAsDOC = lError
    ' Log error only
    ClassError lError, strError, "SaveAsDOC"

End Function


Larry Asher
Reply With Quote
  #3 (permalink)  
Old September 10th, 2003, 01:31 AM
Registered User
 
Join Date: Jul 2003
Location: Cartersville, GA, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Larry,

Thank You for your reply. It helped alot. I was able to successfully create the program I was trying to create, so far.
This has been a great learning experience. I will probably have to reuse this code later because I am working on this project in bits and pieces. I found also an article on msdn that teaches you how to utilize macros to get code to be converted easily into vb once the msword library is loaded.
Do you know how to clear multiple text boxes. I figure the command for one is an empty string?
Thank You
Aaron

Reply With Quote
  #4 (permalink)  
Old September 10th, 2003, 08:28 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: McPherson, Kansas, USA.
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am glad to be able to assist you. I know the "pains" of learning currently I am beginning an adventure in Visual C++, it is a humbling experience.

If you want to clear multiple text boxes there may be several option available to you. If you used a control array you can simply use a For Next loop.

Dim intX as Integer

' This is one way
For intX = 1 to 4
   txtTextBox(intX).Text = ""
Next intX

' This is more dynamic
For intX = txtTextBox.LBound to txtText.UBound
     txtTextBox(intX).Text = ""
Next intX

If you have not created a control array you could also use a collection object with a For Each loop.

' Assume your form is named frmTest
Dim c As Control

For Each c In frmTest
     If TypeName(c) = "TextBox" Then
          c.Text = ""
     End
Next c

Or simply resort to the clearing the text boxes individually. If you find that you are clearing the same text boxes over and over again consider a Control Array, you can read up on it in the help files.
Another option would be to create a procedure that clears your text boxes. I use functions like this to initialize a form and reset it properties.

Private Sub ClearTextBoxes()

     txtTextBox1.Text = ""
     txtTextBox2.Text = ""
     txtTextBox3.Text = ""
     txtTextBox4.Text = ""
     ' Etc.

End Sub

Best of luck.

Larry Asher
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Access data to Excel Template Hamerw Access VBA 1 November 22nd, 2006 12:38 AM
Putting dynamic data into columns rashley XSLT 2 December 21st, 2005 12:03 PM
Next Record, Putting Data in Table....Please Help! stacy Beginning PHP 2 July 28th, 2005 04:32 PM
Trouble putting data into Excel Drop Down List Arsi C# 2 October 26th, 2004 11:47 AM
putting xml data in a html <table> rev XSLT 4 September 10th, 2003 08:49 AM



All times are GMT -4. The time now is 01:18 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.