Link to home
Start Free TrialLog in
Avatar of tvtech
tvtechFlag for Australia

asked on

Populate Doc file with data from VB

Hi,

  I need to open an established Word Doc and fill various fields with data from VB. The Doc file doesn't have any variables setup for each text field that I need to push data to. Can someone let me know how I can make this happen, please.

Thanks.

TJ
Avatar of [ fanpages ]
[ fanpages ]

Hi TJ,

This code should get you going along the "write" (sorry) path...

' Start of Code...

Option Explicit
Public Sub Test()

  Dim objWord_Application                               As Object

  Set objWord_Application = CreateObject("Word.Application")

' Un-comment the next line to see the Word document creation on-screen, otherwise, to hide it, leave the next line alone...
' objWord_Application.Visible = True

  objWord_Application.Documents.Add
 
  objWord_Application.Selection.TypeText Text:="Hello World"
                                                         
  objWord_Application.ActiveDocument.SaveAs "c:\new_word.doc"
  objWord_Application.ActiveDocument.Close
  objWord_Application.Quit
 
  Set objWord_Application = Nothing
 
End Sub

' ...End of Code

BFN,

fp.
Avatar of tvtech

ASKER

Hi fanpages,

  You're on the 'write' track. :-)

  I've pulled the text variables from a web site with VB in order to populate the fields in a preset Doc template file. How do I go about opening my Doc and put my text vars in the correct fields?

  For example:

     Name: <Name_Var_To_Insert_From_VB>
     Address: <Address_Var_To_Insert_From_VB>
     Ph: <Phone_Var_To_Insert_From_VB>
     etc, etc ...

  Thanks.

TJ
Hi TJ,

Word! :)

Are you asking what is the code to find the first occurrence of "Name:" & then insert a pre-defined variable after it, and similarly for the other field "titles"?

BFN,

fp.
Avatar of tvtech

ASKER

Hi fp,

  Yep! You got it in one! I can't find a way to insert my vars into the correct fields within a Custs Doc file. :-( There's gotta be an easier way to make a livin'.

Thanks.

TJ
Hi again,

Could you make your customer's document available for download, so I can be sure of the formatting?

You can remove any sensitive information or replace it with X's, if you wish.

BFN,

fp.
Avatar of tvtech

ASKER

Hi fp,

  No problem. The DOC is at www.qldemail.com/ee/planningrequest.doc

Thank you.

TJ
Sorry... couldn't see the 'Phone Number, but I'm sure you can work it out from here...

Public Sub Test_Update()

  Dim objWord_Application                               As Object
  Dim strAddress                                        As String
  Dim strName                                           As String
 
  strAddress = "<Address_Var_To_Insert_From_VB>"
  strName = "<Name_Var_To_Insert_From_VB>"

  Set objWord_Application = CreateObject("Word.Application")

' Un-comment the next line to see the Word document creation on-screen, otherwise, to hide it, leave the next line alone...
' objWord_Application.Visible = True

  objWord_Application.Documents.Open ("http://www.qldemail.com/ee/planningrequest.doc")
 
  objWord_Application.Selection.Find.ClearFormatting
  objWord_Application.Selection.Find.Text = "Name/Description:"
  objWord_Application.Selection.Find.Execute
  objWord_Application.Selection.MoveRight Unit:=12&              ' wdCell
  objWord_Application.Selection.TypeText Text:=strName
 
  objWord_Application.Selection.Find.ClearFormatting
  objWord_Application.Selection.Find.Text = "Address:"
  objWord_Application.Selection.Find.Execute
  objWord_Application.Selection.MoveRight Unit:=12&              ' wdCell
  objWord_Application.Selection.TypeText Text:=strAddress
 
  objWord_Application.ActiveDocument.SaveAs "c:\new_word.doc"
  objWord_Application.ActiveDocument.Close
  objWord_Application.Quit
 
  Set objWord_Application = Nothing

End Sub


BFN,

fp.
Avatar of tvtech

ASKER

fp, you are a legend!!!! Thank you very much.

  Just one minor thing ... the vars I am sending to the Doc are not appearing in the correct filed. For example, 'Name/Description' ends up with the Address and 'Plant' ends up with a different var. Is there a trick to pushing the info in a specific ordxer?

Thank you.

TJ
Confused!?!

The code I posted worked for the Name/Description & Address fields.

Perhaps you could post the code you are using so I can review it?
Avatar of tvtech

ASKER

