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
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 23

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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