tvtech
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
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
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_V B>
Address: <Address_Var_To_Insert_Fro m_VB>
Ph: <Phone_Var_To_Insert_From_ VB>
etc, etc ...
Thanks.
TJ
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_V
Address: <Address_Var_To_Insert_Fro
Ph: <Phone_Var_To_Insert_From_
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.
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.
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
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.
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.
ASKER
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_Fr om_VB>"
strName = "<Name_Var_To_Insert_From_ VB>"
Set objWord_Application = CreateObject("Word.Applica tion")
' Un-comment the next line to see the Word document creation on-screen, otherwise, to hide it, leave the next line alone...
' objWord_Application.Visibl e = True
objWord_Application.Docume nts.Open ("http://www.qldemail.com/ee/planningrequest.doc")
objWord_Application.Select ion.Find.C learFormat ting
objWord_Application.Select ion.Find.T ext = "Name/Description:"
objWord_Application.Select ion.Find.E xecute
objWord_Application.Select ion.MoveRi ght Unit:=12& ' wdCell
objWord_Application.Select ion.TypeTe xt Text:=strName
objWord_Application.Select ion.Find.C learFormat ting
objWord_Application.Select ion.Find.T ext = "Address:"
objWord_Application.Select ion.Find.E xecute
objWord_Application.Select ion.MoveRi ght Unit:=12& ' wdCell
objWord_Application.Select ion.TypeTe xt Text:=strAddress
objWord_Application.Active Document.S aveAs "c:\new_word.doc"
objWord_Application.Active Document.C lose
objWord_Application.Quit
Set objWord_Application = Nothing
End Sub
BFN,
fp.
Public Sub Test_Update()
Dim objWord_Application As Object
Dim strAddress As String
Dim strName As String
strAddress = "<Address_Var_To_Insert_Fr
strName = "<Name_Var_To_Insert_From_
Set objWord_Application = CreateObject("Word.Applica
' Un-comment the next line to see the Word document creation on-screen, otherwise, to hide it, leave the next line alone...
' objWord_Application.Visibl
objWord_Application.Docume
objWord_Application.Select
objWord_Application.Select
objWord_Application.Select
objWord_Application.Select
objWord_Application.Select
objWord_Application.Select
objWord_Application.Select
objWord_Application.Select
objWord_Application.Select
objWord_Application.Select
objWord_Application.Active
objWord_Application.Active
objWord_Application.Quit
Set objWord_Application = Nothing
End Sub
BFN,
fp.
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
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?
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?
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.Applica tion")
' Un-comment the next line to see the Word document creation on-screen, otherwise, to hide it, leave the next line alone...
objWord_Application.Visibl e = True
objWord_Application.Docume nts.Open (strFileName)
objWord_Application.select ion.Find.C learFormat ting
objWord_Application.select ion.Find.T ext = "Name/Description:"
objWord_Application.select ion.Find.E xecute
objWord_Application.select ion.MoveRi ght Unit:=12& ' wdCell
objWord_Application.select ion.TypeTe xt Text:=CustName
objWord_Application.select ion.Find.C learFormat ting
objWord_Application.select ion.Find.T ext = "Address:"
objWord_Application.select ion.Find.E xecute
objWord_Application.select ion.MoveRi ght Unit:=12& ' wdCell
objWord_Application.select ion.TypeTe xt Text:=Address
objWord_Application.select ion.Find.C learFormat ting
objWord_Application.select ion.Find.T ext = "Est Load:"
objWord_Application.select ion.Find.E xecute
objWord_Application.select ion.MoveRi ght Unit:=12& ' wdCell
objWord_Application.select ion.TypeTe xt Text:=eLoad
objWord_Application.select ion.Find.C learFormat ting
objWord_Application.select ion.Find.T ext = "Estimated Commissioning Date:"
objWord_Application.select ion.Find.E xecute
objWord_Application.select ion.MoveRi ght Unit:=12& ' wdCell
objWord_Application.select ion.TypeTe xt Text:=EstDate
objWord_Application.select ion.Find.C learFormat ting
objWord_Application.select ion.Find.T ext = "Plant Requirements:"
objWord_Application.select ion.Find.E xecute
objWord_Application.select ion.MoveRi ght Unit:=12& ' wdCell
objWord_Application.select ion.TypeTe xt Text:=Plant
objWord_Application.select ion.Find.C learFormat ting
objWord_Application.select ion.Find.T ext = "Zone Substation:"
objWord_Application.select ion.Find.E xecute
objWord_Application.select ion.MoveRi ght Unit:=12& ' wdCell
objWord_Application.select ion.TypeTe xt Text:=SubStation
objWord_Application.select ion.Find.C learFormat ting
objWord_Application.select ion.Find.T ext = "Upstream Isolator:"
objWord_Application.select ion.Find.E xecute
objWord_Application.select ion.MoveRi ght Unit:=12& ' wdCell
objWord_Application.select ion.TypeTe xt Text:=UpIS
objWord_Application.select ion.Find.C learFormat ting
objWord_Application.select ion.Find.T ext = "Downstream Isolator:"
objWord_Application.select ion.Find.E xecute
objWord_Application.select ion.MoveRi ght Unit:=12& ' wdCell
objWord_Application.select ion.TypeTe xt Text:=DownIS
objWord_Application.select ion.Find.C learFormat ting
objWord_Application.select ion.Find.T ext = "Feeder:"
objWord_Application.select ion.Find.E xecute
objWord_Application.select ion.MoveRi ght Unit:=12& ' wdCell
objWord_Application.select ion.TypeTe xt Text:=Fdr
Thanks again, fp.
TJ
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.Applica
' Un-comment the next line to see the Word document creation on-screen, otherwise, to hide it, leave the next line alone...
objWord_Application.Visibl
objWord_Application.Docume
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
objWord_Application.select
Thanks again, fp.
TJ
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.Applica tion")
End If
On Error GoTo Err_
With wd
.Visible = True
End With
wd.Documents.Add Template:="\\Nantucket\Doc uments\Tem plates\Dir ectMailNoD ealer.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
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.Applica
End If
On Error GoTo Err_
With wd
.Visible = True
End With
wd.Documents.Add Template:="\\Nantucket\Doc
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
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
Thanks fp.
TJ
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
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.
[ 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.
[ 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.
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
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.
I just spotted your other question before your last posting, & have added a comment.
BFN,
fp.
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.Applica
' Un-comment the next line to see the Word document creation on-screen, otherwise, to hide it, leave the next line alone...
' objWord_Application.Visibl
objWord_Application.Docume
objWord_Application.Select
objWord_Application.Active
objWord_Application.Active
objWord_Application.Quit
Set objWord_Application = Nothing
End Sub
' ...End of Code
BFN,
fp.