gpt4 book ai didi

excel - VBA TypeText Word Wrapping 内联与前一行的缩进

转载 作者:行者123 更新时间:2023-12-04 21:45:20 26 4
gpt4 key购买 nike

我在 Excel 中有一个 VBA 脚本,它在 MS Word 中生成一个简单的报告。它遍历一个二维数组,并将每一行打印为一条线(带有一些转换和检查)。
我的问题是:每行以 2 个制表符开头,每当一行太长而无法容纳时,它就会换到下一行。这个新行从页面的开头开始,但是,我需要与上一行对齐并从 2 个选项卡开始。
这是代码:

Sub Macro()

Dim i As Long
Dim szLine As String
szLine = "EXAMPLE STRING DATA, EXAMPLE STRING DATA, EXAMPLE STRING DATA, EXAMPLE STRING DATA"

'Header information
Selection.TypeText Text:="1. PARA 1"
Selection.TypeText Text:=vbCrLf
Selection.TypeText Text:=vbTab & "(a) SUB PARA"
Selection.TypeText Text:=vbCrLf

For i = 1 To 5
'Create String Line to Print
szLine = "EXAMPLE STRING DATA, EXAMPLE STRING DATA, EXAMPLE STRING DATA, EXAMPLE STRING DATA"
szLine = vbTab & vbTab & i & "." & vbTab & szLine

'Print
Selection.TypeText Text:=szLine
Selection.TypeText Text:=vbCrLf
Next i
End Sub
Here is an image showing an example of the document as is.
And here is how I need the document to be formatted
我目前的解决方案是检测当前行是否高于或低于某个阈值字符数,如果是,则插入换行符,然后在字符串中插入制表符。
这是此解决方案的代码:
Public Function FixTabs(sz As String) As String

FixTabs = "NULL"

Dim numTabs As Long
Dim numSplit As Long
Dim sepSplit As String

Dim szReturn As String
Dim i As Long
Dim j As Long
Dim hasSplit As Boolean
Dim charLimit As Long

numTabs = 2
numSplit = 3
sepSplit = ","

'Determined by trial and error. Tabs are 5 chars long-ish
charLimit = (numTabs * 5) + 10

'Return the string as is if its not too big
If LenB(sz) < charLimit Then
MsgBox LenB(sz)
FixTabs = sz
Exit Function
End If

'Otherwise iterate through, and split the line by the sep char.
hasSplit = False
szReturn = ""
j = 0
For i = 1 To LenB(sz)

'Build the return string
szReturn = szReturn & Mid(sz, i, 1)

If Mid(sz, i, 1) = sepSplit Then
'Seperator found
j = j + 1
Else
'Check when to insert the new line
If j >= numSplit And hasSplit = False Then
hasSplit = True 'Only do this once.
szReturn = szReturn & vbCrLf & vbTab & vbTab 'Add newline and more tabs
End If
End If
Next i

FixTabs = szReturn

