Share via

Table format aligns left when using InsertFile method

Anonymous
2015-05-28T23:04:27+00:00

I have a problem where I insert 1000's of rtf files into one consolidated word document. I define the format of the consolidated file and start inserting all the document. The problem is when I insert the rtf file document which has data embedded into a word tables aligns left (after inserting). How to get the alignment as is from the input file? I do not want to use copy/paste preserve method as it is very slow for what I am doing.

I have attached sample rtf file I use as well as a sample macro that I use to consolidate. The sample macro is not the actual code I use. I use ranges and many lines of code to perform other things. But, the provided macro should give you an understanding of what I am trying to do.

Place the sample file.rtf in a folder. Run the below macro. Now, compare the sample file.rtf with the consolidated i.e. the inserted word document then you will see the inserted word document has its table aligned left when you highlight the table.

Please help.

https://drive.google.com/file/d/0B925g5AXvujTN3I1elVEZ09KSUE/view?usp=sharing

Sub Macro5()

'

' Macro5 Macro

'

'

    Selection.PageSetup.Orientation = wdOrientLandscape

    With Selection.PageSetup

        .LineNumbering.Active = False

        .Orientation = wdOrientLandscape

        .TopMargin = InchesToPoints(0.5)

        .BottomMargin = InchesToPoints(0.5)

        .LeftMargin = InchesToPoints(0.5)

        .RightMargin = InchesToPoints(0.5)

        .Gutter = InchesToPoints(0)

        .HeaderDistance = InchesToPoints(0.5)

        .FooterDistance = InchesToPoints(0.5)

        .PageWidth = InchesToPoints(11)

        .PageHeight = InchesToPoints(8.5)

        .FirstPageTray = wdPrinterDefaultBin

        .OtherPagesTray = wdPrinterDefaultBin

        .SectionStart = wdSectionNewPage

        .OddAndEvenPagesHeaderFooter = False

        .DifferentFirstPageHeaderFooter = False

        .VerticalAlignment = wdAlignVerticalTop

        .SuppressEndnotes = False

        .MirrorMargins = False

        .TwoPagesOnOne = False

        .BookFoldPrinting = False

        .BookFoldRevPrinting = False

        .BookFoldPrintingSheets = 1

        .GutterPos = wdGutterPosLeft

    End With

    Selection.Font.Name = "Times"

    Selection.Font.Size = 10

    Selection.InsertFile FileName:="sample file.rtf", Range:="", _

        ConfirmConversions:=False, Link:=False, Attachment:=False

    Selection.HomeKey Unit:=wdStory

    Selection.Delete Unit:=wdCharacter, Count:=1

    Selection.EndKey Unit:=wdStory

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

John Korchok 232.8K Reputation points Volunteer Moderator
2015-05-29T02:00:10+00:00

The table you're trying to import has some creative formatting that Word can't interpret. The centered inset portion of the table is connected to the rows above and below by a 0" width cell that is centered on the page.

I did the following: clicked the 0 width cell below the insert and used Table Tools>Layout>Split Table. Then I clicked at the left end of the inset and repeated. the Split Table command. The inset detached and popped back into a centered position.

You should be able revise your macro to search each table for 0 width cells and use the Split Table command to free them, then delete the 0 width cells to restore the spacing above and below the inset.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

John Korchok 232.8K Reputation points Volunteer Moderator
2015-05-29T18:01:27+00:00

This will do it. The target cells actually have a width of 1:

Sub FindNarrowCell()

  Dim oTable As Table, oCell As Cell

  Dim rngTable As Range, rngCell1 As Range

  For Each oTable In ActiveDocument.Tables

    Set rngTable = oTable.Range

    For Each oCell In rngTable.Cells

      If oCell.Width = 1 Then

        Set rngCell1 = oCell.Range

        With rngCell1

          .Collapse wdCollapseStart

          .Select

          Selection.SplitTable

          .MoveEnd wdCharacter, 3

          .MoveStart wdCharacter, 1

          .Cells(1).Delete

        End With

      End If

    Next oCell

  Next oTable

End Sub

Was this answer helpful?

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. John Korchok 232.8K Reputation points Volunteer Moderator
    2015-05-29T21:11:34+00:00

    Good idea, but Find doesn't have parameters to check cell width. My first try writing this used the Rows object, but Word complained it couldn't access rows because there were vertically merged cells (I couldn't find any.) So the macro has to test every cell in the table for width, slowing it down.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-05-29T20:57:44+00:00

    This works but runs slower as it scans about 1000 pages with 1000 tables. Is it possible to use "find" method to search for the single width cell and use range to split table and delete cells?

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-05-29T16:09:46+00:00

    Thank you John.

    That worked. What is the best method to find the zero width column in a table in the whole document?

    Was this answer helpful?

    0 comments No comments