将文本从 Excel 单元格输入到 Word 并保留格式

发布于 2024-12-17 16:39:20 字数 349 浏览 0 评论 0原文

我有以下代码,

strMyFormat1 = oWB.Worksheets(strSelectedSheet).Range(strStemLocation).NumberFormat

WordApp.Selection.TypeText Format(oWB.Worksheets(strSelectedSheet).Range(strStemLocation), strMyFormat1) & vbCr

但它不保留 Excel 单元格的格式

,例如,如果 Excel 单元格为粗体,我希望在输入我的 Word 文档时保留该格式,

我知道它适用于复制和粘贴,但我不想这样做

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

e.g. if the Excel Cell is bold I want that to be retained when entering into my word doc

I know it works with copy and paste but I dont want to do that

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨小沫ゞ 2024-12-24 16:39:20

单元格格式化有很多部分,但这里有一些可以帮助您入门:

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

注意:使用范围对象的 Text 属性来获取格式化版本;使用 Value 获取未格式化的版本。另请注意,范围对象 (rng) 应该是单个单元格。

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.

不气馁 2024-12-24 16:39:20

此示例过程创建一个新的 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

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:

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
半枫 2024-12-24 16:39:20

那么您有一个单元格,并且想要将其内容复制到 Word 文档中?
我会这样做:

Cells(currentRow, currentCol).Copy
.Cell(2,1).Range.PasteAndFormat (wdFormatOriginalFormatting)

这将完全复制文本,因为它原来是..字体,粗体,颜色等。

提醒:
单元格 = Excel 电子表格中的单元格
.Cell = 您想要粘贴到 Word 文档中的单元格

但是假设您想要将字体修改为 Arial 以更好地匹配 WordDoc 的其余部分,但仍然保留原始颜色和粗体等(如果存在)。只需添加

.Cell(2,1)Range.Font.Name ("Arial")

:可以对任何其他字体属性(如大小等)执行相同的操作
关键是在将其复制到 Word 文档后进行这些更改。

希望这可以帮助别人。
-S

So you have a cell and you want to copy its contents into a word Doc?
I'd do:

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

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:

.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.

Hope this helps someone out.
-S

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文