如何使用Excel VBA更改Word文档中的文本方向

问题描述 投票:0回答:3

我在 Excel VBA 中创建了一个程序,用于将 Word 文档内容复制到邮件正文。

我想改变一篇文字的方向。

我尝试过

.ParagraphFormat.Alignment

Sub sendMail()

    Dim ol As Outlook.Application
    Dim olm As Outlook.MailItem
    Dim wd As Word.Application
    Dim doc As Word.Document
    Dim Cell As Range

    For r = 23 To Sheet1.Cells(Rows.Count, 21).End(xlUp).Row

        With wd.Selection.Find
            .Text = "<<Duration>>"
            .Replacement.Text = Sheet1.Cells(r, 32).Value
            .Execute Replace:=wdReplaceAll
        End With
        
        With wd.Selection.Find
            .Text = "<<objectivs>>"
            .ParagraphFormat.Alignment = wdAlignParagraphLeft   'this code suppose to change the direction but it didn't work
            .Replacement.Text = Sheet1.Cells(r, 33).Value
            .Execute Replace:=wdReplaceAll
        End With

    Next

End Sub
excel vba ms-word text-alignment direction
3个回答
0
投票

尝试

.Replacement.ParagraphFormat.Alignment = wdAlignParagraphLeft

0
投票

您的代码正在寻找具有所需对齐方式的段落,而不是设置它。 Selection的使用效率也很低。尝试:

For r = 23 To Sheet1.Cells(Rows.Count, 21).End(xlUp).Row
  With wd.ActiveDocument.Range.Find
    .Text = "<<Duration>>"
    .Replacement.ClearFormatting
    .Replacement.Text = Sheet1.Cells(r, 32).Value
    .Execute Replace:=wdReplaceAll
    .Text = "<<objectivs>>"
    .Replacement.ParagraphFormat.Alignment = wdAlignParagraphLeft
    .Replacement.Text = Sheet1.Cells(r, 33).Value
    .Execute Replace:=wdReplaceAll
  End With
Next

请注意,除非您希望更改除第一个 <> 段落之外的所有段落的格式,否则您需要确保每次迭代都将其清除。


0
投票

我找到了,应该是这样的:

.Replacement.ParagraphFormat.ReadingOrder = wdReadingOrderLtr

© www.soinside.com 2019 - 2024. All rights reserved.