Solved

Fill test Form Fields with input variables

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

932 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

11 Experts available now in Live!

Get 1:1 Help Now