用途:OpenOffice 宏将
### heading **bold** *italics* and list
以及诸如 - 第一 - 第二个 Markdown 之类的项目转换为 OpenOffice 样式文本
以下崩溃与 OBJECT VARIABLE NOT SET at
ErrorHandler:
MsgBox "An error occurred: " & Err.Description
手写时
**bold**
降价。
有什么建议吗?
下面的宏”
Sub convertMarkdown()
On Error GoTo ErrorHandler ' Global error handling
MsgBox "convertMarkdown() started."
' Get the current document
Dim oDoc As Object
oDoc = ThisComponent
' Check if the document is text (Writer)
If oDoc.SupportsService("com.sun.star.text.TextDocument") Then
' Get the text content of the document
Dim oText As Object
oText = oDoc.Text
' Check if oText is properly initialized
If IsNull(oText) Then
MsgBox "oText is not initialized."
Exit Sub
End If
' Create an enumeration for paragraphs
Dim oParagraphs As Object
oParagraphs = oText.createEnumeration()
' Process each paragraph
While oParagraphs.hasMoreElements()
Dim oParagraph As Object
oParagraph = oParagraphs.nextElement()
Dim paragraphText As String
paragraphText = Trim(oParagraph.getString()) ' Trim spaces
' Check if oParagraph is properly initialized
If IsNull(oParagraph) Then
MsgBox "oParagraph is not initialized."
Exit Sub
End If
' Handle ### headings
If Left(paragraphText, 3) = "###" Then
MsgBox "### found"
oParagraph.ParaStyleName = "Heading 1"
oParagraph.setString Mid(paragraphText, 4) ' Remove ###
End If
' Handle **bold** text
Dim startBold As Long
startBold = InStr(paragraphText, "**")
While startBold > 0
Dim endBold As Long
endBold = InStr(startBold + 2, paragraphText, "**")
If endBold > 0 Then
MsgBox "** found"
Dim boldText As String
boldText = Mid(paragraphText, startBold + 2, endBold - startBold - 2)
paragraphText = Replace(paragraphText, "**" & boldText & "**", boldText)
oParagraph.setString(paragraphText)
' Create a new cursor for the bold text
Dim oBoldCursor As Object
oBoldCursor = oText.createTextCursorByRange(oParagraph)
' Check if oBoldCursor is created successfully
If Not IsNull(oBoldCursor) Then
oBoldCursor.gotoRange(oParagraph, False)
oBoldCursor.goRight(startBold, False) ' Move to the start of the bold text
oBoldCursor.goRight(Len(boldText), True) ' Move to the end of the bold text
oBoldCursor.CharWeight = com.sun.star.awt.FontWeight.BOLD
Else
MsgBox "Failed to create bold cursor."
End If
End If
startBold = InStr(startBold + 1, paragraphText, "**") ' Move to next bold
Wend
' Handle *italics* text
Dim startItalics As Long
startItalics = InStr(paragraphText, "*")
While startItalics > 0
Dim endItalics As Long
endItalics = InStr(startItalics + 1, paragraphText, "*")
If endItalics > 0 Then
MsgBox "* found"
Dim italicsText As String
italicsText = Mid(paragraphText, startItalics + 1, endItalics - startItalics - 1)
paragraphText = Replace(paragraphText, "*" & italicsText & "*", italicsText)
oParagraph.setString(paragraphText)
' Create a new cursor for the italic text
Dim oItalicsCursor As Object
oItalicsCursor = oText.createTextCursorByRange(oParagraph)
' Check if oItalicsCursor is created successfully
If Not IsNull(oItalicsCursor) Then
oItalicsCursor.gotoRange(oParagraph, False)
oItalicsCursor.goRight(startItalics, False) ' Move to the start of the italic text
oItalicsCursor.goRight(Len(italicsText), True) ' Move to the end of the italic text
oItalicsCursor.CharPosture = com.sun.star.awt.FontSlant.ITALIC
Else
MsgBox "Failed to create italic cursor."
End If
End If
startItalics = InStr(startItalics + 1, paragraphText, "*") ' Move to next italics
Wend
' Handle bullet points (-)
If Left(paragraphText, 2) = "- " Then
MsgBox "- found"
oParagraph.setString Mid(paragraphText, 3) ' Remove - and space
' Apply bullet formatting to the modified paragraph
oParagraph.NumberingStyleName = "Numbering 1"
End If
Wend ' End of While for paragraphs
MsgBox "convertMarkdown() ended."
Else
MsgBox "This is not a Writer document."
End If
Exit Sub ' Ensure error handling is exited properly
ErrorHandler:
MsgBox "An error occurred: " & Err.Description
Resume Next
End Sub
我放弃了为 OpenOffice 编写宏,而 chatGPT 极大地帮助编写了一个 VBA 宏,以便与 Word 一起使用,以处理将其 Markdown 编码响应转换为 OpenOffice 样式。它将 Markdown 转换为粗体、斜体、下划线、1 2 3 级标题、项目符号列表和编号列表。
最初使用正则表达式是复杂且有缺陷的。转换为使用字符串搜索、替换功能可以快速使其工作,并且更易于阅读和维护。
以下是有关如何将宏添加到 Word 的简洁说明:
打开 Word:启动 Microsoft Word。
打开 VBA 编辑器:
Alt + F11
打开 Visual Basic for Applications (VBA) 编辑器。插入新模块:
Normal
(或活动文档的名称)。复制并粘贴宏:
Sub ApplyChatGPTMarkupStyles_WithNumberedLists()
Dim rng As Range
Dim paragraph As Paragraph
Dim strText As String
Dim startPos As Long, endPos As Long
' Loop through each paragraph
For Each paragraph In ActiveDocument.Paragraphs
Set rng = paragraph.Range
strText = rng.Text
' Handle headings
If Left(strText, 3) = "###" Then
rng.Text = Mid(strText, 4) ' Remove ###
rng.Style = ActiveDocument.Styles("Heading 3")
ElseIf Left(strText, 2) = "##" Then
rng.Text = Mid(strText, 3) ' Remove ##
rng.Style = ActiveDocument.Styles("Heading 2")
ElseIf Left(strText, 1) = "#" Then
rng.Text = Mid(strText, 2) ' Remove #
rng.Style = ActiveDocument.Styles("Heading 1")
End If
' Handle **bold** (multiple instances)
Do While InStr(strText, "**") > 0
startPos = InStr(strText, "**")
endPos = InStr(startPos + 2, strText, "**")
If endPos > startPos Then
Set rng = paragraph.Range
rng.SetRange paragraph.Range.Start + startPos - 1, paragraph.Range.Start + endPos + 1
rng.Font.Bold = True
rng.Text = Replace(rng.Text, "**", "")
End If
strText = paragraph.Range.Text
Loop
' Handle *italic* (multiple instances)
Do While InStr(strText, "*") > 0
startPos = InStr(strText, "*")
endPos = InStr(startPos + 1, strText, "*")
If endPos > startPos Then
Set rng = paragraph.Range
rng.SetRange paragraph.Range.Start + startPos - 1, paragraph.Range.Start + endPos
rng.Font.Italic = True
rng.Text = Replace(rng.Text, "*", "")
End If
strText = paragraph.Range.Text
Loop
' Handle _underline_ (multiple instances)
Do While InStr(strText, "_") > 0
startPos = InStr(strText, "_")
endPos = InStr(startPos + 1, strText, "_")
If endPos > startPos Then
Set rng = paragraph.Range
rng.SetRange paragraph.Range.Start + startPos - 1, paragraph.Range.Start + endPos
rng.Font.Underline = wdUnderlineSingle
rng.Text = Replace(rng.Text, "_", "")
End If
strText = paragraph.Range.Text
Loop
' Handle bullet points: - item
If Left(strText, 2) = "- " Then
rng.Text = Mid(strText, 3) ' Remove the dash
rng.ListFormat.ApplyBulletDefault
End If
' Numbered lists: 1. item
If IsNumeric(Left(strText, 1)) And Mid(strText, 2, 1) = "." Then
rng.Text = Mid(strText, 3) ' Remove the number and period
rng.ListFormat.ApplyNumberDefault
End If
Next paragraph
End Sub
保存您的宏:
Ctrl + S
或单击保存图标以保存更改。关闭 VBA 编辑器:
X
或按 Alt + Q
关闭 VBA 编辑器。运行宏:
Alt + F8
打开“宏”对话框。ApplyChatGPTMarkupStyles_WithNumberedLists
,然后单击 运行。现在宏应该根据指定的标记格式化文档中的文本。