Consulting

Results 1 to 3 of 3

Thread: Problem with Word - Pasting content from multiple table cells into one

  1. #1
    VBAX Regular
    Joined
    Apr 2020
    Location
    Perth, Western Australia
    Posts
    6
    Location

    Question Problem with Word - Pasting content from multiple table cells into one

    This program is written in Excel VBA for the latest version of MS Office. The program calls and manipulates objects in both Excel and Word. However, the problem in this case is specifically related to Word Tables and copying content from multiple cells in one document and pasting this with formatting into a Table Cell in another document.

    Background: The intent is to upgrade an existing program to create a more advanced report generation tool for student marks and feedback. However, I am having a problem with an upgrade to the text concatenation within the reports because I now want the program to keep the source formatting of the original report elements in the finalised report.
    The report text for multiple groups is held in a sequence of tables within in a Word document (D1). Each table refers to a different aspect of the assignment and each row in the table is allocated to a group. Comments are written in the second column of each of the D1 tables (the first column in each table is used for the group number). The program draws content from multiple table cells in D1 and conjoins these in the appropriate table cells in a report template document (D2). In the past this step was implemented using the following type of code (wdDocRep is D2 and wdDocContent is D1).
    'wdDocRep.Tables(2).Cell(2, 1).Range.Text = wdDocContent.Tables(1).Cell(sngCnt1 + 2, 2).Range.Text & wdDocContent.Tables(2).Cell(sngCnt1 + 2, 2).Range.Text

    The Problem:
    To pull in the correct content and the associated formatting, I am using the following code. Once again wdDocRep is D2 and wdDocContent is D1. rngCell is a Word Range object. sngCnt1 is a group counter used in a looping construct for each member of the groups and it is not pertinent to the problem.
    wdDocRep.Tables(2).Cell(2, 1).Range.Text = "" 'Clear the content in the cell in D2
    For intCnt = 1 To 2 'Start the counter loop
    Set rngCell = wdDocContent.Tables(intCnt).Rows(sngCnt1 + 2).Cells(2).Range 'This defines the range object from D1 that needs to be copied and pasted into the D2 report.
    rngCell.End = rngCell.End - 1 'This removes the cell end point, so it is just the content that is being copied.
    rngCell.Copy 'This copies the content to the clipboard for transfer
    wdDocRep.Tables(2).Rows(2).Cells(1).Range.Collapse Direction:=WdCollapseDirection.wdCollapseEnd 'It was intended that this would shift the focus for inserting the text to the end point in the target cell - Please note that I have also tried 0 as the enumerated value for the collapse direction.
    wdDocRep.Tables(2).Rows(2).Cells(1).Range.PasteAndFormat Type:=WdRecoveryType.wdFormatOriginalFormatting 'It was intended that this should paste the clipboard content into the end of the appropriate cell with the original formatting utilised – Please note that I have also tried 16 as the enumeration of wdFormatOriginalFormatting.
    Next intCnt 'This ends the loop

    Outcome.
    When this program runs - on the first pass through it effectively copies the first cell material from D1 into the cell in D2. On the second pass through the loop, it replaces the first D1 content with the second D1 content. I originally tried this technique with InsertAfter and this worked, but it did not bring in the source formatting.
    Solution Requested. Can anyone please explain how to achieve the required outcome. In other words, I need to keep the content from the first D1 cell and then insert the second D1 cell content into this D2 cell as well. Most importantly, the intent is to keep the source formatting from D1. Your assistance in this matter will be very much appreciated. Thank you in advance for your help.

  2. #2
    You shouldn't need to copy and paste if you are using ranges. Create another range for the target cell then

    For intCnt = 1 To 2    'Start the counter loop
            Set rngCell = wdDocContent.Tables(intCnt).Rows(sngCnt1 + 2).Cells(2).Range    'This defines the range object from D1 that needs to be copied and pasted into the D2 report.
            rngCell.End = rngCell.End - 1    'This removes the cell end point, so it is just the content that is being copied.
            Set rngCell2 = wdDocRep.Tables(2).Rows(2).Cells(1).Range
            rngCell2.End = rngCell2.End - 1
            rngCell2.Collapse 0
            rngCell2.FormattedText = rngCell.FormattedText
    Next intCnt
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Apr 2020
    Location
    Perth, Western Australia
    Posts
    6
    Location
    Dear Graham,
    That worked brilliantly and I have now introduced this throughout the various stages of the program. The only change that I needed to add was to introduce a carriage return/line feed after the insertion for each iteration of the loop, except the last. This is because there was no paragraph end character for the last paragraph in the D1 cells.

    Thank you very much for your insights. This is very much appreciated. If I can ever repay your kindness, please do not hesitate to ask.

    Yours sincerely,
    Bruce Hilliard

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •