Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Fill test Form Fields with input variables

Posted on 2011-09-23
5
Medium Priority
?
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 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 23

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 23

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 23

Author Closing Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Suggested Courses

604 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