将表格,书签和页面插入Word文档的中间 [英] Insert table, bookmarks, and pages into middle of Word document

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

问题描述

@macropod(底部)中的以下非常有用的代码可以很好地将Excel中生成的表/书签/分页符插入Word文档的末尾(

The following very helpful code from @macropod (at bottom) works well to insert tables/bookmarks/page breaks, generated in Excel, into the end of a Word document (How to insert table and bookmark into Word, one per page). I'm trying to modify the code to insert into the middle of an original 2 page document, with one table on one page and a second table on page 2, and a few empty paragraphs and a pagebreak in between. The result should be each new bookmark/table combo is iteratively inserted into a new page in between the original two tables.

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

我尝试替换:

    Set wdRng = .Characters.Last

使用

   Set wdRng = .Characters.First
   Set wdRng = wdRng.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=(d - datMin + 2)) 

其中(d-datMin + 2)是页码,但这不起作用.它将第一个表/书签/页面放置在正确的位置,但随后将随后的表/书签/页面中断放置在文档的末尾,并且它们相互重叠.有什么想法我做错了什么以及我该如何解决?

where (d - datMin + 2) is the page number, but this doesn't work. It puts the first table/bookmark/page in the right place, but then it puts the subsequent tables/bookmarks/pagebreaks at the end of the document, and they overlap each other. Any ideas what I'm doing wrong and how I can fix this?

推荐答案

令人惊讶的是,该修复很容易.

Embarrasingly, it turns out the fix is quite easy.

而不是上面的

Set wdRng = .Characters.Last

就像替换为一样简单

Set wdRng = .Paragraphs(2).Range

或者更好的方法是,在要添加内容(表格,书签,分页符等)的位置插入分节符(插入->中断->分节符类型).在文档中间,然后使用:

Or better still, insert a Section Break (Insert->Break->Section Break Type) where you want to add stuff (tables, bookmarks, page breaks, etc). in the middle of the document, and then use:

Set wdRng = .Sections(2).Range
. . . 
. . . 

并且要确保插入顺序正确(不是反向顺序),需要进行以下更改:

And to make sure the order of insertions is correction (not in reverse order), need the following changes:

    Set Rng = .Sections((d - datMin) + 1).Range 'Go to next section each loop
    . . . 
    . . . 

    wdTbl.Range.Characters.Last.Next.InsertBreak wdSectionBreakNextPage  'To add pagebreak . . . instead of .InsertBefore Chr(12) as in original code


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

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