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
278 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

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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 a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

910 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

23 Experts available now in Live!

Get 1:1 Help Now