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
* 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