#1
|
|||
|
|||
Spaces inside Fieldcode curly braces
Hello,
We have a application where we have to count the length of the fieldcode text for which we are counting the text inside the InstrText tag which is working fine so far. But we are not able to calculate the fieldcode text of the internal hyperlink field. we using below code snippet to manually build instrText of the hyperlink field. string HyperlinkFC = string.Empty; if (element.Attributes().Any(a => a.Name.LocalName == “anchor”)) { string s = @”HYPERLINK \l”; HyperlinkFC = string.Format(” {0} \”{1}\” “, s, element.Attribute(W.anchor).Value); } fieldCodeText.Append(HyperlinkFC); Above code is working fine for some cases like below: {HYPERLINK \l “_Toc165886552”} But it is failing for some cases where there is a trailing and leading white space inside the curly braces of Hyperlink fieldcode in the begining and end as below { HYPERLINK \l “_Toc165886552” } It is giving 2 characters length difference than expected. Could someone please tell me how to remove this trailing and leading white space inside curly braces? Any help will be truly appreiciated!! Regards, Manohar |
#2
|
||||
|
||||
Fields created by Word itself usually have a space inside each of the field braces. That space isn't necessary, so field codes constructed programmatically may lack them. Fields created manually may even have more of these inconsequential spaces. That said, it's not apparent why you're not using the various Field methods that are available to you, with which it doesn't really matter whether the extra spaces are there. For example:
MsgBox Trim(ActiveDocument.Hyperlinks(1).Range.Fields(1). Code.Text) will always return the field code without any superfluous leading/trailing spaces. Similarly, for the different hyperlink elements, you might use any one or more of: Code:
With ActiveDocument.Hyperlinks(1) MsgBox "Address: " & .Address & vbCr & _ "Sub-Address: " & .SubAddress & vbCr & _ "Text To Display: " & .TextToDisplay & vbCr & _ "Screen Tip: " & .ScreenTip & vbCr & _ "Target: " & .Target End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks for the quick reply Paul!, unfortunately I have to use only Openxml to extract fields but openxml is not providing proper fieldcode for internal hyperlink like below, that is the reason why i am building fieldcode through code.
Is there a way to trim all hyperlink fieldcode values through word object model? Code:
<w:hyperlink w:anchor="_Toc165886552" w:history="1" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> <w:r pt14:StyleName="DefaultParagraphFont" pt14:FontName="Arial" pt14:LanguageType="western" xmlns:pt14="http://powertools.codeplex.com/2011"> <w:rPr> <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:eastAsiaTheme="minorEastAsia" w:cs="Times" w:eastAsia="Calibri" /> <w:b /> <w:color w:val="0000FF" /> <w:sz w:val="22" /> <w:szCs w:val="24" /> <w:u w:color="000000" /> <w:lang w:val="en-US" w:eastAsia="en-US" w:bidi="ar-SA" /> </w:rPr> <w:t pt14:TabWidth="0.000">‘Software’</w:t> </w:r> </w:hyperlink> Last edited by macropod; 01-03-2017 at 02:47 AM. Reason: Added code tags |
#4
|
||||
|
||||
I have no knowledge of openxml coding or what it can do.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Is there a way to uniformly add space or delete space in all hyperlink field codes using macro or word object model??
|
#6
|
||||
|
||||
You could process all the hyperlinks with a macro such as:
Code:
Sub Demo() Application.ScreenUpdating = False Dim HLnk As Hyperlink For Each HLnk In ActiveDocument.Hyperlinks With HLnk.Range.Fields(1).Code .Text = Trim(.Text) End With Next Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Thank you very much Paul!
|
#8
|
|||
|
|||
paul,
how can we programmatically insert above macro using interop and run using the same interop object? Regards Manohar |
#9
|
||||
|
||||
I haven't done any interop coding, so I can't give you those details. Nevertheless, the methods should be the same as they're all in the Word API.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
paul,
Is there any other way to remove the leading and trailing spaces inside curly braces? because clients are not okay to use macro in their machines due to security issues. please help. Regards Manohar |
#11
|
||||
|
||||
You could clean up most cases by right-clicking on the hyperlinks, choosing 'Edit Hyperlink' and pressing OK.
Alternatively you could press Alt-F9 to expose the document's field codes, for which hyperlinks will mostly look something like: { HYPERLINK "https://www.msofficeforums.com/word/33680-spaces-inside-fieldcode-curly-braces.html" } then using a wildcard Find/Replace, where: Find = [^32](HYPERLINK "*")^32 Replace = \1 Other hyperlinks that have field codes like: { HYPERLINK "https://www.msofficeforums.com/word/33680-spaces-inside-fieldcode-curly-braces.html" \ o "Click Here" } After running the foregoing Find/Replace, those will look like: {HYPERLINK "https://www.msofficeforums.com/word/33680-spaces-inside-fieldcode-curly-braces.html"\ o "Click Here" } Note the loss of the space before the \o. The following wildcard Find/Replace will fix those, too: Find = (HYPERLINK "*)(\\o "*")^32 Replace = \1 \2 When you're done, press Alt-F9 to restore the display. The advantage of the Find/Replace is that it's faster on a document with many hyperlinks.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
can this find/Replace achived programmtically on word object?
|
#13
|
||||
|
||||
You just said your clients can't use macros - which is what "find/Replace achived programmtically on word object" entails (unless you're using interop - for which I've already told you "the methods should be the same as they're all in the Word API").
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
could you please give me the wild card pattern to replace space in below type hyperlink aswell
{ HYPERLINK \l “_Toc165886552” } |
#15
|
||||
|
||||
That is a Table of Contents hyperlink. Doing anything with those is pointless as they get deleted & replaced every time the Table of Contents is updated/refreshed. Still, if that's what you want to do, try:
Find = ^32(HYPERLINK*"*")^32 Replace = \1 Note also that exposing field codes via Alt-F9 usually does not reveal these hyperlinks for a Find/Replace to work on.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Text inside text boxes create headings inside my table of contents!!! How do I delete the created he | carstj | Word | 3 | 04-11-2016 12:46 PM |
What does <!//FieldCode**> indicate? | kopp123 | Mail Merge | 1 | 09-03-2015 10:03 PM |
Straight Vs Curly Quotation Marks | FOsmund | Word | 4 | 04-16-2015 01:48 PM |
Replacing curly braces in a wildcard F&R | Ulodesk | Word | 9 | 02-21-2014 07:28 AM |
Balky Curly Quotes | JamesT | Word | 0 | 01-23-2010 05:36 PM |