Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-07-2013, 02:10 PM
Designergrrl Designergrrl is offline Replace Unless Style = Hyperlink? Mac OS X Replace Unless Style = Hyperlink? Office for Mac 2011
Novice
Replace Unless Style = Hyperlink?
 
Join Date: Oct 2013
Posts: 5
Designergrrl is on a distinguished road
Default Replace Unless Style = Hyperlink?

Hi all and thanks in advance for any replies;



This question is about replacing text only under certain conditions.

Background: I'm working on a macro for the editorial department of an academic institution. They get loads of documents that have the same issues and asked for some help to reduce the time they spend on each one.

Two of the things they want:
  • If a hyphen is between two digits, change it to an en-dash
  • Change every ampersand (&) to the word "and"
I have a RegExp that finds and replaces those hyphens, but I noticed a problem. My find/replace changes the "display text" of hyperlinks. Same with ampersands. Bad. So what I'm trying to figure out is how to exclude text that has Selection.Style = Word.ActiveDocument.Styles("Hyperlink")

BTW, what's the logical operator for "not equal"? I tried <> and >< but I always get an error telling me that an expression is expected. I'm new to VBA so please forgive the newbie question.

This is working (part of a much larger Sub):

Code:
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "([0-9])-([0-9])"
        .Replacement.Text = "\1" & Chr$(150) & "\2"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
So can I create an If/Then statement to tell it to replace only if the style is not hyperlink?

Thanks again,
Rissa
Reply With Quote
  #2  
Old 10-07-2013, 03:37 PM
slaycock slaycock is offline Replace Unless Style = Hyperlink? Windows 7 64bit Replace Unless Style = Hyperlink? Office 2013
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

Before you start exploring a conditional statement select your document and do an update (F9) then check to see if your hyperlinks have gone back to their original text. If yes then you just need to add the line

Activedocument.fields.update

to replicate the same thing programatically.
Reply With Quote
  #3  
Old 10-07-2013, 08:00 PM
Designergrrl Designergrrl is offline Replace Unless Style = Hyperlink? Mac OS X Replace Unless Style = Hyperlink? Office for Mac 2011
Novice
Replace Unless Style = Hyperlink?
 
Join Date: Oct 2013
Posts: 5
Designergrrl is on a distinguished road
Default

Quote:
Originally Posted by slaycock View Post
Before you start exploring a conditional statement select your document and do an update (F9) then check to see if your hyperlinks have gone back to their original text. If yes then you just need to add the line

Activedocument.fields.update

to replicate the same thing programatically.
Thanks slaycock. I did indeed check and the text retains its style as hyperlink, accompanied by a field containing the actual link.

I'll keep looking.
Reply With Quote
  #4  
Old 10-08-2013, 01:50 AM
slaycock slaycock is offline Replace Unless Style = Hyperlink? Windows 7 64bit Replace Unless Style = Hyperlink? Office 2013
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

Hi Designergrrl

Apologies, I may be confusing you.

If you go to any hyperlink and click in it (not CTRL+Click) you can type over the text. However as soon as you refresh your document (or even just the hyperlink field) the original text will be restored.

My understanding of your original post is that this is exactly what you want to happen, you don't want any changes to the hyperlink text. Is this correct or am I misunderstanding you.
Reply With Quote
  #5  
Old 10-08-2013, 05:21 AM
Designergrrl Designergrrl is offline Replace Unless Style = Hyperlink? Mac OS X Replace Unless Style = Hyperlink? Office for Mac 2011
Novice
Replace Unless Style = Hyperlink?
 
Join Date: Oct 2013
Posts: 5
Designergrrl is on a distinguished road
Default not sure which of us is confused :)

Quote:
Originally Posted by slaycock View Post
Hi Designergrrl

Apologies, I may be confusing you.

If you go to any hyperlink and click in it (not CTRL+Click) you can type over the text. However as soon as you refresh your document (or even just the hyperlink field) the original text will be restored.

My understanding of your original post is that this is exactly what you want to happen, you don't want any changes to the hyperlink text. Is this correct or am I misunderstanding you.
Hi Slaycock;

Thanks again for your responses. I do appreciate it.

Yes that's right. I don't want to change the hyperlink text. But I think we're talking about two different things. There are actually three parts to a hyperlink, right? There's the hyperlink field, which is hidden by default. Then there's the text that displays when the field is hidden—the text that the user clicks on—which is called "display text". And finally there's the tip that displays when the user hovers.

I'm trying to prevent changes to the "display text". The other two parts are unchanged as long as I keep fields hidden when the macro is running.

