如何用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。
怎么用VB打开word/EXCEL
……
……
恐怕难以完成你的要求。
要知道在VB中doc和xls文件只能调用word和excel来打开
VB是不能独立读取这些文件的。
除非你对doc和xls文件格式有相当了解
达到了wps2005工程师的水平
xls文件到还能通过其他方法来获取其中的数据
而且只能获得数据,
格式信息一定要通过引用office对象来取得
doc文件就更不要多想了
肯定不行。
但后台打开文件不让软件界面显示是完全可以做到的。
事实是
当你引用了office对象
除非专门加入Visible = True 命令显示word或excel界面
不然软件的界面是不会显示的。
如何用vb调用word,画出表格,然后向表格中写入东西
为什么不用VB在Excell里写东西呢?也可以打印并作出相应设置的。如果可以在Excell中使用的话,请与俺联系,俺给你操作代码。
----------------------------
在“工程”菜单-->;"引用"选项内选中“Microsoft Excel 11.0 Objiect library”
----------------------------
Dim xlApp As Excel.Application '定义Excel对象
Dim xlBook As Excel.Workbook '定义工作表对象
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application") '-------------创建excell对象!!!
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1) '指定工作表1
'-------------------------------------
'--------赋值语句---------------------
' xlSheet.Cells(1, 1) = "序号"
xlSheet.Cells(1, 1) = "项目" 'xlSheet.Cells(i, j) ------ i:行定位;j:列定位.
xlSheet.Cells(1, 2) = "单位"
xlSheet.Cells(1, 3) = "数据"
xlSheet.Cells(1, 4) = "备注"
如何用VB打印表格
VB下数据报表打印输出方法有多种,一是通过自己的设计和编程完成报表打印输出。
二是通过MS OFFICE实现报表打印输出。三是应用数据报表设计器的报表打印输出。
本人经常使用第一种方法,它具有根据需要灵活应用的特点,但需编写程序。下附代码,第一段代码定义基本变量,第二段代码定义打印函数,第三段代码是具体打印代码,含数据转换。
第四段代码是窗体加载代码,具体连接数据库和打印代码根据你所涉及的数据要求进行修改。 Option Explicit Private n As Integer Private m As Integer Private zh_dm(10) As Double Private dy_dm(3000, 10) As Variant Private zsl As Integer Private y As Integer Private x As Integer Private i As Integer Private k As Integer Private p As Integer Private pa As Integer Private j As Integer Private txt As String Private fnt As Integer Private dd As Variant Public Function prnt(x As Variant, y As Variant, fnt As Variant, txt0 As Variant) Printer。
CurrentX = x Printer。CurrentY = y Printer。
FontSize = fnt Printer。Print txt0 End Function Private Sub cmdPrint_Click() Adodc1。
Recordset。MoveFirst For i = 0 To zsl - 1 For j = 0 To 7 dy_dm(i, j) = Adodc1。
Recordset(j) Next j Adodc1。 Recordset。
MoveNext Next i '确定页数 k = Int(zsl / 80)'80为每页行数 If k - zsl / 80 80 Then Printer。 Line (500, 695 + 180 * 80)-(10000, 695 + 180 * 80) Printer。
NewPage End If Next p Printer。EndDoc ' 打印完成。
Cls End Sub Private Sub Form_Load() Adodc1。ConnectionString = "Provider=MSDASQL。
1;Persist Security Info=False;Data Source=jwl_dbf" Adodc1。 RecordSource = "select cs。
js_dm,cs。备件代码,cs。
备件名称,cs。备件规格,cs。
进口计算机号,cs。最低库存量,sl。
结存数量,sl。结存金额,sl。
结存单价,sl。类别代码 from JWCK_BM as cs,jwl_jiec as sl" & _ " where cs。
备件代码 = sl。备件代码" & " and cs。
备件代码>" & "''" & " order by sl。类别代码,sl。
备件代码" Adodc1。Refresh DataGrid1。
Refresh zsl = Adodc1。 Recordset。
RecordCount Adodc3。ConnectionString = "Provider=MSDASQL。
1;Persist Security Info=False;Data Source=jwl_dbf" Adodc3。 RecordSource = "select sum(结存金额) as hj from jwl_jiec" Adodc3。
Refresh End Sub 以上供参考。其它请参考西安交通大学出版社出版的“VISUAL BASIC 6。
0 高级编程技巧-ADO数据访问篇“ 。
如何用VB在WORD中添加表格
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=50, NumColumns:= _
4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <>; "网格型" Then
.Style = "网格型"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With
补充一下,可以用录制宏的方法查看代码学习编程,我也是在学习中
simon笑哈哈