1. 如何用 word VBA 在任一选定区域插入1×1表格,既用表框起来,另有它用。谢谢
这种事情,用录制宏就可以了
录制之前,先选定一个区域
开始录制
把选定区域内容剪切,
在当前位置插入一个表格
把剪切内容粘贴进表格
结束录制
我的 Word 2007 ,录制的结果是这样的
Sub 宏1()
Selection.Cut
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
1, 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
Selection.PasteAndFormat (wdPasteDefault)
End Sub
2. 在Excel中,通过VBA往word表格里写数据时,如何根据内容增加或者减少word表格列数
可以通过word的table对象对word表格进行操作。下面的代码在Excel的Vba中运行,运行前先添加Microsoft Word XX.0 Object Library的引用,其中XX.0根据你安装的Office 版本不同而不同,一般来试Office Word 2003 是11.0,Office Word 2013 是15.0 。
SubtoWord()
DimwordAppAsNewWord.Application
wordApp.Visible=True
DimwordDocAsWord.Document
DimwordTableAsWord.Table
SetwordDoc=wordApp.Documents.Add'新建一个文档
SetwordTable=wordDoc.Tables.Add(wordDoc.Range(),1,1,wdWord9TableBehavior,wdAutoFitWindow)'文档中插入一个表格
DimrAsLong,cAsLong
Forr=1ToActiveSheet.UsedRange.Rows.Count
Ifr>wordTable.Rows.CountThen
wordTable.Rows.Add'表格插入行
EndIf
Forc=1ToActiveSheet.UsedRange.Columns.Count
Ifc>wordTable.Columns.CountThen
wordTable.Columns.Add'表格插入列
wordTable.Columns.AutoFit
EndIf
wordTable.Cell(r,c).Range.Text=Cells(r,c)
Nextc
Nextr
wordTable.
EndSub
3. 如何用vba代码向word文档中插入表格
Sub AA()
Dim projectno As String, projectname As String, datereceive As Date, datecomplate As Date, functionary As String
Dim arr As Object
Dim i As Long
Dim brr
projectno = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(1, 2).Range
Dim excelobject As Object
Set excelobject = GetObject("D:\Downloads\project(word)\project.xls")
Set arr = excelobject.Sheets(1).usedrange()
brr = arr
For i = 2 To UBound(brr)
If InStr(1, projectno, brr(i, 1)) > 0 Then
projectname = brr(i, 2)
datereceive = brr(i, 3)
datecomplate = brr(i, 4)
functionary = brr(i, 5)
Exit For
End If
Next i
ActiveDocument.Tables(1).Cell(1, 2).Range = projectname
ActiveDocument.Tables(1).Cell(2, 2).Range = datereceive
ActiveDocument.Tables(1).Cell(3, 2).Range = datecomplate
ActiveDocument.Tables(1).Cell(4, 2).Range = functionary
excelobject.Close False
End Sub