如何通过VBA将Excel指定列数据带格式(粗体/下划线)导出至Word?
Great question! The issue with your current approach is that you're building a plain text string and dumping it into Word all at once—plain text can't carry formatting like bold or underlines. Instead, we need to insert each segment of text individually and apply formatting to the specific parts right after inserting them.
Here's how to modify your code to handle the formatting for column A (bold + underline) and column I (underline only):
Modified VBA Code
Const strSHEET_NAME = "Sheet1" Dim blnNewApp As Boolean Dim wordApp As Object Dim wordDoc As Object Dim j As Long Dim ws As Worksheet On Error Resume Next Set wordApp = GetObject(, "Word.Application") On Error GoTo ErrorHandler If wordApp Is Nothing Then Set wordApp = CreateObject("Word.Application") blnNewApp = True End If Set wordDoc = wordApp.Documents.Add() Set ws = ThisWorkbook.Sheets(strSHEET_NAME) ' Loop through each row For j = 2 To ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Insert Column A content, then apply bold + underline wordApp.Selection.TypeText ws.Cells(j, "A").Text With wordApp.Selection.Font .Bold = True .Underline = 1 ' Equivalent to wdUnderlineSingle for late binding End With ' Insert plain text for the middle segments wordApp.Selection.TypeText ", enlisted " & ws.Cells(j, "B").Text & ", served " & ws.Cells(j, "C").Text & ", " & ws.Cells(j, "D").Text & ", " & ws.Cells(j, "E").Text ' Insert Column I content, then apply underline only wordApp.Selection.TypeText ", " & ws.Cells(j, "I").Text With wordApp.Selection.Font .Bold = False .Underline = 1 ' Single underline End With ' Add any remaining plain text segments here following the same pattern ' wordApp.Selection.TypeText ", [additional column content]" ' Add a new paragraph after each row's content wordApp.Selection.TypeParagraph Next j wordApp.Visible = True AppActivate wordApp.Caption Cleanup: Set ws = Nothing Set wordDoc = Nothing Set wordApp = Nothing Exit Sub ErrorHandler: MsgBox "Error: " & Err.Description, vbCritical Resume Cleanup
Key Changes Explained
- Ditched plain text strings: Instead of building one big unformatted string, we insert each content segment directly into Word. This lets us tweak formatting immediately after inserting the text we want to style.
- Targeted formatting with
Selection.Font: Right after inserting column A's text, we enable bold and single underline. For column I, we only enable underline (and explicitly turn off bold to avoid carryover from column A). - Late binding compatibility: Since you're using
CreateObject(no Word library reference), we use the numeric value1instead of the named constantwdUnderlineSingle—this keeps the code portable across machines. - Robust cleanup & error handling: Added proper object cleanup to prevent orphaned Word processes, plus error handling to catch and report issues gracefully.
Pro Tip for Scalability
If you have multiple columns that need formatting, consider wrapping the "insert text + apply format" logic into a small helper subroutine. This will cut down on repeated code and make your script easier to maintain.
内容的提问来源于stack exchange,提问作者frankbean

