Vba 使用Excel将文本缩进到Word宏

Vba 使用Excel将文本缩进到Word宏,vba,excel,macros,ms-word,Vba,Excel,Macros,Ms Word,我有一个Excel到Word宏,指定如下: Sub CopyToWordDoc() Dim objWord Dim objDoc Dim objSel Dim sht As Worksheet Dim p As Integer Set objWord = CreateObject("Word.Application") 'open new word document Set objDoc = objWord.Documents.Add Set objSel = objWord.Select

我有一个Excel到Word宏,指定如下:

Sub CopyToWordDoc()


Dim objWord
Dim objDoc
Dim objSel
Dim sht As Worksheet
Dim p As Integer

Set objWord = CreateObject("Word.Application") 'open new word document
Set objDoc = objWord.Documents.Add
Set objSel = objWord.Selection

objWord.Visible = True

For x = 1 To Worksheets.Count - 1 'loop through data sheets and export contents to Word
    On Error Resume Next
    Set sht = Sheets("X" & x)
    On Error GoTo 0
    If sht Is Nothing Then Exit Sub

    With sht
        If x = 1 Then 'add version, date, userinfo, projectinfo etc. to first page of Word
            objSel.Style = objDoc.Styles("Heading 1")
            objSel.TypeText (Range("Client").Value2)
            objSel.TypeParagraph

            objSel.Style = objDoc.Styles("Heading 1")
            objSel.TypeText ("Scope of Tax Due Diligence")
            objSel.TypeParagraph
            objSel.Style = objDoc.Styles("Normal")
            objSel.TypeText ("Review Period: " & Range("Period").Value2)
            objSel.TypeParagraph

            If .Range("C3").Value2 = True Then 'check if Level 1 titel has to be added
                objSel.Style = objDoc.Styles("Heading 2")
                objSel.TypeText (.Range("B2").Value2)
                objSel.TypeParagraph
            Else
                p = 1
            End If
        Else
            If p = 1 And .Range("C3").Value2 = True Then
                objSel.Style = objDoc.Styles("Heading 2")
                objSel.TypeText (.Range("B2").Value2)
                objSel.TypeParagraph
                p = 0
            ElseIf p = 0 And .Range("C3").Value2 = True Then
                If .Range("B2").Value2 <> Sheets("X" & x - 1).Range("B2").Value2 Then
                    objSel.Style = objDoc.Styles("Heading 2")
                    objSel.TypeText (.Range("B2").Value2)
                    objSel.TypeParagraph
                End If
            ElseIf p = 0 And .Range("C3").Value2 = False Then
                If .Range("B2").Value2 <> Sheets("X" & x - 1).Range("B2").Value2 Then p = 1
            End If
        End If

        objWord.Run MacroName:="sdWordEngine.mBulletsAndNumbering.FormatBulletDefault"

        If .Range("C3").Value2 = True Then 'add level 2 title
            objSel.Style = objDoc.Styles("Heading 3")
            objSel.TypeText (.Range("B3").Value2)
            objSel.TypeParagraph
        End If

        objWord.Run MacroName:="sdWordEngine.mBulletsAndNumbering.FormatBulletDefault"



        For y = 4 To Application.WorksheetFunction.CountA(.Range("B1:B50")) 'loop through data sheet and add info if in scope

            If .Range("C" & y).Value2 = True Then
                If .Range("A" & y).Value2 = 3 Then
                    objSel.Range.SetListLevel Level:=1
                    objSel.TypeText (.Range("B" & y).Value2)
                    objSel.TypeParagraph
                Else
                    objSel.Range.SetListLevel Level:=2
                    objSel.TypeText (.Range("B" & y).Value2)
                    objSel.TypeParagraph
                End If
            End If
        Next
    End With
Next

objWord.Run MacroName:="sdWordEngine.mBulletsAndNumbering.FormatBulletDefault"
objSel.InsertBreak Type:=wdPageBreak

For x = 1 To Worksheets.Count - 1 'same as above but for info request instead
    On Error Resume Next
    Set sht = Sheets("X" & x)
    On Error GoTo 0
    If sht Is Nothing Then Exit Sub

    With sht
        If x = 1 Then
            objSel.Style = objDoc.Styles("Heading 1")
            objSel.TypeText ("Information Request for Tax Due Diligence")
            objSel.TypeParagraph

            If .Range("C3").Value2 = True Then
                objSel.Style = objDoc.Styles("Heading 2")
                objSel.TypeText (.Range("B2").Value2)
                objSel.TypeParagraph
            Else
                p = 1
            End If
        Else
            If p = 1 And .Range("C3").Value2 = True Then
                objSel.Style = objDoc.Styles("Heading 2")
                objSel.TypeText (.Range("B2").Value2)
                objSel.TypeParagraph
                p = 0
            ElseIf p = 0 And .Range("C3").Value2 = True Then
                If .Range("B2").Value2 <> Sheets("X" & x - 1).Range("B2").Value2 Then
                    objSel.Style = objDoc.Styles("Heading 2")
                    objSel.TypeText (.Range("B2").Value2)
                    objSel.TypeParagraph
                End If
            ElseIf p = 0 And .Range("C3").Value2 = False Then
                If .Range("B2").Value2 <> Sheets("X" & x - 1).Range("B2").Value2 Then p = 1
            End If
        End If

        objWord.Run MacroName:="sdWordEngine.mBulletsAndNumbering.FormatBulletDefault"

        If .Range("C3").Value2 = True And Application.WorksheetFunction.CountIf(.Range("G2:G50"), True) <> 0 Then
            objSel.Style = objDoc.Styles("Heading 3")
            objSel.TypeText (.Range("B3").Value2)
            objSel.TypeParagraph
        End If

        objWord.Run MacroName:="sdWordEngine.mBulletsAndNumbering.FormatBulletDefault"

        For y = 2 To Application.WorksheetFunction.CountA(.Range("F1:F50"))
           If .Range("C3").Value2 = True Then
                If .Range("G" & y).Value2 = True Then
                    If .Range("E" & y).Value2 = 1 Then
                        objSel.Range.SetListLevel Level:=1
                        objSel.TypeText (.Range("F" & y).Value2)
                        objSel.TypeParagraph
                    Else
                        objSel.Range.SetListLevel Level:=2
                        objSel.TypeText (.Range("F" & y).Value2)
                        objSel.TypeParagraph
                    End If
                End If
            End If
        Next
    End With
Next

objSel.TypeBackspace
objSel.WholeStory
objSel.Font.Name = "Arial"

End Sub
当我编译Word文档时,它设置了左缩进72的4级行。但是,只有从第二个level 4行开始,它才会以这种方式格式化这些行。它总是省略第一个level 4行(保持它没有左缩进)。有人知道为什么吗?非常感谢你的帮助

那么:

Selection.Paragraphs.LeftIndent = 72
更改72以符合您的需要。 选择整个文档,然后将该行代码放在后面


哇,这真是太多需要理解的代码了。您能否将代码示例减少到word文档中“生成4级文本”的行数?非常感谢!如果我只想让第四级变成左缩进,我该怎么做呢?请把我的答案投上一票,因为它回答了你的问题。你说的“四级”是什么意思?
Selection.Paragraphs.LeftIndent = 72