Solved

Fill test Form Fields with input variables

Posted on 2011-09-23
5
340 Views
Last Modified: 2012-08-14
I have a Word document that has instructions for connecting to our FTP site.
This document has two Text Form Fields that will contain the Username and Password.  Once filled out it is printed as a PDF and passed on to the user.

I currently have the first part of the code that creates the directory structure and inport.csv file for the FTP server.

What I would like to do next is to
1: Polulate the two Text Form Fields in this word document with the input variables I entered when I first opened this code. (both have bookmark id's)
2: Print as a PDF
3: Save the PDF.

0
Comment
Question by:yo_bee
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36588850
Here is the basic code to open the document and fill the fields:
Dim app As New Microsoft.Office.Interop.Word.Application
Dim doc As Microsoft.Office.Interop.Word.Document

doc = app.Documents.Open("YourWordFile.doc")

app.Visible = True 'For debuggin or if you want the user to see the application

doc.Bookmarks("YourFirstBookmark").Range.Text = "TextForFirstBookmark"
doc.Bookmarks("YourSecondBookmark").Range.Text = "TextForSecondBookmark"

app.Quit()

Open in new window

Now, saving as PDF depends.

I understand that the newer versions of Word have that feature built in or that it can be added through some kind of add-in. but I am still on Office 2003, so I do not not know how it is set up. If you are in 2007 or 2010 and are able to print a pdf, simply start the macro recorder, print a pdf manually, and go look at the code generated in the VBA window (Alt-F11)

In Word 2003, you need a pdf printer driver. Could be Acrobat, could be CutePDF (the one I use), could be something else. The code to print then depends on the tool you use to generate the pdf.
0
 
LVL 21

Author Comment

by:yo_bee
ID: 36589037
I get this in debug

"The requested member of the collection does not exist"
Private Sub oDoc(ByVal szUser As String)
        Dim oword As New Application
        oword.Documents.Open("C:\Users\boscam\Downloads\00903221.doc", Visible:=True)
        oword.Selection.Bookmarks("textbox1").Range.Text = szUser & "Public"
        oword.Selection.Bookmarks("textbox2").Range.Text = "pa$$w0rd"
        'oword.Documents.Save()
        'oword.Documents.Close()
        'oword.Application.Quit()


    End Sub

Open in new window

0
 
LVL 40
ID: 36589312
A few things are not right in your code.

oWord is the application, the copy of word that is running. The Bookmarks are in the document, not in the application. That is why I created 2 separate variables in my sample code. Some properties and methods apply to the application, others apply to the document.

Remove Selection in oword.Selection. It looks for the bookmark only in the selected text. A lot of people do that error, because they try to copy the code generated by the Word macro recorder in .NET. Although the VBA code gives you a very good idea of the operations to do, it usually cannot be copied as such in .NET. The VBA code assumes that a user manipulates the cursor on the screen. In .NET, except for some operations, you do not care at all about the cursor, you go straight to the bookmark. Selection being a representation of what the cursor does, it is thus usually useless from .NET.

It would be nice also, when you get an error, to tell us which of the lines gives you the error. Otherwise, we have to guess over what could be wrong.
0
 
LVL 21

Author Comment

by:yo_bee
ID: 36589608
Revamped it and it works now.

Private Sub oDoc(ByVal szUser As String)
        Dim oword As New Application
        Dim doc = oword.Documents.Open("C:\Users\boscam\Downloads\00903221.doc")
        oword.Visible = True

        doc.Bookmarks("textbox1").Range.Text = szUser & "public"
        doc.Bookmarks("textbox2").Range.Text = "pa$$w0rd"
       
        oword.Documents.Save()
        oword.Documents.Close()
        oword.Application.Quit()


    End Sub

Open in new window

0
 
LVL 21

Author Closing Comment

by:yo_bee
ID: 36589613
You were a great help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now