daily pastebin goal
31%
SHARE
TWEET

replace text and link to html tag a href (MS word)

tolikpunkoff Jan 11th, 2018 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub InsertLink()
  2.  
  3. 'data format:
  4. 'Link text [http://example.com/example/example.html]
  5. 'Result:
  6. '<b><a href="http://example.com/example/example.html" target="_blank">Link text</a></b>
  7. 'Select text & run macros
  8.  
  9.     Dim LinkTemplate As String
  10.     Dim LinkAddr As String
  11.     Dim LinkText As String
  12.     Dim LinkOut As String
  13.     Dim LinkStart As Integer
  14.     Dim LinkEnd As Integer
  15.    
  16.     LinkTemplate = "<b><a href=""%addr%"" target=""_blank"">%text%</a></b>"
  17.     LinkStart = InStr(1, Selection.Text, "[")
  18.     LinkEnd = InStr(1, Selection.Text, "]")
  19.     If (LinkStart = 0) Or (LinkEnd = 0) Then
  20.         MsgBox "No Link :("
  21.         Exit Sub
  22.     End If
  23.    
  24.     LinkAddr = Trim$(Mid$(Selection.Text, LinkStart + 1, LinkEnd - LinkStart - 1))
  25.     LinkText = Trim$(Mid$(Selection.Text, 1, LinkStart - 1))
  26.    
  27.     LinkOut = Replace(LinkTemplate, "%addr%", LinkAddr)
  28.     LinkOut = Replace(LinkOut, "%text%", LinkText)
  29.    
  30.     Selection.Text = LinkOut
  31.     Selection.MoveRight Unit:=wdCharacter, Count:=1
  32.  
  33. End Sub
RAW Paste Data
Top