1、对其WORD内容设置字体样式,以及在WORD中插入表格,以及表格单元格融合与填充.
Option Explicit
Private Sub Command1_Click()
Dim filename As String
CD.ShowSave
filename = CD.filename
OutWord filename
MsgBox "OK"
End Sub
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, N
umColumns:=3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
With .Tables(1)
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
2
VB程序操作word表格(文字、图片)
         很多人都知道,用vb操作excel的表格非常简单,但是偏偏项目中碰到了VB操作word表格的部分,googlebaidu搜爆了,都没有到我需要的东西。到是搜索到了很多问这个问题的记录。没办法,索性只有自己去尝试了。下面把一些代码发上来,给需要的朋友一点提示。
          打开一个已经存在的wrod文件(这个文件包含了表格)
Dim WordApp
Dim Word
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
Set Word = WordApp.Documents.Open("c:\record.dot")
         知道了就很简单了,下面是选定某一个表格的一个单元格,并修改其内容
Word.Tables(1).cell(1, 2)="内容"
         VBA中的这些数组元素下标都是从1开始的,比如excel的第一行一列也是字体代码大全ExSheet.Cells(1,1),而不是ExSheet.Cells0,0,WORD的表格也是这样,不信自己试一下就知道了。所以上面那句话的意思就是对整个word文档中的第一个表格的第一行第二列的内容改变为内容。很简单吧?网上有些人在问是不是
Word.Tables(1).cell(1, 2).或者Word.Tables(1).cell(1, 2).text。试一下就发现这2种都不对。
          插入图片其实也很简单,代码如下:
Word.Tables(1).cell(1, 3).Range.InlineShapes.AddPicture ("c:\photo.jpg")
         微软的那一套东西集成得很不错,其之间任意调用非常方便,大家如果想用VBWORD做更多的应用,却又不知道怎么实现,我想最好的办法就是录制宏了,你把你想完成的功能操作一遍,然后查看宏,一目了然了吧!