End Function
如您所见,这不太理想。它仅在 90% 的时间内有效,因为实际数据不太一致,字符并不总是相同。
我的限制是:
  • 我必须使用 Times new Roman(无单行距字体)
  • 我无法使用文字项目符号功能

  • 编辑:
    .Paragraphs.LeftIndent = 72
    是我最终使用的。不完美,但似乎正是我所需要的是不可能的。

    最佳答案

    您确实需要花一些时间学习 Word 工作原理的基础知识。使用选项卡等蛮力布局是非常糟糕的做法。
    学习使用段落缩进(第一行,悬挂,左右)而不是制表符,更好的是段落样式 - 因为与 Word 中的段落格式有关的一切都基于段落样式。
    此外,您需要学习使用 Word 模板。使用模板,您的所有样板文本、基本格式和段落样式都可以就位,因此您只需在相关位置插入相应的内容。
    最后,学习如何使用 Range 对象。使用选择效率低且容易出错。
    为了让您了解可能的情况,请在新的空文档中运行以下宏。这个宏只需要一次来配置模板。

    Sub ApplyMultiLevelHeadingNumbers()
    Dim LT As ListTemplate, i As Long
    Set LT = ActiveDocument.ListTemplates.Add(OutlineNumbered:=True)
    For i = 1 To 3
    With LT.ListLevels(i)
    .NumberFormat = Choose(i, "%1.", "(%2)", "%3.")
    .TrailingCharacter = wdTrailingTab
    .NumberStyle = Choose(i, wdListNumberStyleArabic, wdListNumberStyleLowercaseLetter, _
    wdListNumberStyleArabic)
    .NumberPosition = 0
    .Alignment = wdListLevelAlignLeft
    .TextPosition = InchesToPoints(0.5 + i * 0.5)
    .ResetOnHigher = True
    .StartAt = 1
    .LinkedStyle = "Heading " & i
    End With
    With ActiveDocument.Styles("Heading " & i)
    .ParagraphFormat.LeftIndent = InchesToPoints(i * 0.5 - 0.5)
    .ParagraphFormat.FirstLineIndent = InchesToPoints(-0.5)
    .ParagraphFormat.Alignment = wdAlignParagraphLeft
    .Font.Name = "Times New Roman"
    .Font.Bold = False
    End With
    Next
    End Sub
    然后运行以下宏:
    Sub Demo()
    Dim Rng As Range
    With ActiveDocument
    Set Rng = .Range(0, 0)
    With Rng
    .Text = "Para 1" & vbCr
    .Paragraphs(1).Range.Style = wdStyleHeading1
    .Start = .Paragraphs(1).Range.End
    .Text = "Sub Para" & vbCr
    .Paragraphs(1).Range.Style = wdStyleHeading2
    .Start = .Paragraphs(1).Range.End
    .Text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa." & vbCr & _
    "Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna." & vbCr & _
    "Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus." & vbCr & _
    "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci. Aenean nec lorem." & vbCr
    .Style = wdStyleHeading3
    .Start = .Paragraphs(4).Range.End
    .Text = "Sub Para" & vbCr
    .Paragraphs(1).Range.Style = wdStyleHeading2
    .Start = .Paragraphs(1).Range.End
    .Text = "In porttitor. Donec laoreet nonummy augue. Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc." & vbCr & _
    "Mauris eget neque at sem venenatis eleifend. Ut nonummy. Fusce aliquet pede non pede. Suspendisse dapibus lorem pellentesque magna. Integer nulla." & vbCr & _
    "Donec blandit feugiat ligula. Donec hendrerit, felis et imperdiet euismod, purus ipsum pretium metus, in lacinia nulla nisl eget sapien." & _
    "Donec ut est in lectus consequat consequat. Etiam eget dui. Aliquam erat volutpat." & vbCr & _
    "Sed at lorem in nunc porta tristique. Proin nec augue. Quisque aliquam tempor magna. " & _
    "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nunc ac magna." & vbCr
    .Style = wdStyleHeading3
    .Start = .Paragraphs(4).Range.End
    .Text = "Para 2" & vbCr
    .Paragraphs(1).Range.Style = wdStyleHeading1
    .Start = .Paragraphs(1).Range.End
    .Text = "Sub Para" & vbCr
    .Paragraphs(1).Range.Style = wdStyleHeading2
    .Start = .Paragraphs(1).Range.End
    .Start = .Paragraphs(1).Range.End
    .Text = "Maecenas odio dolor, vulputate vel, auctor ac, accumsan id, felis. Pellentesque cursus sagittis felis." & vbCr
    .Style = wdStyleHeading3
    End With
    End With
    End Sub
    正如您将看到的,输出的格式与您描述的一样,数据中没有一个选项卡。

    关于excel - VBA TypeText Word Wrapping 内联与前一行的缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67643750/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com