Remove one line above after each table pasted from Excel to Word

mrwad

New Member
Joined
Oct 16, 2018
Messages
49
I have a code for writing Word documents from Excel. Currently after each table pasted to Word from Excel empty line appears in Word document. I was thinking is it possible to remove one line after each table pasted or are there any better suggestions?

My current code:

VBA Code:
    Set xlRng = ThisWorkbook.Sheets("Document").Range("G3", ThisWorkbook.Sheets("Document").Range("G" & Rows.Count).End(xlUp))

    Set wdRng = .Range.Characters.Last

    For Each Cell In xlRng
        wdRng.InsertAfter vbCr & Cell.Offset(0, -5).Text
        Select Case LCase(Cell.Value)

        Case "table6"
          ThisWorkbook.Sheets("Tables").Range("B817:C820").Copy
            With wdRng
            Set rngPara = .Paragraphs.Last.Range
            rngPara.Style = "Data"
            rngPara.PasteExcelTable False, False, False
            .Tables(.Tables.Count).Range.Paragraphs.Indent
            .Font.Hidden = 0
            '.Range.Paragraphs(-1).Range.Delete
            Set rngPara = Nothing
            End With

        End Select
    Next Cell

I have tried to use `.Range.Paragraphs(-1).Range.Delete` (commented out in my code) without success. What is the correct command for that kind of operation?

Even solution for removing previous paragraph would help. I can create own case for it and perform this command after each table.

I have seen this answer by widely respected Macropod for default solution. I am hoping there are might be something that can be done with VBA

Here is full code if somebody want to test:

VBA Code:
    Sub opentemplateWord()
        Dim Paragraphe As Object, WordApp As Object, WordDoc As Object
        Dim wSystem As Worksheet
        Dim Cell As Range
        Dim wdRng As Object 'Word.Range
        Dim xlRng As Excel.Range
        Dim tempFolderPath As String
        Dim filePath As String
        Dim fileTitle As String
        Dim rngPara As Object
 
        'Application.ScreenUpdating = False
     
        On Error GoTo ErrorHandlerEndExecution
 
        Set wSystem = ThisWorkbook.Sheets("Templates")
     
        Dim File: File = Environ("Temp") & "\" & "Document_template" & ".docx"
        'creationsession Word
        Set WordApp = CreateObject("Word.Application")
        'word ll be close to run
        WordApp.Visible = False
        'open the file .doc
        Set WordDoc = WordApp.Documents.Open(File)
     
        With WordDoc
 
        Set xlRng = ThisWorkbook.Sheets("Document").Range("G3", ThisWorkbook.Sheets("Document").Range("G" & Rows.Count).End(xlUp))
 
        Set wdRng = .Range.Characters.Last
 
        For Each Cell In xlRng
            wdRng.InsertAfter vbCr & Cell.Offset(0, -5).Text
            Select Case LCase(Cell.Value)
             
            Case "table6"
              ThisWorkbook.Sheets("Tables").Range("B817:C820").Copy
                With wdRng
                Set rngPara = .Paragraphs.Last.Range
                rngPara.Style = "Data"
                rngPara.PasteExcelTable False, False, False
                .Tables(.Tables.Count).Range.Paragraphs.Indent
                .Font.Hidden = 0
                '.Paragraphs.Last.Range.Delete
                Set rngPara = Nothing
                End With
           
            End Select
        Next Cell
     
            WordDoc.SaveAs2 Environ$("Temp") & "\" & _
                    "Test" & ".docx"
 
        End With
     
        WordDoc.Close
        WordApp.Quit
        Set WordDoc = Nothing
        Set WordApp = Nothing
     
        Exit Sub
     
    ErrorHandlerEndExecution:
 
        WordDoc.Close
        WordApp.Quit
        Set WordDoc = Nothing
        Set WordApp = Nothing
     
        'Application.ScreenUpdating = True
    End Sub

Asked also here: Remove one line above after each table pasted from Excel to Word
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,214,518
Messages
6,119,996
Members
448,935
Latest member
ijat

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top