I'm so confused (Vinnie Babarino in Welcome Back Kotter)! :-)

  Here's what I'm doing. Also, for some reason the Date I am sending (23/12/2005) is being split across 'Address:' and 'Est Load:". How weird is that. :-(

    Dim objWord_Application                               As Object
   
    Set objWord_Application = CreateObject("Word.Application")
   
    ' Un-comment the next line to see the Word document creation on-screen, otherwise, to hide it, leave the next line alone...
     objWord_Application.Visible = True
   
    objWord_Application.Documents.Open (strFileName)
   
    objWord_Application.selection.Find.ClearFormatting
    objWord_Application.selection.Find.Text = "Name/Description:"
    objWord_Application.selection.Find.Execute
    objWord_Application.selection.MoveRight Unit:=12&              ' wdCell
    objWord_Application.selection.TypeText Text:=CustName
   
    objWord_Application.selection.Find.ClearFormatting
    objWord_Application.selection.Find.Text = "Address:"
    objWord_Application.selection.Find.Execute
    objWord_Application.selection.MoveRight Unit:=12&              ' wdCell
    objWord_Application.selection.TypeText Text:=Address
   
    objWord_Application.selection.Find.ClearFormatting
    objWord_Application.selection.Find.Text = "Est Load:"
    objWord_Application.selection.Find.Execute
    objWord_Application.selection.MoveRight Unit:=12&              ' wdCell
    objWord_Application.selection.TypeText Text:=eLoad
   
    objWord_Application.selection.Find.ClearFormatting
    objWord_Application.selection.Find.Text = "Estimated Commissioning Date:"
    objWord_Application.selection.Find.Execute
    objWord_Application.selection.MoveRight Unit:=12&              ' wdCell
    objWord_Application.selection.TypeText Text:=EstDate
       
    objWord_Application.selection.Find.ClearFormatting
    objWord_Application.selection.Find.Text = "Plant Requirements:"
    objWord_Application.selection.Find.Execute
    objWord_Application.selection.MoveRight Unit:=12&              ' wdCell
    objWord_Application.selection.TypeText Text:=Plant
       
    objWord_Application.selection.Find.ClearFormatting
    objWord_Application.selection.Find.Text = "Zone Substation:"
    objWord_Application.selection.Find.Execute
    objWord_Application.selection.MoveRight Unit:=12&              ' wdCell
    objWord_Application.selection.TypeText Text:=SubStation
   
    objWord_Application.selection.Find.ClearFormatting
    objWord_Application.selection.Find.Text = "Upstream Isolator:"
    objWord_Application.selection.Find.Execute
    objWord_Application.selection.MoveRight Unit:=12&              ' wdCell
    objWord_Application.selection.TypeText Text:=UpIS
   
    objWord_Application.selection.Find.ClearFormatting
    objWord_Application.selection.Find.Text = "Downstream Isolator:"
    objWord_Application.selection.Find.Execute
    objWord_Application.selection.MoveRight Unit:=12&              ' wdCell
    objWord_Application.selection.TypeText Text:=DownIS
   
    objWord_Application.selection.Find.ClearFormatting
    objWord_Application.selection.Find.Text = "Feeder:"
    objWord_Application.selection.Find.Execute
    objWord_Application.selection.MoveRight Unit:=12&              ' wdCell
    objWord_Application.selection.TypeText Text:=Fdr

Thanks again, fp.

TJ
ASKER CERTIFIED SOLUTION
Avatar of [ fanpages ]
[ fanpages ]

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is an example You can take data from what ever source you want. One advantage of the form fields is that you will always have the data in the correct place

Sub PrintLetter()
   Dim x    As Integer
   Dim y    As Integer
On Error Resume Next

   Set wd = GetObject(, "Word.Application")
   If Err.Number <> 0 Then
      On Error GoTo Err_
      'Word.Application.9 will set to version 9 which is Office 2000. 10 XP, 11 2003
      Set wd = CreateObject("Word.Application")
   End If
   On Error GoTo Err_
   With wd
      .Visible = True
   End With
 
   wd.Documents.Add Template:="\\Nantucket\Documents\Templates\DirectMailNoDealer.dot", NewTemplate:=False, DocumentType:=0
   With wd.Selection
      .Goto What:=wdGoToBookmark, Name:="CustName"
      .TypeText Text:="John Smith"
 
      .Goto What:=wdGoToBookmark, Name:="Address1"
      .TypeText Text:="123 Main St"
     
      .Goto What:=wdGoToBookmark, Name:="Address3"
      .TypeText Text:="Any Where MI 48061"
   End With
   Set wd = Nothing
Exit_:
   Exit Sub
Err_:
   MsgBox Err.Description
   Resume Exit_
   Resume
End Sub

George
Avatar of tvtech

ASKER

Sorry fp, but the darned fields are still muddled. I have uploaded the doc to www.qldemail.com/ee/planningrequest.doc again with the results. About the only fields that are correct are the 'Area', 'Upstream Isolator' and 'Feed'. Sorry to be a pain, but I just can't figure why you are seeing the right data and I'm not. :-(

Thanks fp.

TJ
 
Avatar of tvtech

ASKER

Damn! My mistakes and my apologies. I wasn't joining the date up correctly!! :-(

The points are all yours with much thanks, fp.

All the best.

TJ
You're very welcome, TJ.  Thanks for helping me reach my next Certification in the "Visual Basic" Topic Area:
[ https://www.experts-exchange.com/M_258171.html ]

Sorry I missed your last postings (at 2:44am & 2:53am UK time).  I waited up while after my revised code, but sleep got the better of me in the end.

I don't know if you considered George's suggestion, but it sounds as if you've found something that works in any respect.

BFN,

fp.
PS. I had forgotten about a previous question I contributed to on the same subject of creating a Word document:

[ https://www.experts-exchange.com/questions/21647854/Writing-to-a-doc-file-using-programming.html ]

You may find the Microsoft Support link I provided useful to you in the future:

"OFF: How to Use (OLE) Automation with Word"
[ http://support.microsoft.com/default.aspx?scid=kb;en-us;184974 ]

BFN,

fp.
Avatar of tvtech

ASKER

Thanks for that fp. Much appreciated.

I posted a question to you yesterday. It's at:
https://www.experts-exchange.com/questions/21674781/Question-for-fanpages-Re-Word-Doc.html

Congrats on your E-E Certs! Well done!!!

Regards,
  TJ
Thanks for your well wishes.

I just spotted your other question before your last posting, & have added a comment.

BFN,

fp.