Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

some sample code for inserting text into existing Word doc?

Status
Not open for further replies.

kskinne

Technical User
Oct 8, 2004
169
US
I have a small program set up for submitting requests and getting those requests approved, whereby the program writes new requests to a table and keeps track of when they are approved, denied, etc., and it also sends an email notification when a request is submitted, when someone goes in and approves it, etc.

I would like to take this a step further and have the option to print a given request out in a nice form layout, whenever one is needed. I was wondering if it would be possible to set up a Word document with a table in it, and then have my code automatically populate specific cells in the table with the data that is unique to that particular request, and then have it print the completed document. There would not really be any need that I can think of to save the changes to the document.

Does this sound like a good way to do this, and if so, can someone get me pointed in the right direction on starting this, or to some sample code that would do this? I have experimented with setting and opening a Word document object in my code, and using .TypeText to add information to the empty document, however what I'm struggling with is getting it write that text to very specific places within the document, for example into a table as I mentioned above.

Or, if there is a better way to do this, I'm open to suggestions.

Thank you,
Kevin
 

In my opnion, you need to set up the bookmarks in your Word document, and then in your code you may just say "Go To MyBookmark7" (or whatever you called your bookmark) and place this text.

This approach works for me.

Have fun.

---- Andy
 


Hi,

I did something like this, way back in 1995, simply using MailMerge and an Excel Sheet for my data source. Each row in my spreadsheet was a customer order for a personalized children's book, I'd open the Word docuent and scroll the MailMerge to the record of interest and print my final output right from the template. THAT could be done programatically. Check out MailMerge Object for the appropriate properites/methods to accomplish.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks for the replies - Andy I think utilizing bookmarks sounds like the way I want to go, however I'm not sure on the syntax for going to a bookmark and entering the text there, then going to the next bookmark and entering text there, etc. If you could help me out I'd appreciate it.

For your reference here is how I'm writing text into my test document right now:

Code:
Private Sub Print_Word_Doc()
Dim strFilePath As String
strFilePath = "S:\FORMS\Old Forms\Request Form.doc"

Dim App As Object
Dim doc as Object

Set App = CreateObject("Word.Application")
Set doc = App.Documents.Open(strFilePath, , 1)

App.ActivePrinter = "Universal Document Converter"
doc.ActiveWindow.Selection.TypeText "INSERT MY TEXT HERE"

Call App.PrintOut(False)

ActiveDocument.Close SaveChanges:=False

Set doc = Nothing

Call App.Quit
Set App = Nothing

End Sub
 
I think I might have this figured out but wanted to check here. I replaced:

doc.ActiveWindow.Selection.TypeText "INSERT MY TEXT HERE"

with this:

doc.Bookmarks("Request_No").Select
Selection.TypeText "10"
doc.Bookmarks("Request_Date").Select
Selection.TypeText "11/09/2009"

and this seems to work well. However if anyone looks at this and knows there is a more efficient way to do the same thing I'd really appreciate if you could let me know.

Thanks,
Kevin
 

I have something like this (Word 2003)"
Code:
With App.Selection
    .GoTo What:=wdGoToBookmark, Name:="Request_No"
    .TypeText Text:="10"

    .GoTo What:=wdGoToBookmark, Name:="Request_Date"
    .TypeText Text:="11/09/2009"
End With

Have fun.

---- Andy
 
I have another question related to my code, right now it prints to my default printer, but I would like to change this so it prints to a different printer in my printer list - actually I want it to print to a pdf print driver, so that it prints it to a pdf file, instead of printing a hard copy.

How can I refer to this printer in my code, so that it prints there instead of to my current printer?

Thanks,
Kevin
 
Try this:

Code:
  Dim Prt As Printer
  
  For Each Prt In Printers
    Debug.Print "Printer #" & CStr(P&) & " = " & Prt.DeviceName
    P& = P& + 1
  Next Prt
  Set Printer = Printers(2)

This will display a list of your printers in the Debug window. All you then have to do is substitute the (2) for the number of your PDF printer.

HOWEVER a better idea would be to use this code to populate a droplist so that you can choose a printer (the number might be different if you install / remove printers) rather than hardwire it into your code, even if it's only ever going to run on your PC.

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Hi Andy - the only problem with this is, this isn't always going to be run from my workstation, and some workstations have access to different printers, use a different default printer, etc., and on each workstation, the PDF print driver is not always in the same position in the list of printers. What I need is to select that specific printer via the code regarding of where it is at in the printer list.

Thanks,
Kevin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top