Solved

Fill test Form Fields with input variables

Posted on 2011-09-23
5
350 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 22

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 22

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 22

Author Closing Comment

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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

776 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