You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Word VBA中复制带格式文档文本到剪贴板?现有代码仅复制纯文本

Fixing Formatted Text Copy in Word VBA

Your current code uses ActiveDocument.Content.Text which only grabs the plain text content of the document—That's why all formatting (bold, underline, etc.) gets lost. To copy the content with its original formatting intact, we can leverage Word's built-in Copy method which handles rich text formatting automatically.

Here's the revised code:

Sub CopyFormattedTextToClipboard()
    ' Copy the entire document content with formatting to clipboard
    ActiveDocument.Content.Copy
End Sub

That's it! This will copy all bold, underlined, styled text (and even things like tables or images if present) directly to your clipboard, preserving every bit of formatting.

If you need to copy a specific range instead of the whole document

If you don't want to copy the entire document, you can target a specific range. For example, to copy the first paragraph:

Sub CopySpecificRangeWithFormatting()
    Dim targetRange As Range
    Set targetRange = ActiveDocument.Paragraphs(1).Range
    ' Remove the trailing paragraph mark if you don't want it included
    targetRange.End = targetRange.End - 1
    targetRange.Copy
End Sub

Why this works better than DataObject

The DataObject you were using is designed for plain text transfers. Word's native Copy method, however, places rich text (and other formats like HTML) onto the clipboard, which is recognized by other apps that support formatted text (like Word itself, Excel, Outlook, etc.).

内容的提问来源于stack exchange,提问作者philipp_chapo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:21:26