Bookmark Update Problem  
Author Message
Scott VanDelinder





PostPosted: Visual Studio Tools for Office, Bookmark Update Problem Top

I'm trying to update the text of a bookmark when a document template opens, and have been working to adapt the sample that was provided for VSTO 2003 to read an XML file and populate the bookmarks. I want to use VSTO 2005 instead so my bookmarks don't get removed from the document when I update the text. I have it more or less working, but I'm getting an extra '%' character appended onto the end of the bookmark text. Does anyone know how to get around this. Here's the basics of what I'm doing:

Dim targetControl As Object
For Each targetControl In Me.Controls
If TypeOf (targetControl) Is Word.Bookmark Then
Dim bookMark As Word.Bookmark = CType(targetControl, Word.Bookmark)
If bookmark.Name = "document_title" Then
bookMark.Text = "A New Title"
End If
End If
Next

When I run this I get the "document_title" bookmark populated with the text "A New Title%".

Also, is there a way to get the Word document to update to display the new text value without having to click "cancel" in the dialog that appears for each bookmark when I execute the statement:

Me.Fields.Update()

Any help would be appreciated.

Scott VanDelinder


Visual Studio Tools for Office8  
 
 
Cindy Meister





PostPosted: Visual Studio Tools for Office, Bookmark Update Problem Top

Hi Scott

A quick test doesn't display the same behavior as you report. Did you test the sample code, as you show it, or did you just extrapolate

From my experience with Word, my guess would be the "stray character" is either coming in from the data source, or is a problem in the Word document (as in damaged file structures).

Also, could you please provide more details about the dialog box you're seeing I don't see one, when I run your code sample. The exact title and prompt may provide a clue as to what's happening...



 
 
Scott VanDelinder





PostPosted: Visual Studio Tools for Office, Bookmark Update Problem Top

The code was pretty much as it shows, I just did a small bit of cleanup. Looking at things closer, I am trying to use an existing template that I'd like to populate the bookmarks for. In that template, the bookmarks are actually embedded within form fields, which use the "ask" function, so that is where the prompt is coming from. When I tell the document to update all the fields it performs the "ask" routine for each one.

I just tried picking the bookmark of interest from the properties list in the word template (within VSTO environment) and updating the text property manually there, then requesting the form field to update. It also gave me the "%", so I guess it is something to do with the form field and not the bookmark. However, if I select all the fields in the document then perform the "update field" function and type some text when prompted, then I don't get a "%". If I look at the bookmark properties after this the text appears just as typed at the prompt.

Any ideas on what is happening with the "%"

Thanks,
Scott VanDelinder

 
 
Cindy Meister





PostPosted: Visual Studio Tools for Office, Bookmark Update Problem Top

True form fields Or just ASK fields My head is whirling from the possibilities you toss up with this explanation :-)

Could you very, very precisely describe the position you're starting from, please :-) Then I can do some experimenting - but my time is money, too, so I'd like to be sure I'm duplicating your environment (no more "false starts") .

At a guess, the problem is due to the layers of "nesting" involved, in combination with how VSTO is programmed to deal with the bookmark range. I suspect the VSTO developers didn't consider they'd need to deal with bookmarks nested in field codes... Since we can't change how VSTO works, it may turn out, in order to deal with these bookmarks, that you'll have to work directly with the Word object model, rather than using the bookmark tool VSTO provides. That would mean writing the text to the .Range.Text property, then re-adding the bookmark.

Now, if for testing purposes, you insert a "real" bookmark into the document text, I assume that behaves "as expected"



 
 
Scott VanDelinder





PostPosted: Visual Studio Tools for Office, Bookmark Update Problem Top

