vba 通过Excel修改Word文档中的行距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9180188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Through Excel modify the line spacing in a Word Document
提问by ichigo
I have this excel file which I have been able to write the data into a Word document through vba for a report. The VBA code is within the excel file containing the data.
我有这个 excel 文件,我已经能够通过 vba 将数据写入 Word 文档以生成报告。VBA 代码位于包含数据的 excel 文件中。
I already have most of the code done and it is working properly, but I can not figure out how to tell word (through the vba code in excel) to change the paragraph spacing. Any advice?
我已经完成了大部分代码并且它工作正常,但是我不知道如何告诉 word(通过 excel 中的 vba 代码)更改段落间距。有什么建议吗?
I am using the following code to write into word from excel(not the full code just an example):
我正在使用以下代码从 excel 写入 word(不是完整代码只是一个示例):
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Set wdApp = New Word.Application
Set wdDoc = wdApp.Documents.Add
wdDoc.Content.InsertAfter "Test"
wdDoc.Content.InsertParagraphAfter
wdDoc.SaveAs (file & "\SpecificReport_" & filter & ".doc")
wdDoc.Close
wdApp.Quit
Set wdDoc = Nothing
Set wdApp = Nothing
回答by kazmone1
Recommendation, but not specific answer. If you have any trouble figuring out how to do something from Excel with Word, i find that recording actions in word and reviewing the code generated usually answers that for me.
推荐,但不是具体的答案。如果您在弄清楚如何使用 Word 从 Excel 执行某些操作时遇到任何问题,我发现在 Word 中记录操作并查看生成的代码通常可以为我解答。
In this specific instance I came up with the following lines of code:
在这个特定的例子中,我想出了以下几行代码:
wdDoc.Content.ParagraphFormat.SpaceBefore = 6
wdDoc.Content.ParagraphFormat.SpaceBeforeAuto = False
wdDoc.Content.ParagraphFormat.SpaceBefore = 6
wdDoc.Content.ParagraphFormat.SpaceBeforeAuto = False
回答by LeasMaps
I'm guessing that you have been asked to change the normal font back to single spacing (pre-word2010). Apart from altering the normal.dotx try this:
我猜你被要求将普通字体改回单行距(word2010 之前)。除了改变 normal.dotx 试试这个:
With The_WordDoc.Styles("Normal").ParagraphFormat
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
End With
The_WordDoc.Styles("Normal").NoSpaceBetweenParagraphsOfSameStyle = _
False
With The_WordDoc.Styles("Normal")
.AutomaticallyUpdate = False
.BaseStyle = ""
.NextParagraphStyle = "Normal"
End With