Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 01-03-2017, 12:07 AM
MANOHAR MANOHAR is offline Windows 7 32bit Office 2007
Novice
 
Join Date: Jan 2017
Posts: 22
MANOHAR is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 01-03-2017, 01:28 AM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 15,013
macropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to all
Default

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
[MS MVP - Word]
Reply With Quote
  #3  
Old 01-03-2017, 01:54 AM
MANOHAR MANOHAR is offline Windows 7 32bit Office 2007
Novice
 
Join Date: Jan 2017
Posts: 22
MANOHAR is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 01-03-2017, 02:46 AM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 15,013
macropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to all
Default

I have no knowledge of openxml coding or what it can do.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 01-03-2017, 10:40 AM
MANOHAR MANOHAR is offline Windows 7 32bit Office 2007
Novice
 
Join Date: Jan 2017
Posts: 22
MANOHAR is on a distinguished road
Default

Is there a way to uniformly add space or delete space in all hyperlink field codes using macro or word object model??
Reply With Quote
  #6  
Old 01-03-2017, 01:33 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 15,013
macropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to all
Default

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
[MS MVP - Word]
Reply With Quote
  #7  
Old 01-04-2017, 12:23 AM
MANOHAR MANOHAR is offline Windows 7 32bit Office 2007
Novice
 
Join Date: Jan 2017
Posts: 22
MANOHAR is on a distinguished road
Default

Thank you very much Paul!
Reply With Quote
  #8  
Old 01-04-2017, 10:05 PM
MANOHAR MANOHAR is offline Windows 7 32bit Office 2007
Novice
 
Join Date: Jan 2017
Posts: 22
MANOHAR is on a distinguished road
Default

paul,

how can we programmatically insert above macro using interop and run using the same interop object?

Regards
Manohar
Reply With Quote
  #9  
Old 01-04-2017, 10:25 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 15,013
macropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to all
Default

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
[MS MVP - Word]
Reply With Quote
  #10  
Old 01-10-2017, 12:20 AM
MANOHAR MANOHAR is offline Windows 7 32bit Office 2007
Novice
 
Join Date: Jan 2017
Posts: 22
MANOHAR is on a distinguished road
Default

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
Reply With Quote
  #11  
Old 01-10-2017, 01:22 AM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 15,013
macropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to all
Default

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 "http://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 "http://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 "http://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
[MS MVP - Word]
Reply With Quote
  #12  
Old 01-10-2017, 01:39 AM
MANOHAR MANOHAR is offline Windows 7 32bit Office 2007
Novice
 
Join Date: Jan 2017
Posts: 22
MANOHAR is on a distinguished road
Default

can this find/Replace achived programmtically on word object?
Reply With Quote
  #13  
Old 01-10-2017, 01:43 AM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 15,013
macropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to all
Default

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
[MS MVP - Word]
Reply With Quote
  #14  
Old 01-10-2017, 02:11 AM
MANOHAR MANOHAR is offline Windows 7 32bit Office 2007
Novice
 
Join Date: Jan 2017
Posts: 22
MANOHAR is on a distinguished road
Default

could you please give me the wild card pattern to replace space in below type hyperlink aswell

{ HYPERLINK \l “_Toc165886552” }
Reply With Quote
  #15  
Old 01-10-2017, 02:44 AM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 15,013
macropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to all
Default

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
[MS MVP - Word]
Reply With Quote
Reply
Please reply to this thread with any new information or opinions.

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


All times are GMT -7. The time now is 01:34 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft