Converting URLs in comments to Markdown-formatted links

Joined
Dec 13, 2022
Messages
2
Reaction score
0
Hi all. I run an editorial website that uses the Markdown formatting language within the CMS. For example, [this is a hyperlink](https://link.url). We get perhaps half of the articles we publish as Word documents, and links come in a range of formats -- classic hyperlinks, but also footnotes, endnotes, and comments. I have written macros that very capably convert the first three cases to Markdown-formatted links within the text, but am having trouble with converting comments that contain URLs to Markdown links.

Below is a mix of code and pseudocode to give you an idea of what I'm looking for:

' switch links comments to text if there's an http within
For Each oComment In ActiveDocument.Comments
If InStr(oComment.Range.Text, cHttp) > 1 Or Left(oComment.Range.Text, 4) = cHttp Then
{select the text that was commented; we should be in the document text itself, not the comments}
Selection.TypeText "[" & Selection.Text & "](" & oComment.Range.Text & ")"
End If
oComment.Delete
Next

Aspects of the above work, but I run into problems with selecting the actual text, not the comment. For example, "oComment.Range.Select" will highlight where the comment is in the document, but doesn't actually move the cursor to that point. If I use "ActiveWindow.ActivePane.Close" to close the comments pane, the cursor still remains wherever it was in the document. This causes problems when I insert the link text + link URL, as it's in the wrong place.

Thanks for any suggestions you might have. I'm sure that I'm just missing a single line or two of code. Endless Internet searches haven't revealed much, so here I am. Fingers crossed...
 
Joined
Dec 13, 2022
Messages
2
Reaction score
0
Absent any collective insight, I kept digging and turned up https://answers.microsoft.com/en-us...ain-text/4d5a9ec9-1a55-4a6f-aa66-369ca7aa2c45. It's not exactly what I had in mind (the comment text is inserted after the anchor), but with some jiggering it more or less works.

For Each oComment In ActiveDocument.Comments
' if there's an https within the comment, or at its start
If InStr(oComment.Range.Text, cHttp) > 1 Or Left(oComment.Range.Text, 4) = cHttp Then
oComment.Reference.Select
CommentLink = Alltrim(oComment.Range.Text)
' if the http isn't right to the left, whack off what's before
If InStr(CommentLink, cHttp) > 1 Then
CommentLink = Right(CommentLink, Len(CommentLink) - (InStr(CommentLink, cHttp) - 1))
End If
' if there's a space, whack off what's after
If InStr(CommentLink, cSpace) > 1 Then
CommentLink = Left(CommentLink, (InStr(CommentLink, cSpace) - 1))
End If
' insert remaining link surrounded by spaces
Selection.Text = cSpace & cLinkText & cParenL & CommentLink & cParenR & cSpace ' " [link text]("
Selection.ClearFormatting
End If
Selection.Collapse wdCollapseEnd
oComment.Delete
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top