如何将表和书签插入Word,每页一个 [英] How to insert table and bookmark into Word, one per page

查看:208
本文介绍了如何将表和书签插入Word,每页一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Word文档,该文档以第一页上的一个表开头,然后根据Excel文件中的数据,我想开始在随后的页面中插入表,并且我想在每个表之前插入一个文本.标题或标题.我正在使用书签插入此文本.插入表格后,我想添加一个分页符,以便可以在下一页上开始下一个表格的插入.

I have a Word document that starts with one table on page one, and then depending on the data in an Excel file, I want to start inserting tables in subsequent pages, and just before each table, I want to insert a text blurb or caption. I'm using a bookmark to insert this text. After inserting the table, I want to add a page break, so that I can start the next table insertion on the next page.

我从下面的代码中获得的结果将表和书签以不正确的顺序,看似随机的页面和看似随机的顺序放置,并且表的尺寸不正确(不是应该的6X5,而是某种程度上,重新3x3).如何控制表格,书签和分页符以正确订购文档?

The result I get from the code below places the tables and bookmarks in incorrect order, on seemingly random pages, and in seemingly random order, and the tables are not dimensioned properly (not 6X5 as they should be, but somehow they're 3x3). How can I control the tables, bookmarks, and page breaks to order the document properly?

'FROM EXCEL
Dim wd as New Word.Document
Dim doc as Word.Document
Dim Rng as Range
Dim d as Variant
dim datMin, datMax as Date
datMin = "04/01/2020"
datMax = "04/05/2020"

Set doc = wd.Documents.Open("myFile")
Set Rng = doc.Range(0, 0)
For d = datMin To datMax
    Set Rng = Rng.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=(d - datMin + 2)) 'Go to top of page
    doc.Bookmarks.Add "D" & d - datMin + 2, Rng  'BOOKMARK NAMES ARE "D2", "D3", etc.
    Rng.Bookmarks("D" & d - datMin + 2).Range.Text = d   'Place Text in bookmark
    doc.Tables.Add Rng, 6, 5  'Add table
    Rng.InsertBreak wdPageBreak  'Add pagebreak
Next d

推荐答案

您无条件使用'Dim Rng as Range'意味着您引用的是Excel范围的代码.试试:

Your unqualified use of 'Dim Rng as Range' implies to the code that you're referring to an Excel range. Try:

Dim wdApp As New Word.Document, wdDoc As Word.Document, wdRng As Word.Range, wdTbl As Word.Table
Dim d As Long, StrBkMk As String, datMin As Date, datMax As Date
datMin = "04/01/2020": datMax = "04/05/2020"

Set wdDoc = wdApp.Documents.Open("myFile")
With wdDoc
  For d = datMin To datMax
    StrBkMk = "D" & d
    Set wdRng = .Characters.Last
    With wdRng
      .Collapse wdCollapseStart
      .Text = CDate(d) 'Place Text in bookmark
      .Bookmarks.Add StrBkMk, .Duplicate
      .Collapse wdCollapseEnd
      .InsertBefore vbCr
      Set wdTbl = .Tables.Add(.Duplicate, 6, 5)  'Add table
      wdTbl.Range.Characters.Last.Next.InsertBefore Chr(12) 'Add pagebreak
    End With
  Next d
  .Characters.Last.Previous.Text = vbNullString
End With

这篇关于如何将表和书签插入Word,每页一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