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