PDA

View Full Version : [SOLVED:] Help inserting selected text from a variable into a string



aharown18
01-29-2018, 01:17 PM
I'm working on a macro that is intended to go like this:
* User selects a plain text URL
* User launches macro
* Macro inserts html link tag and other stuff in front of the selected text, copies the plain text url as the link text, adds a closing tag at the end.

(Yes, I know this is kind of a dumb thing to do to a URL in MS Word... but for long, complicated, boring reasons I don't want to go into, we need to keep it all in plain text and then copy the plain text into a database... which will turn the plain text -- really, html code at that point -- into a working link. Yes, we're pretending it's still 1995! )

What isn't working: I have the selection loading into a variable, but then when I go to insert it back into my string in the right place, the variable seems to have changed. Tested the variable using MsgBox. Pretty sure the change as to do with my use of Selection.InsertBefore later in the macro. So I need to find a different way to do a couple of things it seems.

But with VBA, I mostly copy-paste, so... don't really know what it all does or how to fix it.

Here's my macro so far...



Sub LinkMaker()'
' LinkMaker Macro
' Format plain text URL to be a live link in the database.
'
Dim sText As Range
Set sText = Selection.Range
MsgBox sText
If Selection.Type = wdSelectionIP Then
Selection.InsertBefore "<a href=\"""
Selection.InsertAfter """ target=""_blank"" class=""CurrentAffairsLink"">" & sText & "</a>"
Selection.MoveStart Unit:=wdCharacter, Count:=1
Selection.Collapse Direction:=wdCollapseStart
Else
' Shrinks selection to exclude leading or trailing spaces
' Also excludes trailing paragraph break
While Selection.Characters.First = " "
Selection.MoveStart Unit:=wdCharacter, Count:=1
Wend
While Selection.Characters.Last = " " Or Selection.Characters.Last = vbNewLine
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
Wend
Selection.InsertBefore "<a href="""
Selection.InsertAfter """ target=""_blank"" class=""CurrentAffairsLink"">" & sText & "</a>"
' shed any character style acquired from selection
Selection.Characters.First.Font.Reset
Selection.Characters.Last.Font.Reset
End If
End Sub

macropod
01-29-2018, 11:45 PM
Try:

Sub LinkMaker() '
' LinkMaker Macro
' Format plain text URL to be a live link in the database.
With Selection
.MoveStartWhile " ", wdForward
.MoveEndWhile " " & vbTab & vbCr & Chr(11), wdBackward
.Text = "<a href=\"""""" target=""_blank"" class=""CurrentAffairsLink"">" & .Range.Text & "</a>"
.Font.Reset
End With
End Sub

aharown18
02-02-2018, 12:35 PM
I haven't had a chance to try this yet, but should get back to it next week some time. Just wanted to say thanks for the time being.

aharown18
03-12-2018, 09:27 AM
Took much longer than expected to get back to this, but the code above is not working for me. Using a sample URL of
http ://samplelink.com/, if I select it and run the macro I get this result:

<a href=\""" target="_blank" class="CurrentAffairsLink">http ://samplelink.com/</a>

The result I'm trying to get would be:

<a href="http :samplelink.com/" target="_blank" class="CurrentAffairsLink">http ://samplelink.com/</a>
(I had to alter this so it would post... since I'm not allowed to post anything the forum software thinks is a link. So the space after http is not actually in my code.)

aharown18
03-12-2018, 10:25 AM
The following mostly works. I say mostly, because it's supposed to exclude end of line/paragraph return and that part is not working yet. But if the selection does not include the paragraph symbol, it works.

It was apparent that the variable sText was changing value when the selection range itself changed later in the script. So, what if we could set it in a way that would not change? I'm sure there's a better way to do it, but what I have working is a second variable that captures the initial selection value and keeps it.


Sub LinkMaker()'
' LinkMaker Macro
' Format plain text URL to be a live link in the database.
'
Dim sText As Range
Dim oText As String
Set sText = Selection.Range
oText = sText
MsgBox sText
If Selection.Type = wdSelectionIP Then
Selection.InsertBefore "<a href=\"""
Selection.InsertAfter """ target=""_blank"" class=""CurrentAffairsLink"">" & sText & "</a>"
Selection.MoveStart Unit:=wdCharacter, Count:=1
Selection.Collapse Direction:=wdCollapseStart
Else
' Shrinks selection to exclude leading or trailing spaces
' Also excludes trailing paragraph break
While Selection.Characters.First = " "
Selection.MoveStart Unit:=wdCharacter, Count:=1
Wend
While Selection.Characters.Last = " " Or Selection.Characters.Last = vbNewLine
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
Wend
Selection.InsertBefore "<a href="""
Selection.InsertAfter """ target=""_blank"" class=""CurrentAffairsLink"">" & oText & "</a>"
' Selection.InsertAfter sText
' shed any character style acquired from selection
Selection.Characters.First.Font.Reset
Selection.Characters.Last.Font.Reset
End If
End Sub

aharown18
03-12-2018, 12:35 PM
Eureka!

Here's a fully functional (as far as original objective) cleaned up version. The selection adjustment lines were not in the right place to work as intended, but are now. Just passing this on for whoever else might find it useful in the future.

Sub LinkMaker()'
' LinkMaker Macro
' Format plain text URL to be a live link in the database.
'
Dim sText As Range
Dim oText As String
If Selection.Characters.First = " " Then
Selection.MoveStart Count:=1
End If
If Selection.Characters.Last = " " Then
' MsgBox "There is a space. Unselecting the space"
Selection.MoveEnd Count:=-1
ElseIf Selection.Characters.Last = vbNewLine Then
' MsgBox "There is a return. Unselecting the return."
Selection.MoveEnd Count:=-1
End If
Set sText = Selection.Range
oText = sText
Selection.InsertBefore "<a href="""
Selection.InsertAfter """ target=""_blank"" class=""CurrentAffairsLink"">" & oText & "</a>"
End Sub

macropod
03-12-2018, 03:11 PM
I don't know what when wrong with the code I posted - that long """""" string is missing some content. The code should be:

Sub LinkMaker() '
' LinkMaker Macro
' Format plain text URL to be a live link in the database.
With Selection
.MoveStartWhile " ", wdForward
.MoveEndWhile " " & vbTab & vbCr & Chr(11), wdBackward
.Text = "<a href=\""" & .Range.Text & """ target=""_blank"" class=""CurrentAffairsLink"">" & .Range.Text & "</a>"
.Font.Reset
End With
End Sub