<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Word VBA常用代碼合集

          共 2553字,需瀏覽 6分鐘

           ·

          2022-05-28 18:12

          ▎寫在前面

          都說寫VBA像累積木,除了核心部分的循環(huán)邏輯思路,其余都是再堆砌代碼。這篇文章就羅列一下我在寫Word VBA程序中,常用的一些代碼。




          今天先介紹操作Word表格部分。



          ?新建表格


          在光標(biāo)位置,新建一個(gè)1行3列的表格,邊框線設(shè)置為實(shí)線。

          sub testSet tb = ActiveDocument.Tables.Add(Selection.Range, 1, 3)tb.Style?=?"網(wǎng)格型"end sub



          ?向表格里面寫入值


          兩種方式

          Sub 第一種寫入方法()    Dim t As Table        Set t = ActiveDocument.Tables(1)        t.Cell(1, 1).Range = 1End Sub
          Sub 第二種寫入方法() Set t = ActiveDocument.Tables(1).Range For i = 1 To t.Cells.Count t.Cells(i).Range = i NextEnd Sub



          ?設(shè)置表格列寬


          設(shè)置表格的列寬,其中需要注意單位的轉(zhuǎn)換。厘米和磅。

          Sub 設(shè)置word表格的寬度()     Dim tbl As Table    Set tbl = ActiveDocument.Tables(2)    tbl.Columns(1).Width = Application.CentimetersToPoints(1.42)    tbl.Columns(2).Width = Application.CentimetersToPoints(5.07)    tbl.Columns(3).Width = Application.CentimetersToPoints(2.13)    tbl.Columns(4).Width = Application.CentimetersToPoints(4.33)    tbl.PreferredWidth = Application.CentimetersToPoints(18.99)End?Sub



          ?判斷光標(biāo)是否在表格中


          利用的是information方法判斷

          Sub 判斷光標(biāo)是否在表格里()    If Selection.Information(wdWithInTable) = True Then        MsgBox "光標(biāo)在表格里"    Else        MsgBox "光標(biāo)不在表格里"    End IfEnd?Sub



          ?表格中插入多行


          Sub 表格最后插入2行()   Dim myTable As Table    Set myTable = ActiveDocument.Tables(1)    myTable.Rows.Last.Select    Selection.InsertRowsBelow 2End?Sub





          ?向表格末尾插入內(nèi)容并居中


          利用move方法,移動(dòng)光標(biāo),寫內(nèi)容


          Sub 向表格末尾插入內(nèi)容并居中()    Dim tbl As Word.Table    For Each tbl In Selection.Range.Tables        tbl.Range.Cells(tbl.Range.Cells.Count).Select        Selection.Move wdCharacter, 2        Selection.TypeParagraph        Selection.Move wdParagraph, -1        Selection.TypeText "表格X"        Selection.Extend        '/字體格式        With Selection.Range.Font            .Color = -16777216            .NameFarEast = "宋體"            .Size = 10 '字號(hào) 四號(hào)            .Bold = 0 '加粗為1,不加粗為0        End With        With Selection.ParagraphFormat            .LineSpacing = 20 '行間距20磅            .Alignment = wdAlignParagraphCenter '居中顯示        End With    NextEnd?Sub




          ?表格居中


          內(nèi)容居中,表格整體居中

          Sub 向表格末尾插入內(nèi)容并居中()    '將表格內(nèi)容調(diào)整為上下,左右居中    For Each oTable In oDoc.Tables        oTable.AutoFitBehavior (wdAutoFitWindow)               '根據(jù)窗口調(diào)整內(nèi)容        oTable.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter    '水平居中        oTable.Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter    '垂直居中        oTable.Rows.Alignment = wdAlignRowCenter               '表格整體居中    NextEnd?Sub




          ?選中第二頁第一個(gè)表格


          Sub test()  With Selection    .GoTo wdGoToPage, , 2    .Bookmarks("\Page").Range.Tables(2).Select  End WithEnd?Sub



          推薦閱讀:(點(diǎn)擊下方標(biāo)題即可跳轉(zhuǎn))


          瀏覽 187
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产乱妇无码毛片A片在线看下载 | 蜜桃二区| 久久精品2019中文字幕人妻欧 | 色老汉在线视频 | 福利黄色|