簡體   English   中英

將Excel范圍復制到嵌入的Word文檔中,跳過“”(空單元格)

[英]Copy Excel range to embedded Word document skipping “” (empty cells)

(此問題是有關如何使用 Word應用程序界面(而不是就地)中嵌入Excel工作簿中的文檔的后續操作。之所以這樣做,是因為能夠將結果另存為獨立文檔。)

問題是我在Excel中有動態內容。 在EI列的單元格中實際上具有公式,例如E55 =IF(B55="";"";"normal") E55也是=IF('Technical!B55'="";"";'Technical!B55') 我當前的代碼不了解例如單元格B57為空,並認為應將其打印到MS Word。 它不會在MS Word中插入任何內容,但會插入例如為MS Word中的樣式預定義的項目符號。 如何停止呢? 例如,我可以使=IF(B55="";"empty";"normal")因此如果有幫助,不必要的行將用單詞“空”標記。

   A    B                   C                   D                  E
49    Paragraph with number 1                                    main
48    Ok text is text and it is good to have here.. a lot of     normal
50    Legal             John Smith                               table
51                      Telephone         +4854132155            table 
52                      Email             john.smith@mail.com    table
53    Paragraph with number 2                                    main
54    Text again a lot of text again comes here                  normal
55    Text again a lot of text again comes here                  normal
56    Text again a lot of text again comes here                  normal
57    =IF('Technical!B57'="";"";'Technical!B57')                 =IF(B57="";"";"normal")            
58    =IF('Technical!B58'="";"";'Technical!B58')                 =IF(B58="";"";"normal")

我當前的代碼:

With objWord
  Set wdRng = .Range.Characters.Last
  Set wdUndo = .Application.UndoRecord
  wdUndo.StartCustomRecord ("Doc Data")
  Set xlSht = Sheets("Other Data")
  'Here comes Header
  .Bookmarks("Date").Range.Text = xlSht.Range("AT2").Value
  .Bookmarks("DocumentName").Range.Text = xlSht.Range("AX13").Value
Set xlSht = Nothing

Set xlSht = Sheets("Pricelist")
  For Each cell In xlRng
    wdRng.InsertAfter vbCr & cell.Offset(0, -4).Text
     Select Case LCase(cell.Value)
        Case "title"
          wdRng.Paragraphs.Last.Style = .Styles("Heading 1")
        Case "main"
          wdRng.Paragraphs.Last.Style = .Styles("Heading 2")
        Case "sub"
          wdRng.Paragraphs.Last.Style = .Styles("Heading 3")
        Case "sub-sub"
          wdRng.Paragraphs.Last.Style = .Styles("Heading 4")
        Case "normal"
          wdRng.Paragraphs.Last.Style = .Styles("Normal")
        Case "contact"
          wdRng.Paragraphs.Last.Style = .Styles("Contact")
          Case "attachment"
          wdRng.Paragraphs.Last.Style = .Styles("Attachment")
          Case "technical"
          wdRng.Paragraphs.Last.Style = .Styles("Technical")
          Case "topic"
          wdRng.Paragraphs.Last.Style = .Styles("Topic")
          Case "signature"

           Sheets("Signatures").Range("M7:N7").Copy

  With wdRng

    .Paragraphs.Last.Range.PasteSpecial (wdPasteBitmap)

  End With

          Case "pagebreak"
     With wdRng
     .Paragraphs.Last.Range.InsertBreak Type:=wdPageBreak
     End With
          Case "table"

          xlSht.Range(cell.Offset(0, -4), cell.Offset(0, -1)).Copy

  With wdRng

    .Paragraphs.Last.Range.PasteAndFormat (wdFormatPlainText)

  End With

    End Select
  Next cell
  .SaveAs2 ActiveWorkbook.Path & "\" & _
Sheets("Other Data").Range("AN2").Value & ", " & _
Sheets("Other Data").Range("AN7").Value & "_" & _
Sheets("Other Data").Range("AN8").Value & "_" & _
Sheets("Other Data").Range("AX3").Value & ".docx"

  wdUndo.EndCustomRecord
  Set wdUndo = Nothing
  .Undo
  .Application.Quit False
End With

有時候喝杯茶,然后考慮其他事情是件好事。 我想出了一個使用=IF(B55="";"empty";"normal")的想法,因此我將在不必要的單元格中得到單詞“ empty”。 然后,我創建了名稱為“ empty”的新Case,並使用了以下代碼:

         Case "empty"

  With wdRng

    .Paragraphs.Last.Range.Delete

  End With

我不知道這是否是最好的解決方案。 至少有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM