Dim WordApp As Word.Application
Dim wordDoc As Word.Document
Option Explicit
Private Sub Command1_Click()
Dim WordApp
Dim Word
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
Set Word = WordApp.Documents.Open(App.Path & "\\1.doc")
Word.Content.InsertAfter vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf
'Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=15 '此句可以控制,但我想直接在末尾加入表格,因为实现不知道Word中有几行文本
WordApp.ActiveDocument.Tables.Add Word.Application.Selection.Range, NumRows:=3, NumColumns:=4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
End Sub
在插入图表之前加一句
wordApp.selection.endkey unit:=wdStory '将光标移到文档末尾
二
Option Explicit
Private Sub Command1_Click()
'点击菜单 "工程/引用/Microsoft Word 9.0 Object Library "
Dim wd As New Word.Application
wd.Documents.Add DocumentType:=wdNewBlankDocument
'插入一个5列10行的表格
wd.ActiveDocument.Tables.Add Range:=wd.Selection.Range, NumRows:=10, NumColumns _
:=5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
wd.Selection.EndKey Unit:=wdStory '将光标移到最后
wd.Selection.InsertBreak Type:=wdPageBreak '插入分页符
wd.Visible = True
wd.ShowMe
'Set wd = Nothing
End Sub
怎么样判断Word文档中有没有图片?
Word里的图片包括两种(据我所知
If ActiveDocument.Shapes.Count + ActiveDocument.InlineShapes.Count > 0 Then
MsgBox "esit "
Else
MsgBox "Document doesn 't contain a shape "
End If
三
在表格中定位还好一些。 首先确定表格 然后 对象.cell(rowindex,colindex).text 即可。 好人做到底,发给你一个实例吧:
Private Sub cmdExport_Click()
Dim i As Integer, j As Integer
Dim ifieldcount As Integer, irecordcount As Integer
Dim wdapp As Word.Application
Dim wddoc As Word.Document
Dim atable As Word.Table
cmdFind_Click If Adodc1.Recordset.RecordCount > 0 Then irecordcount = Adodc1.Recordset.RecordCount '创建word应用程序,这一句话打开word2000
Set wdapp = CreateObject("Word.Application") '在word中添加一个新文档
Set wddoc = wdapp.Documents.Add
With wdapp
.Visible = True
.Activate '在word中增加一个表格
.Caption = "送检表"
Set atable = .ActiveDocument.Tables.Add(.Selection.Range, irecordcount + 1, 7)
atable.Cell(1, 1).Range.InsertAfter "物资编号"
atable.Cell(1, 2).Range.InsertAfter "物资名称"
atable.Cell(1, 3).Range.InsertAfter "规格型号"
atable.Cell(1, 4).Range.InsertAfter "批号或炉号"
atable.Cell(1, 5).Range.InsertAfter "送检数量"
atable.Cell(1, 6).Range.InsertAfter "进货日期"
atable.Cell(1, 7).Range.InsertAfter "送检时间" '指定表格内容
Adodc1.Recordset.MoveFirst
Do Until Adodc1.Recordset.EOF
atable.Cell(DataGrid1.Bookmark + 1, 1).Range.InsertAfter Adodc1.Recordset.Fields("物资编号") atable.Cell(DataGrid1.Bookmark + 1, 2).Range.InsertAfter Adodc1.Recordset.Fields("物资名称") atable.Cell(DataGrid1.Bookmark + 1, 3).Range.InsertAfter Adodc1.Recordset.Fields("规格型号")
If Adodc1.Recordset.Fields("批号或炉号") <> "" Then
atable.Cell(DataGrid1.Bookmark + 1, 4).Range.InsertAfter Adodc1.Recordset.Fields("批号或炉号") atable.Cell(DataGrid1.Bookmark + 1, 5).Range.InsertAfter Adodc1.Recordset.Fields("数量")
If Adodc1.Recordset.Fields("进货日期") <> "" Then
atable.Cell(DataGrid1.Bookmark + 1, 6).Range.InsertAfter Adodc1.Recordset.Fields("进货日期")
If Adodc1.Recordset.Fields("送检时间") <> "" Then
atable.Cell(DataGrid1.Bookmark + 1, 7).Range.InsertAfter Adodc1.Recordset.Fields("送检时间") Adodc1.Recordset.MoveNext
Loop
End With '清除word对象
Set wdapp = Nothing
Set wddoc = Nothing
Else MsgBox "没有送检物资!", , "提示窗口"
End If
End Sub
问题补充:如果已经有表格了,现在可以用书签或者其他方式来实现么? 可能我直接获取新建表格对象误导了你,如果已有表格,也有很多办法,常用的是通过从tables集合中获取表格对象。如此句:Set atable = .ActiveDocument.Tables.Add(.Selection.Range, irecordcount + 1, 7) 可以改成: Set atable = .ActiveDocument.Tables(1) 即通过表格索引(第几个表格)来获得表格对象,接下来的赋值操作还是一样的。 当然通过书签也可以,如果表中要修改的数据不多且不需要一般循环的话。
回答者: home20010252 | 六级 | 2008-12-17 17:37
Sub 修改单元格()
Selection.Tables(1).Cell(1, 1).Select ‘第1行第1列
Selection.TypeText Text:="ABCDEFG" ‘第1行第1列 的内容为
Selection.Tables(1).Columns.Width = 125 '定义表格的列宽
end sub
评论这张
转发至微博
转发至微博