首页 > 解决方案 > 从 Excel 写入 Word

问题描述

在 Excel 宏中,我将工作表行的内容写入 Word .docx。实际的写作指导是

objselection.TypeText (textB)

这以无可争议的格式写入。

我的问题是:我可以控制字体吗?例如,我想用斜体字书写。

标签: excelvbafonts

解决方案


Sub Skeleton()
'

Dim wkb1 As Workbook
Dim objApplication As Object
'
file_path = "E:\TUSHINO COMPANY\AAAB FIND NEW PROJECT\Breadline 2021\Customer Documents\"
sfulname = file_path & "Experimental Doc"
'
' CREATE THE WORD OBJECT
'
Set objWord = CreateObject("Word.application")
objWord.Visible = True
'
' ADD A DOCUMENT TO THE WORD OBJECT
'
Set objDoc = objWord.Documents.Add
'
Set objselection = objWord.Selection
'
' I was surprised that this is not " = objDoc.Selection"
'
' In production there would be code extracting stuff
' from the worksheet ready to write on the word document
'
textA = " This is the first write"
With objselection
.Font.Size = 12
.TypeText (textA)   ' Top of the page heading
End With
'
objDoc.SaveAs (sfulname)
objWord.Application.Quit ' This is needed to close the document
Set objWord = Nothing
'
998 xx = 0
'
ay = "Finished"
MsgBox ay
'
999   xx = 0
'
End Sub

推荐阅读