Link to home
Start Free TrialLog in
Avatar of yo_bee
yo_beeFlag for United States of America

asked on

Fill test Form Fields with input variables

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.

ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yo_bee

ASKER

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

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.
Avatar of yo_bee

ASKER

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

Avatar of yo_bee

ASKER

You were a great help.