vba 将文本从 Excel 单元格输入到 Word 并保留格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8245945/
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
Enter Text From Excel cells to Word and retain the formatting
提问by Rob
I have the fllowing code
我有流动的代码
strMyFormat1 = oWB.Worksheets(strSelectedSheet).Range(strStemLocation).NumberFormat
WordApp.Selection.TypeText Format(oWB.Worksheets(strSelectedSheet).Range(strStemLocation), strMyFormat1) & vbCr
But it does not retain the format of the Excel Cell
但它不保留 Excel Cell 的格式
e.g. if the Excel Cell is bold I want that to be retained when entering into my word doc
例如,如果 Excel 单元格是粗体,我希望在输入我的 word 文档时保留它
I know it works with copy and paste but I dont want to do that
我知道它适用于复制和粘贴,但我不想这样做
回答by Rachel Hettinger
There are many parts to cell formatting, but here are a few to get you started:
单元格格式有很多部分,但这里有一些可以帮助您入门:
Dim rng As Excel.Range
Set rng = oWB.Worksheets(strSelectedSheet).Range(strStemLocation)
With Selection
.Text = rng.Text
.Font.Bold = rng.Font.Bold
.Font.Color = rng.Font.Color
End With
Note: use the Text
property of the range object to get the formatted version; use Value
to get the unformatted version. Also note that the range object (rng
) should be a single cell.
注意:使用Text
range对象的属性获取格式化版本;用于Value
获取未格式化的版本。另请注意,范围对象 ( rng
) 应为单个单元格。
回答by Rachel Hettinger
This sample procedure creates a new Word document and adds the contents of each cell in the active Excel worksheet, keeping (some of) the formatting:
此示例过程创建一个新的 Word 文档并添加活动 Excel 工作表中每个单元格的内容,同时保留(部分)格式:
Sub ExportToWord_Example2()
Dim WordApp As Word.Application
Dim doc As Word.Document
Dim rng As Range
Set WordApp = CreateObject("Word.Application")
With WordApp
.Visible = True
Set doc = .Documents.Add
End With
For Each rng In ActiveSheet.UsedRange
With doc.Paragraphs(doc.Paragraphs.Count).Range
.Text = rng.Text
.Font.Bold = rng.Font.Bold
.Font.Color = rng.Font.Color
End With
doc.Range.InsertParagraphAfter
Next rng
End Sub
回答by Catfisch
So you have a cell and you want to copy its contents into a word Doc?
I'd do:
所以你有一个单元格,你想把它的内容复制到一个单词 Doc 中?
我会做:
Cells(currentRow, currentCol).Copy
.Cell(2,1).Range.PasteAndFormat (wdFormatOriginalFormatting)
This will copy the text EXACTLY as it was originally.. font, bold, color etc.
这将完全复制原来的文本......字体、粗体、颜色等。
Reminder:
Cell = the cell in the Excel spreadsheet
.Cell = the cell you want to paste into in the Word document
提醒:
单元格 = Excel 电子表格中的单元格。单元格 =
Word 文档中要粘贴到的单元格
But say you want to modify the font to Arial to better match the rest of your WordDoc but still leave the original colors and bold etc IF any exists.. just add:
但是假设您想将字体修改为 Arial 以更好地匹配 WordDoc 的其余部分,但仍保留原始颜色和粗体等(如果存在).. 只需添加:
.Cell(2,1)Range.Font.Name ("Arial")
You can do the same for any of the other Font attributes like size etc The key is making these changes AFTER it's copied into the Word doc.
您可以对任何其他字体属性(如大小等)执行相同的操作。关键是在将其复制到 Word 文档后进行这些更改。
Hope this helps someone out. -S
希望这可以帮助某人。-S