Yes, you are correct - if I create a new "real" bookmark in the template the update works fine. I would love to be able to tell you precisely where I'm starting from, but I'm having some trouble doing that. I grabbed a template that someone else created and was trying to incorporate this into it, while preserving the ASK functionality that is going on. However, I can't seem to figure out how to get to a point where I can see precisely what is going on with the field code in the document. When I edit that field it just shows the bookmark name in the "field codes" portion of the dialog that appears. The ASK function is definitely being used, but I just can't get at the underlying details. Guess I might be out of luck.

 
 
Cindy Meister





PostPosted: Visual Studio Tools for Office, Bookmark Update Problem Top

OK, I think I understand more or less where you're starting from. The main problem you're going to have with this approach is that the bookmark is a "point" IN the ASK field. If you press Alt+F9 in the document, with the bookmark symbols option (Tools/Options/View) displayed, you'll see the gray I-bar symbol inside the field code.

There really isn't any way to write information to this bookmark correctly. Word will let you, but any REF fields displaying the result won't update correctly until/unless the prompt displays. (A quick test here, using VBA, doesn't get the %, but since the approach really isn't viable, as it stands...)

Can you tell me the purpose behind keeping the ASK field prompts And what the bookmark information is being used for, in the document (since an ASK field doesn't display in the text). A couple of workarounds come to mind, but I need to understand the end-goal, first :-)



 
 
Ade Miller - MSFT





PostPosted: Visual Studio Tools for Office, Bookmark Update Problem Top

So this problem here is embedding bookmarks inside the ASK field I think. I'm not very familiar with these fields but I'm rather over familiar with the internal workings of the VSTO bookmark code.

Under the hood the VSTO Bookmark attempts to prevent the underlying Word bookmark from being deleted by adding special characters to the begining and end of the existing text prior to modifying it. It'll come as no big surprise that the characters used are (from memory) '$' and '%'. What's happening here is a bug as the VSTO bookmark algorithm isn't taking into account the ASK field and is incorrectly leaving the '%' behind when it's done.

See the following post if you'd like to report this as a bug:

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=196504&SiteID=1

I'm not sure there's an obvious workaround for this. You might want to follow up with Cindy on some workarounds as maybe there's an overall better approach you can take.

Thanks,

Ade



 
 
Scott VanDelinder





PostPosted: Visual Studio Tools for Office, Bookmark Update Problem Top

The purpose of keeping the ASK fields was to continue to support the method that the users currently have for updating the template, while moving towards a method to read the data from an XML file to fill in the fields. My thought was to have an application they use to initially populate the document from the template, possibly executed by an administrative assistant. Then the person providing the content can just open and fill in the resulting document as required. However, if later it was determined that the data filled in had an error, I wanted to let the user update them by doing a 'select all', then use 'Update Field', which will then prompt the user to fill in the text for each field (or hit cancel for a particular field to keep existing text).

These fields hold things like the document title, customer name, project name, project number, etc. The ASK field has a prompt which identifies which field it is asking for, then the entered text updates the bookmark, which updates the text that is displayed within the document.

I saw the explanation about the "%" being an artifact left while VSTO used it as a placeholder being a bug, which makes sense.

I realize the path of least resistance may be to update the templates and no longer use the ASK fields, but it seemed like I was so close to success.

 
 
Cindy Meister





PostPosted: Visual Studio Tools for Office, Bookmark Update Problem Top

Hi Scott

Hmmm. Well, since you're posting in a VSTO forum, I'll just make the remark that rebuilding the entire template to use VSTO functionality would be a possibility :-) Instead of the ASK fields you could

- link the document (or template) with an XML schema that represents the data

- insert XML nodes in place of the ASK fields, with a data cache

- create an Action Pane (task pane) to display the nodes' content

- fill the data cache/nodes more or less as you're envisioning.

- the user can then edit the nodes' content, as required, in the action pane without having to scroll through the entire document

 
 
Scott VanDelinder





PostPosted: Visual Studio Tools for Office, Bookmark Update Problem Top

You've given me some options and insight, so I'll play around with these ideas as I get a chance. Thanks for the help on this.