我编写了以下代码,尝试将粘贴到 Word 中的 ChatGPT 输出格式化为
PasteSpecial
未格式化。
我必须执行双循环,因为随着循环的进行,所有内容都会丢失格式。
我遇到了一个问题,项目符号列表中的最后一项(以“-”开头的文本)丢失了最初正确设置的“列表段落”格式。
Sub FormatChatGPT()
Dim para As Paragraph
Dim txt As String
Dim doc As Document
Set doc = ActiveDocument
' Loop through each paragraph in the document
For Each para In doc.Paragraphs
txt = Trim(para.Range.Text) ' Use Trim to handle any trailing whitespace
' Handle different cases based on the prefix of the paragraph
If Left(txt, 4) = "### " Then
para.Range.Style = "Heading 2" ' Then apply the style
ElseIf Left(txt, 2) = "- " Then
para.Range.Style = doc.Styles("List Paragraph") ' Then apply the style
Else
para.Range.Style = "Normal"
End If
Next para
For Each para In doc.Paragraphs
txt = Trim(para.Range.Text) ' Use Trim to handle any trailing whitespace
' Handle different cases based on the prefix of the paragraph
If Left(txt, 4) = "### " Then
para.Range.Text = Mid(txt, 5) ' Update the text first
ElseIf Left(txt, 2) = "- " Then
para.Range.Text = Mid(txt, 3) ' Update the text first
End If
' Check for bold formatting within **
If InStr(para.Range.Text, "**") > 0 Then
Call FormatBold(para.Range) ' Only call if there are "**" in the text
End If
Next para
End Sub
Sub FormatBold(rng As Range)
Dim iStart As Integer, iEnd As Integer
Dim textLength As Integer
Dim strText As String
Dim txtNew As String
' Refresh the text from the range
strText = rng.Text
textLength = Len(strText)
' Find the first and second occurrences of "**"
iStart = InStr(1, strText, "**")
If iStart > 0 Then
iEnd = InStr(iStart + 2, strText, "**")
End If
' Check if both markers are found
If iStart > 0 And iEnd > 0 Then
' Calculate new text, removing "**" markers
txtNew = Left(strText, iStart - 1) & Mid(strText, iStart + 2, iEnd - iStart - 2) & Mid(strText, iEnd + 2)
' Replace the text in the range with new text without asterisks
rng.Text = txtNew
' Apply bold from the start of the bold text to the end of the bold text within the new string
With rng
.Start = rng.Start + iStart - 1
.End = .Start + (iEnd - iStart - 2)
.Font.Bold = True
End With
' Reset the range to reflect changes
rng.Start = rng.Start
rng.End = rng.Start + Len(txtNew)
End If
End Sub
ChatGPT 输出示例
Here's the revised version of the business plan guide, incorporating the specific questions from the uploaded document into each section, followed by a more general open-ended question for further exploration:
### 1. The Executive Summary
**Introduction:** The Executive Summary is arguably the most crucial part of your business plan. It must capture the essence of your business and make a compelling case to potential investors or partners right from the start. It should succinctly summarize the key points of your business plan, including your business concept, financial features, and strategic goals. It's your opportunity to make a strong first impression and set the stage for the detailed explanations that follow.
- What is the core business idea? - What is the central product or service your business will offer?
- What is your mission statement? - What are the guiding principles and end goals of your business?
- What are the key success factors for your business? - What specific aspects of your business (e.g., technology, expertise, market access) will make it successful?
- What are your financial goals? - How much revenue do you anticipate, and what are your profitability expectations?
- What funding are you seeking? - How much capital do you need to start or expand your business, and how do you plan to use this funding?
- What is the return on investment for potential investors? - What can investors expect in return for their investment?
- Who are the key team members, and what is their expertise? - Briefly introduce the team that will drive the company's success.
- What are the major milestones for your business? - What are the critical steps or stages in the development and growth of your business?
**Open question:** Besides the points listed, what other unique elements of your business could be highlighted in the Executive Summary to attract immediate interest or investment?
### 2. Company Description
**Introduction:** This section offers a deeper dive into what your company does, its legal structure, its business goals, and its place within the industry. It should give stakeholders a clear understanding of the nature of your business and its potential to grow and succeed. Describe your business operations, the markets you serve, and what makes your business unique.
- What is the legal structure of your business? - Are you a sole proprietorship, partnership, corporation, or LLC?
- What is the history of your business? - When was it founded? What inspired its creation? What key milestones have you achieved so far?
- What are the primary products or services your company offers? - What do you sell? How does it benefit your customers?
- What are your business goals? - Short-term and long-term goals for growth, market expansion, product development, etc.
- What are the core values of your business? - What principles guide your business operations and decisions?
- What makes your business unique? - What sets you apart from the competition? Is it your products, your services, your approach, or something else?
- Where is your business located? - Do you have physical premises? How does your location affect your operations?
- Who are the key stakeholders in your business? - This could include owners, management, investors, or other significant parties.
- How does your company fit into the industry? - Positioning within the broader industry landscape.
您的最后一个段落是一个空段落,其格式为“正常”。
当您替换最后一个列表段落的文本时,您还会替换段落符号,这会根据最后一个段落的样式导致错误的样式。
在替换之前调整范围大小,如下所示:
Dim rg As Range
For Each para In doc.Paragraphs
Set rg = para.Range
rg.MoveEnd wdCharacter, -1 'resize range to only return the text with out the para sign
txt = Trim(rg.Text) ' Use Trim to handle any trailing whitespace
' Handle different cases based on the prefix of the paragraph
If Left(txt, 4) = "### " Then
rg.Text = Mid(txt, 5) ' Update the text first
ElseIf Left(txt, 2) = "- " Then
rg.Text = Mid(txt, 3) ' Update the text first
End If