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
277 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 21

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
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.

758 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

20 Experts available now in Live!

Get 1:1 Help Now