利用VB操作Excel与Word
| |
:雨 2007/05/10 20:15 ..by 风の雪
  VB对Excel和Word的基本操作还是比较简单的,但由于经常要用到,更重要的是经常有人问我,所以今天把两份示例代码放上来让大家参考。

1、VB操作Excel

   '引用Microsoft Excel 9.0 Object Library(Offic2000,Office2003的为Microsoft Excel 11.0 Object Library)
   Dim newxls As New Excel.Application
   Dim newbook As Excel.Workbook
   Dim newsheet As Excel.Worksheet
   newxls.Visible = True  '界面可见,当要后台处理的时候,可以让newxls.Visible = False
   Set newbook = newxls.Workbooks.Add  '添加工作簿
   Set newsheet = newbook.Worksheets(1)  '引用工作表
   newsheet.Columns("A").ColumnWidth = 5  'A列的宽为5
   newsheet.Columns("B:K").ColumnWidth = 15  'B~K列的宽为15
   newsheet.Rows(1).Font.Bold = True  '第一行粗体
   For i = 0 To 10
       newsheet.Rows(i + 1).HorizontalAlignment = xlCenter  '居中显示
       For j = 0 To 19
           newsheet.Cells(i + 1, j + 1) = (i+1)*(j+1)  '添加数据
       Next
   Next


2、VB操作Word

   ''引用Microsoft Word 9.0 Object Library(Offic2000,Office2003的为Microsoft Word 11.0 Object Library)
   Dim newword As Word.Application
   Dim newdoc As Word.Document
   Set newword = CreateObject("Word.Application")
   Set newdoc = newword.Documents.Add  '添加文档
   newword.Visible = True  '界面可见,当要后台处理的时候,可以让newxls.Visible = False
   newdoc.Content.Text = "1234567890"  '在文档上添加内容
   newdoc.Range(0, 10).Font.Size = 24  '文字大小
   newdoc.Range(0, 10).Bold = True  '粗体
< Tags: , , >

3 comment(s)
kill tt
[2008/09/23 15:53]
tt你是猪啊,学习的地方你学就学,不学就不学,嫌这嫌那的,你来写写看。fn
风の雪 Email Homepage
[2008/07/31 09:00]
兄弟,代码写出来是给你作个参考的。每人知道你到底要做什么,所以是不可能写得符合你的要求的。

这个例子只是让你知道,要引用组件,然后如何创建Word应用程序对象和Word文档对象等。其余的就需要看实际情况而定。

如果我有那么多时间把完成的教程写出来,我早就出书了,还用得着写在这?

看别人代码或例子最主要要学习的是其中的思维,而不是看着代码照搬就用。多动脑筋思考下,就算是一个很简单的例子你也可以知道其余的地方大概是怎么用的,这样才能达到句一反三的效果哦。
tt
[2008/07/31 07:15]
写的是个屁,就不能写的详细点?
分页: 1/1 第一页 1 最后页

发表评论
表情
emotemotemotemotemotemotemot
emotemotemotemotemotemotemot
emotemotemotemotemotemotemot
emotemotemotemotemotemotemot
emotemotemotemotemotemotemot
emotemotemotemotemotemotemot
emotemotemotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]