Solved

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

Posted on 2011-09-26
3
279 Views
Last Modified: 2012-05-12
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.

 
0
Comment
Question by:yo_bee
3 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36599877
Changing the Text of a bookmark (Range.Text) replaces the bookmark by the specified String, thus deleting the bookmark. The bookmark on a field is seen as a way to enable cross-references, it is not intended to be modified.

Go straight to the field instead of going through its bookmark:
doc.FormFields("textbox1").Result = = szUser & "RSPTY"

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36600101
Also, unless you want to change the design of a forms document, keep it protected.
0
 
LVL 22

Author Closing Comment

by:yo_bee
ID: 36600769
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.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

770 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