What you describe is different from what I'm seeing. If I make any change to the "display text," the change is permanent. The link itself still works—the hidden field is unchanged—but the display text is changed and it stays changed, even after updating the application screen.

So... yeah, I'm stumped.

Best,
DG
Reply With Quote
  #6  
Old 10-08-2013, 08:17 AM
slaycock slaycock is offline Replace Unless Style = Hyperlink? Windows 7 64bit Replace Unless Style = Hyperlink? Office 2013
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

Hi Designgrrl

You are correct. I'm confusing word cross references with hyperlinks. Doing a search and replace on hyperlinks reproduces the effect you describe.

I found two possible ways around this.

1. Show field code rather than field results. the text in the field code gets changed but updating the field produces the unchanged original. However this is not robust as there may be stuff in the field codes which shouldn't be changed.

2. A more laborious search and replace making sure you use each style in turn. Something like

Dim myStyle as Style
For each myStyle in Activedocument.styles
With selection.find
If mystyle = activedocument.style(wdstylehyperlink) then
'do nothing
else
.clearformatting
.replace.clearformatting
.text=<search text>
.replacement.text=<replace text>
.format=true
.style=mystyle
.forward=true
.wrap=wdfindcontinue
.execute
end if
next
end sub


Please note that you have to include the style in the search parameters so you can avoid searching when the style matches 'Hyperlink'

Last edited by slaycock; 10-08-2013 at 08:33 AM. Reason: Premature close
Reply With Quote
  #7  
Old 10-08-2013, 09:02 AM
Designergrrl Designergrrl is offline Replace Unless Style = Hyperlink? Mac OS X Replace Unless Style = Hyperlink? Office for Mac 2011
Novice
Replace Unless Style = Hyperlink?
 
Join Date: Oct 2013
Posts: 5
Designergrrl is on a distinguished road
Default Sloppy but effective

OK, here's the deal...Word Macros don't exactly execute linearly.

I eventually figured out how to write an If/Then/Else statement that mostly worked. Mostly. It didn't actually check the condition until after it replaced (wdReplaceOne). So it would change the first hyphen in a hyperlink and then go "oh, wait! This is a hyperlink!" and then it would skip any subsequent hyphens in that hyperlink.

So I ended up splitting my If/Then/Else into two separate If/Then blocks. The first one says "move on, nothing to do here," and the second one says, "aha! here's where we need a change." The code below, although cringe-worthy, does exactly what I want.

Code:
Sub replaceHyphens()
'
' Find hyphens that occur between digits and change them to en-dash, EXCEPT in hyperlinks
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "([0-9])-([0-9])"
    .Forward = True
    .Format = True
    .Wrap = wdFindContinue
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
 End With
 Do While (Selection.Find.Found = True)
    If (Selection.Style = ActiveDocument.Styles("Hyperlink")) Then
       Selection.Move Unit:=wdSentence, Count:=1
    End If
    Selection.Find.Execute
    If (Selection.Style <> ActiveDocument.Styles("Hyperlink")) Then
        Selection.Find.Replacement.Text = "\1" & Chr$(150) & "\2"
        Selection.Find.Execute Replace:=wdReplaceOne
    End If
Loop
End Sub
Reply With Quote
  #8  
Old 10-08-2013, 09:04 AM
Designergrrl Designergrrl is offline Replace Unless Style = Hyperlink? Mac OS X Replace Unless Style = Hyperlink? Office for Mac 2011
Novice
Replace Unless Style = Hyperlink?
 
Join Date: Oct 2013
Posts: 5
Designergrrl is on a distinguished road
Default

Quote:
Originally Posted by slaycock View Post
Hi Designgrrl

You are correct. I'm confusing word cross references with hyperlinks. Doing a search and replace on hyperlinks reproduces the effect you describe.

(snipped)

Yeah, see my post of a minute ago. You're right on track -- it's just that VBA isn't precisely linear. It may not figure out that it has found a hyperlink until after it digs in there. (sigh)
Reply With Quote
Reply

Tags
conditional, replace



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace Unless Style = Hyperlink? Insert the Hyperlink and hide all sheets except clicking Hyperlink PRADEEPB270 Excel 1 02-22-2013 09:47 AM
Hyperlink/Data Insert & replace jclinton Word 1 09-19-2012 07:22 PM
Replace Unless Style = Hyperlink? Macro to replace one specific heading style with another ubns Word VBA 44 09-04-2012 08:17 PM
Find bullets and replace with paragraph style? cdybdahl Word 1 12-02-2011 02:14 AM
Find and replace multiple documents change style BaPW Word 0 08-14-2011 11:12 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:36 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft