Private Sub Command1_Click()Dim 文件名称As String文件名称= "C:\Documents and Settings\Administrator\桌面\Hellod.Doc "Dim uObjDoc As Word.DocumentSet uObjWord = New Word.ApplicationuObjWord.Documents.Add "Normal", IIf(iMode = 1, True, False) '1、根据"Normal"模板添加一个新的文档[1-创建一个模板,0-创建一个文件]uObjWord.Documents.Save NoPrompt:=True, OriginalFormat:=wdWordDocument '2、设置Word对象为自动保存模式uObjWord.ActiveDocument.SaveAs 文件名称'3、保存Word文档到文件名称Set uObjDoc = NothingEnd SubEXCEL用法Public Function ExporToExcel(strOpen As String)'*********************************************************'* 名称:ExporToExcel'* 功能:导出数据到EXCEL'* 用法:ExporToExcel(sql查询字符串)'*********************************************************Dim Rs_Data As New ADODB.RecordsetDim Irowcount As IntegerDim Icolcount As IntegerDim xlApp As New Excel.ApplicationDim xlBook As Excel.WorkbookDim xlSheet As Excel.WorksheetDim xlQuery As Excel.QueryTableWith Rs_DataIf .State = adStateOpen Then.CloseEnd If.ActiveConnection = Cn.CursorLocation = adUseClient.CursorType = adOpenStatic.LockType = adLockReadOnly.Source = strOpen.OpenEnd With' Rs_Data.Open strOpen, Cn, adOpenStatic, adLockReadOnly With Rs_Data' .MoveFirstIf .RecordCount < 1 ThenMsgBox ( "没有记录! ")Exit FunctionEnd If'记录总数Irowcount = .RecordCount'字段总数Icolcount = .Fields.CountEnd WithSet xlApp = CreateObject( "Excel.Application ")Set xlBook = NothingSet xlSheet = NothingSet xlBook = xlApp.Workbooks().AddSet xlSheet = xlBook.Worksheets( "sheet1 ")xlApp.Visible = True'添加查询语句,导入EXCEL数据Set xlQuery = xlSheet.QueryTables.Add(Rs_Data, xlSheet.Range( "a1 "))With xlQuery.FieldNames = True.RowNumbers = False.FillAdjacentFormulas = False.PreserveFormatting = True.RefreshOnFileOpen = False.BackgroundQuery = True.RefreshStyle = xlInsertDeleteCells.SavePassword = True.SaveData = True.AdjustColumnWidth = True.RefreshPeriod = 0.PreserveColumnInfo = TrueEnd WithxlQuery.FieldNames = True '显示字段名xlQuery.RefreshWith xlSheet.Range(.Cells(1, 1), .Cells(1, Icolcount)) = "黑体"'设标题为黑体字.Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Bold = True'标题字体加粗.Range(.Cells(1, 1), .Cells(Irowcount + 1, Icolcount)).Borders.LineStyl e = xlContinuous'设表格边框样式End With' With xlSheet.PageSetup' .LeftHeader = " " & Chr(10) & "& " "楷体_GB2312,常规" "&10公司名称:" ' & Gsmc' .CenterHeader = "& " "楷体_GB2312,常规" "公司人员情况表& " "宋体,常规" " " & Chr(10) & "& " "楷体_GB2312,常规" "&10日期:"' .RightHeader = " " & Chr(10) & "& " "楷体_GB2312,常规" "&10单位:"' .LeftFooter = "& " "楷体_GB2312,常规" "&10制表人:"' .CenterFooter = "& " "楷体_GB2312,常规" "&10制表日期:"' .RightFooter = "& " "楷体_GB2312,常规" "&10第&P页共&N页"' End WithxlApp.Application.Visible = TrueSet xlApp = Nothing ' "交还控制给ExcelSet xlBook = NothingSet xlSheet = NothingEnd Function在VB 中控制WordWord 提供了一个Word 对象,通过在“引用” 中的该对象,可以实现对Word 的控制。
以下的代码演示了执行WordBasic 语句,该段代码是动态引用对象,无须在工程中引用Word 对象。
Private Sub Command1_Click()Dim wd As ObjectSet wd = CreateObject("Word.Basic ")wd.FileNewDefaultwd.FontSize 20wd.Insert "Hello, World "wd.FileSaveAs "C:\Documents and Settings\Administrator\桌面\Hello.Doc "wd.FileCloseSet wd = NothingEnd Sub执行后,将产生一个Hello.Doc 。
一个种办法是在Word 中调试好WordBasic 语句后,再发布到VB 中。
类似的处理应该也可以用在Execl 中。
巧用VBA自动处理Word表格Microsoft Word 97是大家熟悉的文字处理软件,强大的功能为我们的工作提供了很大的帮助。
而Visual Basic for Applications(VBA)的应用更为Word 97增添了不少特色,合理而恰当地使用VBA可为用户提供极大的方便。
下面介绍几则使用VBA编程自动处理Word表格的例子。
1. 创建表格,插入文字本例的功能是在文档开头插入一张 3 行 4 列的表格。
可用For Each...Next 结构来循环遍历表格中的每个单元格。
在For Each...Next 结构中,InsertAfter 方法用来向表格单元格添加文字( "第 1 单元格"、" 第 2 单元格"等等),oTable.AutoFormat属性用于指定表格套用格式。
运行结果如图1:@@0869600.JPG;图1@@Set oDoc = ActiveDocumentSet oTable= oDoc.Tables.Add(Range:=oDoc.Range(Start:=0, End:=0), NumRows :=3,NumColumns:=4)iCount = 1For Each oCell In oTable.Range.CellsoCell.Range.InsertAfter "第"& iCount & "单元格"iCount = iCount + 1Next oCelloTable.AutoFormat Format:=wdTableFormatColorful2,ApplyBorders:=True, App lyFont:=True, ApplyColor:=True2. 在表格的列、行插入序号1. 如果需要在表格的第一列插入序号,只需将For Each...Next 结构中的内容改为下述程序行即可,其中InsertAfter 方法用来向表格单元格添加序号( "第 1 行"、" 第2 行"等等)。