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

asked on

VB.net Code that opens and updates Text fields in a Word Doc returns " Requested Member of the Collection Does Not Exist"

So I have a code that I am writing to help standardize the creation of FTP sites and user accounts.
Part 1 of the code works as expected.
1: Asked for a Client Name and a Sub-Name
2: Creates the folder structure and produces a CSV import file for the FTP server.

Part 2 is where I am having some issues.
1: Opens a Word File that has the Text Form Fields (Bookmarks)
2: Updates the two text form fields and exports to a PDF successfully upon the first time updating the text form fields.
3: Upon the second pass is where I run into the error "Requested Member of the Collection Does Not Exist" when trying to update the bookmarks a second time with the same opened Doc.

Attached is the code snippet that has remarks indicating the areas of trouble.
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 = False

        'This section is working properly
        doc.Bookmarks("textbox1").Range.Text = szUser & "public"
        doc.Bookmarks("textbox2").Range.Text = "pa$$w0rd"
        doc.ExportAsFixedFormat("c:\users\boscam\downloads\" & szUser & "public.pdf", WdExportFormat.wdExportFormatPDF, OpenAfterExport:=False)


        'Loops through an opened Word document with a Text Form Field and adds
        'a value to each textbox then repeats 5 more times.
        'ToDo:  There is a error "Requested Member of the Collection Does Not Exist" 
        'when trying to process the text form field after the first pass is complete.
        For i = 1 To 6
            doc.Bookmarks("textbox1").Range.Text = szUser & "area" & i
            doc.Bookmarks("textbox2").Range.Text = "pa$$w0rd" & i
            doc.ExportAsFixedFormat("c:\users\boscam\downloads\" & szUser & "area" & i & ".pdf", WdExportFormat.wdExportFormatPDF, OpenAfterExport:=False)
        Next

        'It never gets to this section due to the error
        doc.Bookmarks("textbox1").Range.Text = szUser & "RSPTY"
        doc.Bookmarks("textbox2").Range.Text = "pa$$w0rd"
        doc.ExportAsFixedFormat("c:\users\boscam\downloads\" & szUser & "RSPTY.pdf", WdExportFormat.wdExportFormatPDF, OpenAfterExport:=False)

        doc.Close(WdSaveOptions.wdDoNotSaveChanges)
        oword.Application.Quit()


    End Sub

Open in new window


I thought of Closing and reopening the file, but if there is a way to address this logic using the current open Doc I would rather do that.

On a side note there is a @WD.dot that opens with this document that is used to hook into our Document Management system.  When closing the document the user is prompted "Do you want to save the changes to the @wd.dot.  With this project I do not need to have it hook into our Document Managment system and can bypass the @wd.dot.  If opening the file in safe mode would address the issue I am having then I would like to open it that way using VB.net.

 
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 GrahamSkan
Also, unless you want to change the design of a forms document, keep it protected.
Avatar of yo_bee

ASKER

Perfecto.
Thanks so much.  I Found that solution prior to your suggestion, but I must have applied it incorrectly.
I replaced all doc.bookmarks("").range.text with doc.formfields("").result =  and It worked.