永发信息网

用VB向word中输入文字,如何设置连续文字的不同字体格式

答案:2  悬赏:20  手机版
解决时间 2021-12-24 15:44
  • 提问者网友:刺鸟
  • 2021-12-23 22:32
用VB(Visual Basic)向word中输入的文字,希望实现连续的文字有不同的字体格式,比如上一个文字是宋体,下一个文字是楷体,或者有些文字使用下标格式,有些文字使用上标格式,谢谢了!
我用的如下程序,只能设置整个段落使用同样的字体格式:
Dim wordApp As New Word.Application
Dim wordDoc As New Word.Document
Set wordDoc = wordApp.Documents.Add
wordApp.Visible = True
With wordDoc

.Paragraphs(.Paragraphs.Count).Range.Font.NameFarEast = "宋体"
.Paragraphs(.Paragraphs.Count).Range.Font.NameAscii = "Arial"
.Paragraphs(.Paragraphs.Count).Range.Font.Size = 12
.Paragraphs(.Paragraphs.Count).Range.Font.Bold = True
.Paragraphs(.Paragraphs.Count).Alignment = wdAlignParagraphLeft
.Content.InsertAfter "需要输入的文字" & vbCrLf
请高手帮忙!
最佳答案
  • 五星知识达人网友:第幾種人
  • 2021-12-23 23:55
可以试一下vba的录制功能把操作录下来,然后在代码里试着去实现一下。
全部回答
  • 1楼网友:几近狂妄
  • 2021-12-24 00:14
'vb 操作word代码 option explicit private function outword(byval filepath as string) as boolean dim newdoc as word.document set newdoc = new word.document with newdoc .paragraphs(.paragraphs.count).range.font.name = "宋体" .paragraphs(.paragraphs.count).range.font.size = 10.5 .paragraphs(.paragraphs.count).alignment = wdalignparagraphright .content.insertafter "编号:" & vbcrlf .paragraphs(.paragraphs.count).range.font.name = "宋体" .paragraphs(.paragraphs.count).range.font.size = 26 .paragraphs(.paragraphs.count).range.font.bold = true .paragraphs(.paragraphs.count).alignment = wdalignparagraphcenter .content.insertafter vbcrlf & "xxxxxxxxx报告" & vbcrlf & vbcrlf & vbcrlf & vbcrlf & vbcrlf .paragraphs(.paragraphs.count).range.font.name = "宋体" .paragraphs(.paragraphs.count).range.font.size = 15 .paragraphs(.paragraphs.count).range.font.bold = false .paragraphs(.paragraphs.count).alignment = wdalignparagraphleft .content.insertafter "项目名称:" & vbcrlf .content.insertafter "应急类型:" & vbcrlf .content.insertafter "预警状态:正常/警界/危机" & vbcrlf .paragraphs(.paragraphs.count).alignment = wdalignparagraphcenter .tables.add range:=.range(start:=.range.end - 1, end:=.range.end), numrows:=1, numcolumns:=3, defaulttablebehavior:=wdword9tablebehavior, autofitbehavior:=wdautofitfixed 'with .tables(0) 'if .style <> "表 (格子)" then '.style = "表 (格子)" 'end if '.applystyleheadingrows = true '.applystylelastrow = true '.applystylefirstcolumn = true '.applystylelastcolumn = true '.columns.width = 50 '.rows.height = 20 'end with .paragraphs(.paragraphs.count).range.font.name = "宋体" .paragraphs(.paragraphs.count).range.font.size = 15 .paragraphs(.paragraphs.count).range.font.bold = false .paragraphs(.paragraphs.count).alignment = wdalignparagraphleft .content.insertafter "委 托 人:" & vbcrlf .content.insertafter "预 警 机 构:" & vbcrlf .content.insertafter "报告负责人:" & vbcrlf .content.insertafter "时 间:" & vbcrlf .paragraphs(.paragraphs.count).alignment = wdalignparagraphleft .tables.add range:=.range(start:=.range.end - 1, end:=.range.end), numrows:=8, numcolumns:=2, defaulttablebehavior:=wdword9tablebehavior, autofitbehavior:=wdautofitfixed with .tables(2) 'if .style <> "表 (格子)" then '.style = "表 (格子)" 'end if '.applystyleheadingrows = true '.applystylelastrow = true '.applystylefirstcolumn = true '.applystylelastcolumn = true .cell(2, 1).range.text = "项目名称" .range.cells(3).row.cells.merge .range.cells(3).range.font.size = 15 .range.cells(3).range.text = "信息来源/文献检索范围:" & vbcrlf & vbcrlf & vbcrlf .range.cells(4).row.cells.merge .range.cells(4).range.text = "情况描述/检索结果:" & vbcrlf & vbcrlf & vbcrlf .range.cells(5).row.cells.merge .range.cells(5).range.text = "影响分析:" & vbcrlf & vbcrlf & vbcrlf & vbcrlf .range.cells(6).row.cells.merge .range.cells(6).range.text = "建议:" & vbcrlf & vbcrlf & vbcrlf & vbcrlf & vbcrlf & vbcrlf .range.cells(7).row.cells.merge .range.cells(7).range.text = "专家组成员:" & vbcrlf & vbcrlf & vbcrlf & vbcrlf & vbcrlf & vbcrlf .range.cells(8).row.cells.merge .range.cells(8).range.text = "附件目录:" & vbcrlf & vbcrlf & vbcrlf & vbcrlf & vbcrlf & vbcrlf .range.cells(9).row.cells.merge .range.cells(9).range.text = "报告负责人:" & vbcrlf & vbcrlf & vbcrlf & vbcrlf & " 年 月 日" end with end with newdoc.saveas filepath newdoc.close end function private sub form_load() dim filename as string filename = app.path & "\aa.doc" outword filename msgbox filename msgbox "ok" end sub
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