PDA

View Full Version : Convert Word document comments to Markdown-formatted links



Leighton
12-13-2022, 05:13 AM
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. If I use "ActiveWindow.ActivePane.Close" to close the comments pane, the cursor still remains wherever it was in the document.

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

macropod
12-13-2022, 03:18 PM
Cross-posted at: Converting URLs in comments to Markdown-formatted links | Microsoft Office Forums (office-forums.com) (https://www.office-forums.com/threads/converting-urls-in-comments-to-markdown-formatted-links.2351592/)
Please read VBA Express' Cross-Posting requirements in Rule 3: http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item3

Leighton
10-03-2023, 11:38 PM
Update: I've mostly succeeded in extracting URLs from comments. However, I now have a two-level challenge -- footnotes/endnotes that have hyperlinked text within them. I can extract the text of the reference, but I lose the link URL. I can convert all hyperlinks in the document to Markdown, but those in foot/endnotes are ignored. Bottom line, what I would like is to get all reference URLs out of wherever they are (footnote, endnote, comment) and in whatever form (bare URLs or hyperlinks) and convert them to Markdown in the document body. Thanks in advance for any suggestions that anyone might have. .