PDA

View Full Version : Solved: How to maintain text formatting after a loop unlinks a dropdown field???



DaVinney
06-14-2011, 08:43 AM
It appears that when a snippet of code like the one below encounters a dropdown formfield, sometimes text formatting is lost. For example, bold is no longer bold, color switches back to black, etc.

Any suggestions how I can modify the loop to ensure formatting is always restored or maintained?

Dim fField as field

With Activedocument
If .ProtectionType <> wdNoProtection Then
.Unprotect
End If

For Each fField In .Fields
fField.Unlink
Next
End With

DaVinney
06-15-2011, 06:53 PM
:anyone:

DaVinney
06-20-2011, 01:54 PM
I didn't receive any forum help with this snag, so I had to come up with my own clunky workaround using the duplicate method. When the loop encounters a culprit drop down field, it duplicates the font formatting of the .result, then applies it later so the unlinked text looks like the original. Actually, in my version, I worked around the field.unlink method and sorta created my own.

I'd be interested in a more elegant solution if anyone has one.

A snippet of the workaround:

Dim fField As Field

With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect
End If

For Each fField In .Fields
If fField.Type = wdFieldFormDropDown Then
fField.Select
Dim ffFont As Font
Dim ffResult As String
Set ffFont = Selection.Font.Duplicate
ffResult = Selection.FormFields(1).Result
fField.Delete
Selection.InsertAfter ffResult
Selection.Font = ffFont
End If
Next
End With

Frosty
06-20-2011, 03:56 PM
DaVinney, if that works for your needs, then I think you *might* be able to get rid of your use of the selection object and use a range object instead, but I'm hesitant to even provide a code example, because even switching from the selection object to a range object with fields can be wonky at times, depending on how things are set up (hiding field codes, displaying field codes, smart select, nested fields, etc).

It's tough to give you a more elegant solution without knowing a bit more. The problem is sort of conceptual:

While Word will allow you to set your ffFont object to a .Duplicate of your Selection.Font, you're not necessarily always getting what you think you're getting (i.e., there are a *lot* of things which are contained in the font object, not the least of which are the actual font, bold/italic/underline, any colors, size, etc).

However, since Font-formatting is applied on a *PER CHARACTER* basis in Word, if your range (Selection, in this case) has multiple values (some characters are bold and others aren't, as an example), Word won't give you anything as the value (the .Bold property, for example, will equal 999999).

Or, at times, it may give you the value of the first character (and the way word works in this regard is also version dependent, as well as, in some cases, dependent on compatibility settings, "smart" selecting, etc).

In short, there are a lot of variables at play to feel comfortable giving you The Solution. Especially since fields will flip to bold formatting in some cases (like when a bookmark is deleted that the field wanted to reference), and ranges will vary depending on whether you happen to be revealing field codes or not.

I would need to back up and ask why you're unlinking only the drop down fields in the document, if the same behavior (some font formatting lost) exists when you natively unlink the field, and whether or not there are just a few things you care about, or if you potentially care about everything.

I read over this and didn't originally give a reply, because this is such a big question (in my opinion). I was stricken with Paralysis By Analysis, I guess ;)

I've never really liked the Font object itself (in terms of capturing existing formatting from a range or selection), although I've certainly used it in the following type manner:


dim oFont as Font
With oFont
.Bold = True
.Underline = wdUnderlineSingle
.SmallCaps = True
.Italic = True
.ColorIndex = wdBrightGreen
End With

If I wanted to set a number of attributes at once, and then pass the object to another routine.

Maybe someone else can add or give a simpler explanation, but that's all I've got ;)

So, in summary-- looks good! :rotlaugh:

DaVinney
06-20-2011, 05:40 PM
Thanks for the response and insight, Frosty. For a few days there, I thought I was on everyone's Ignore list.

The docs that users will run this routine on consist only of drop down and text input fields. .unlink of text fields behaves fine, but the drop down lists create a problem when someone has changed the color or bold attribute of the field, then they wish to unlink the fields. So that's why I only dealt with drop downs in the snippet.

I'm going to keep testing over the following days to see if this is good enough.

Maybe this thread will help someone else with the same snag.

Cheers :hi: