Public Function FormatStatInfoToWord(msgData As MSHFlexGrid, ByVal FilePath As String, Optional PassWord As String = "") As Boolean On Error GoTo Err_FormatStatInfoToWord MsgBox "系统开始排版操作!", vbInformation, "系统提示" FormatStatInfoToWord = False Set WordOBJ = New Word.Application Set doc = WordOBJ.Documents.Add WordOBJ.Visible = False With WordOBJ .ActiveDocument.PageSetup.PaperSize = wdPaperCustom .ActiveDocument.PageSetup.PageWidth = 841.95 .ActiveDocument.PageSetup.PageHeight = 595.35 .Selection.Font.Name = "宋体" .Selection.Font.Size = 18 .Selection.TypeText Text:=" 长青会员消费信息" .Selection.TypeParagraph .ActiveDocument.Tables.Add Range:=.Selection.Range, NumRows:=msgData.Rows - 1, NumColumns:=msgData.Cols, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
Dim i As Integer, j As Long For i = 0 To msgData.Rows - 1 For j = 0 To msgData.Cols - 1 .Selection.Font.Size = 10 .Selection.TypeText Text:=msgData.TextMatrix(i, j) If j = msgData.Cols - 1 And i = msgData.Rows - 1 Then
Else .Selection.MoveRight Unit:=wdCell End If Next j Next i End With '保存 doc.SaveAs FileName:=FilePath, PassWord:=PassWord WordOBJ.Quit SaveChanges:=wdDoNotSaveChanges Set doc = Nothing Set WordOBJ = Nothing FormatStatInfoToWord = True MsgBox "排版成功,文件已存储为" & FilePath, vbInformation, "系统提示" Exit Function Err_FormatStatInfoToWord: WordOBJ.Quit Set doc = Nothing Set WordOBJ = Nothing MsgBox Err.Description, vbInformation, "系统提示" End Function
On Error GoTo Err_FormatStatInfoToWord
MsgBox "系统开始排版操作!", vbInformation, "系统提示"
FormatStatInfoToWord = False
Set WordOBJ = New Word.Application
Set doc = WordOBJ.Documents.Add
WordOBJ.Visible = False
With WordOBJ
.ActiveDocument.PageSetup.PaperSize = wdPaperCustom
.ActiveDocument.PageSetup.PageWidth = 841.95
.ActiveDocument.PageSetup.PageHeight = 595.35
.Selection.Font.Name = "宋体"
.Selection.Font.Size = 18
.Selection.TypeText Text:=" 长青会员消费信息"
.Selection.TypeParagraph
.ActiveDocument.Tables.Add Range:=.Selection.Range, NumRows:=msgData.Rows - 1, NumColumns:=msgData.Cols, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
Dim i As Integer, j As Long
For i = 0 To msgData.Rows - 1
For j = 0 To msgData.Cols - 1
.Selection.Font.Size = 10
.Selection.TypeText Text:=msgData.TextMatrix(i, j)
If j = msgData.Cols - 1 And i = msgData.Rows - 1 Then
Else
.Selection.MoveRight Unit:=wdCell
End If
Next j
Next i
End With
'保存
doc.SaveAs FileName:=FilePath, PassWord:=PassWord
WordOBJ.Quit SaveChanges:=wdDoNotSaveChanges
Set doc = Nothing
Set WordOBJ = Nothing
FormatStatInfoToWord = True
MsgBox "排版成功,文件已存储为" & FilePath, vbInformation, "系统提示"
Exit Function
Err_FormatStatInfoToWord:
WordOBJ.Quit
Set doc = Nothing
Set WordOBJ = Nothing
MsgBox Err.Description, vbInformation, "系统提示"
End Function
Dim myTable as Tableset mytable=ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:=5
t.Cell(2, 2).Range.Text = "qwe" ‘将第二行第二列的格子填入qwe.