如何在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

