There is a program that displays a tabular report in Word. The program is in Delphi, the work is done through OleVariant, but this is not the point, the problem remains with the use of pure VBA.

The essence of the problem: when trying to access the column, the error "There is no access to individual columns because the table cells have different widths" is displayed

Sub Макрос2() ActiveDocument.Range.Text = "" '' //если следующие 2 строчки закомментировать - ошибка выходит далеко не всегда '' //как это связано - не знаю, но в коде Delphi этих строк нет, '' //а там ошибка появляется гарантированно. '' // оставляем как есть - ошибка проявляется всегда. Application.ActiveWindow.View.Type = wdNormalView Application.ActiveWindow.View.Type = wdWebView ActiveDocument.Range.Text = _ vbTab & "asdf asdf" & vbTab & vbTab & vbTab & "asdf asdf" & vbCrLf _ & vbTab & "asdf asdf" & vbTab & vbTab & vbTab & "asdf asdf" & vbCrLf _ & vbTab & "asdf asdf" & vbTab & vbTab & vbTab & "asdf asdf" & vbCrLf _ & vbTab & "asdf asdf" & vbTab & vbTab & vbTab & "asdf asdf" & vbCrLf _ ActiveDocument.Range.ConvertToTable (vbTab) '' //ошибка здесь, при доступе к Item(1) ActiveDocument.Tables(1).Columns.Item(1).Width = 11 ''// ActiveDocument.Tables(1).Columns(1).Width = 11 тоже не работает End Sub 

I looked through other questions about this error - they write everywhere that it is associated with the union of cells. However, in this code there is no union and the number of columns in the rows is identical!

Is it possible (and how) to get around this?

  • And what do you want to do this line? if you set the column width, then Columns.Item(1) with Columns(1) - Edward Izmalkov
  • @EduardIzmalkov doesn’t work either. The error is the same, it is played stably. - kami
  • Does VBA not work? What error gives? I work without errors. - Edward Izmalkov
  • hmm, very interesting. gives an error, but if you continue execution, it works. need to dig deeper. - Edward Izmalkov
  • @EduardIzmalkov Not working in VBA, and when accessing through OLEVariant in Delphi. Unfortunately, when issuing an error, the width of the column and other parameters (alignment, etc.) are not set, which negates all table formatting. - kami

1 answer 1

Before changing the column width, insert the code:

 ActiveDocument.Tables(1).AutoFitBehavior wdAutoFitFixed 

Parameters can be as follows: wdAutoFitFixed, wdAutoFitContent, wdAutoFitWindow . The first indicates that the width of the table is fixed, the second indicates selection by content, and the third indicates stretching the table to the width of the window. More details can be read on MSDN