简体   繁体   English

如果满足条件,则将 Word 中的段落复制到下一页(宏)

[英]Copy paragraphs from Word into the next page if the condition is met (macro)

I am trying to create macro which is going to export data from Excel file into Word document with specific conditions.我正在尝试创建宏,它将在特定条件下将数据从 Excel 文件导出到 Word 文档中。 Each row in the table has photos attached - sometimes 1 and sometimes more.表格中的每一行都附有照片 - 有时是 1 个,有时是更多。 I would like to paste text from the table and then the photo below.我想粘贴表格中的文字,然后粘贴下面的照片。 If my current row has more than 1 photo attached, then I would like to copy the same text to the next page and paste next photo below.如果我的当前行附加了 1 张以上的照片,那么我想将相同的文本复制到下一页并粘贴下面的下一张照片。 As a result I will have 1 photo per page with the description.因此,我将每页有 1 张带有说明的照片。

For now I have a code which is checking photo's name by counting two first numbers (example: 66_foto1.jpg, 66_foto2.jpg, 67_foto1.jpg) but I am not sure how to copy the text at the begining of the next page.现在我有一个代码,它通过计算两个第一个数字(例如:66_foto1.jpg、66_foto2.jpg、67_foto1.jpg)来检查照片的名称,但我不确定如何复制下一页开头的文本。

Part of the code:部分代码:

   Dim fso As Object
    Dim objfolder As Object
    Dim objfile As Object
    Dim lCount As Long 'number of photo starts with 66_
    Dim strpath As String
    Dim objsub As Object
    strpath = "C:\xxx\photos" 'path where photos are located
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objfolder = fso.GetFolder(strpath)
    For Each objfile In objfolder.Files
    If UCase(objfile.Name) Like "66_*" Then lCount = lCount + 1
    Next objfile

    Dim imagePath As String

    For i = 1 To lCount
    imagePath = "C:\xxx\photos\" & "66_" & "Foto " & i & ".jpg"
        objWord.Selection.InlineShapes.AddPicture Filename:= _
        imagePath, LinkToFile:=False, _
        SaveWithDocument:=True
        objWord.Selection.TypeParagraph
    Next

For now there is just photo no.现在只有照片没有。 66, but I would like to make variables and count different ones. 66,但我想制作变量并计算不同的变量。

Paste both the text and the picture into a two-row table, with the first row marked as a header row.将文本和图片粘贴到一个两行表格中,第一行标记为标题行。 Thereafter, anything that causes a new row to start on a new page will replicate the header row on that page.此后,任何导致新行在新页面上开始的操作都将复制该页面上的标题行。 No text replication code required;无需文本复制代码; it's also simpler to update later if needed.如果需要,以后更新也更简单。

For example (as a Word macro - ie not automated from Excel, which I'll leave to you):例如(作为 Word 宏 - 即不是从 Excel 自动化,我将留给您):

Sub AddPics()
Application.ScreenUpdating = False
Dim wdDoc As Word.Document, wdTbl As Word.Table, strPic As String, r As Long
Const strFldr As String = "C:\xxx\photos\": r = 1
Set wdDoc = ActiveDocument
With wdDoc
  Set wdTbl = .Tables.Add(Range:=.Bookmarks("MyBookmark").Range, NumRows:=1, NumColumns:=1)
  With wdTbl
    .Rows.Alignment = wdAlignRowCenter
    .PreferredWidthType = wdPreferredWidthPoints
    .PreferredWidth = InchesToPoints(6)
    .Range.Cells(1).Range.Text = "Excel text"
    strPic = Dir(strFldr & "66_*.jpg", vbNormal)
    Do While strPic <> ""
      r = r + 1
      .Rows.Add
      With .Rows(r)
        .HeightRule = wdRowHeightExactly
        .Height = InchesToPoints(6)
        .AllowBreakAcrossPages = False
        .Range.InlineShapes.AddPicture FileName:=strFldr & strPic, Range:=.Cells(1).Range
      End With
    strPic = Dir()
    Loop
    .Rows(1).HeadingFormat = True
  End With
End With
Application.ScreenUpdating = True
End Sub

The above code will insert all the 66_*.jpg pics and have the 'Excel text' appear above each pic.上面的代码将插入所有 66_*.jpg 图片,并在每张图片上方显示“Excel 文本”。 As coded, pic sizes are automatically limited to 6in*6in at their correct aspect ratios.按照编码,图片大小自动限制为 6in*6in,以正确的纵横比。

Please note: StackOverflow is not a free coding forum.请注意:StackOverflow 不是一个免费的编码论坛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM