Table AutoFitBehavior does not work in Word Document
up vote
0
down vote
favorite
i am trying to develop a print function in my Java Swing Desktop Application. All i need is a basic printing, so there is no need of an external library or something. My app, is only for windows, so there is nothing to stop me for using a VBS file in order to generate a Word (.docx) document and just print it.
The thing is that i need to print a table. My app generates, the following script (ignore the fields, texts and etc).
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
objWord.Visible = False
Set objSelection = objWord.Selection
Set objSection = objDoc.Sections(1)
objDoc.Sections(1).Footers(1).Range.ParagraphFormat.Alignment = 0
objDoc.Sections(1).Footers(1).Range.Bold = True
objDoc.Sections(1).Footers(1).Range.Text = ""
objSelection.Font.Bold = True
objSelection.Font.Underline = True
objSelection.ParagraphFormat.Alignment = 1
objSelection.Font.Size = "14"
objSelection.TypeText "I am a title"
objSelection.TypeParagraph()
objSelection.Font.Underline = False
objSelection.Font.Size = "12"
Const NUMBER_OF_ROWS = 2
Const NUMBER_OF_COLUMNS = 4
Set objRange = objDoc.Range()
objrange.Collapse wdCollapseEnd
objDoc.Tables.Add objRange, NUMBER_OF_ROWS, NUMBER_OF_COLUMNS
Set objTable = objDoc.Tables(1)
objTable.Style = "Πλέγμα Πίνακα" 'Greek MS Word needs style name on greek. "Table Grid" is the english one
objTable.Cell(1, 1).Range.Text = "Description"
objTable.Cell(1, 2).Range.Text = "Date"
objTable.Cell(1, 3).Range.Text = "Count"
objTable.Cell(1, 4).Range.Text = "Notes"
objTable.Cell(2, 1).Range.Text = "Hello there i am"+Chr(13)+"a big text"+Chr(13)+"but i am far bigger than you"
objTable.Cell(2, 2).Range.Text = "10/11/2018"
objTable.Cell(2, 3).Range.Text = "10.000"
objTable.Cell(2, 4).Range.Text = "nothing here"
objSelection.Tables(1).Select
objSelection.Collapse wdCollapseEnd
objDoc.SaveAs("C:UsersGeorgeAppDataLocalTempAAA - 13213908551013013192846.docx")
objWord.Quit
With this script, i get 
What i want to achieve is: 
In order to get the table which is in the second image, i did the following steps from MS Word. Right click in the table - > Auto Adjust - > Auto Adjust with the contents, then again, Right click in the table - > Auto Adjust -> Auto Adjust in the window.
So, i guessed recording these steps via "Create Macro" action from MS Word will give me the answer. It "gave" me these lines:
objSelection.Tables(1).AutoFitBehavior (wdAutoFitContent)
objSelection.Tables(1).AutoFitBehavior (wdAutoFitWindow)
I added them in my script, right before "Save as..." but there is not any changes to the table. So....I was wondering, is there anything more should i do, or do i do something wrong? I am not familiar with VBA.
OS: Windows 10 x64
MS-Office: Microsoft Office Professional Plus 2016
EDIT - Answer:
objSelection.Tables(1).AutoFitBehavior (1) 'word VBA does not support enumerations
objSelection.Tables(1).Rows.Alignment = 1
vba vbscript ms-word word-vba
|
show 5 more comments
up vote
0
down vote
favorite
i am trying to develop a print function in my Java Swing Desktop Application. All i need is a basic printing, so there is no need of an external library or something. My app, is only for windows, so there is nothing to stop me for using a VBS file in order to generate a Word (.docx) document and just print it.
The thing is that i need to print a table. My app generates, the following script (ignore the fields, texts and etc).
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
objWord.Visible = False
Set objSelection = objWord.Selection
Set objSection = objDoc.Sections(1)
objDoc.Sections(1).Footers(1).Range.ParagraphFormat.Alignment = 0
objDoc.Sections(1).Footers(1).Range.Bold = True
objDoc.Sections(1).Footers(1).Range.Text = ""
objSelection.Font.Bold = True
objSelection.Font.Underline = True
objSelection.ParagraphFormat.Alignment = 1
objSelection.Font.Size = "14"
objSelection.TypeText "I am a title"
objSelection.TypeParagraph()
objSelection.Font.Underline = False
objSelection.Font.Size = "12"
Const NUMBER_OF_ROWS = 2
Const NUMBER_OF_COLUMNS = 4
Set objRange = objDoc.Range()
objrange.Collapse wdCollapseEnd
objDoc.Tables.Add objRange, NUMBER_OF_ROWS, NUMBER_OF_COLUMNS
Set objTable = objDoc.Tables(1)
objTable.Style = "Πλέγμα Πίνακα" 'Greek MS Word needs style name on greek. "Table Grid" is the english one
objTable.Cell(1, 1).Range.Text = "Description"
objTable.Cell(1, 2).Range.Text = "Date"
objTable.Cell(1, 3).Range.Text = "Count"
objTable.Cell(1, 4).Range.Text = "Notes"
objTable.Cell(2, 1).Range.Text = "Hello there i am"+Chr(13)+"a big text"+Chr(13)+"but i am far bigger than you"
objTable.Cell(2, 2).Range.Text = "10/11/2018"
objTable.Cell(2, 3).Range.Text = "10.000"
objTable.Cell(2, 4).Range.Text = "nothing here"
objSelection.Tables(1).Select
objSelection.Collapse wdCollapseEnd
objDoc.SaveAs("C:UsersGeorgeAppDataLocalTempAAA - 13213908551013013192846.docx")
objWord.Quit
With this script, i get 
What i want to achieve is: 
In order to get the table which is in the second image, i did the following steps from MS Word. Right click in the table - > Auto Adjust - > Auto Adjust with the contents, then again, Right click in the table - > Auto Adjust -> Auto Adjust in the window.
So, i guessed recording these steps via "Create Macro" action from MS Word will give me the answer. It "gave" me these lines:
objSelection.Tables(1).AutoFitBehavior (wdAutoFitContent)
objSelection.Tables(1).AutoFitBehavior (wdAutoFitWindow)
I added them in my script, right before "Save as..." but there is not any changes to the table. So....I was wondering, is there anything more should i do, or do i do something wrong? I am not familiar with VBA.
OS: Windows 10 x64
MS-Office: Microsoft Office Professional Plus 2016
EDIT - Answer:
objSelection.Tables(1).AutoFitBehavior (1) 'word VBA does not support enumerations
objSelection.Tables(1).Rows.Alignment = 1
vba vbscript ms-word word-vba
Put them before the collapse statement
– Freeflow
Nov 10 at 13:47
@Freeflow i have already tried to do it. The result is the same.
– George Zougianos
Nov 10 at 13:51
I tried objSelection.Tables(1).Select objSelection.Tables(1).AutoFitBehavior (wdAutoFitContent) objSelection.Tables(1).AutoFitBehavior (wdAutoFitWindow) objSelection.Collapse wdCollapseEnd and it adjusts the size of the table just fine on my PC Office 365 64 bit Word 2016.
– Freeflow
Nov 10 at 21:55
@Freeflow Could you upload the full code in pastebin? Also did you try this specific text content as mine? Also, how do you run the script? Maybe inside from MS-Word it has different behavior than double-clicking on it.
– George Zougianos
Nov 10 at 21:59
1
On further investigation I found that vbscript does not use the Word enumerations so the wdAutofitXXXX enumerations are being translated as 0 and the autofit behaviour is wdAutoFitFixed.. If you change wdAutoFitContent to 1 or wdAutoFitWidow to 2 the table should auto fit. BUT, you only need one of those lines.
– Freeflow
Nov 11 at 11:21
|
show 5 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
i am trying to develop a print function in my Java Swing Desktop Application. All i need is a basic printing, so there is no need of an external library or something. My app, is only for windows, so there is nothing to stop me for using a VBS file in order to generate a Word (.docx) document and just print it.
The thing is that i need to print a table. My app generates, the following script (ignore the fields, texts and etc).
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
objWord.Visible = False
Set objSelection = objWord.Selection
Set objSection = objDoc.Sections(1)
objDoc.Sections(1).Footers(1).Range.ParagraphFormat.Alignment = 0
objDoc.Sections(1).Footers(1).Range.Bold = True
objDoc.Sections(1).Footers(1).Range.Text = ""
objSelection.Font.Bold = True
objSelection.Font.Underline = True
objSelection.ParagraphFormat.Alignment = 1
objSelection.Font.Size = "14"
objSelection.TypeText "I am a title"
objSelection.TypeParagraph()
objSelection.Font.Underline = False
objSelection.Font.Size = "12"
Const NUMBER_OF_ROWS = 2
Const NUMBER_OF_COLUMNS = 4
Set objRange = objDoc.Range()
objrange.Collapse wdCollapseEnd
objDoc.Tables.Add objRange, NUMBER_OF_ROWS, NUMBER_OF_COLUMNS
Set objTable = objDoc.Tables(1)
objTable.Style = "Πλέγμα Πίνακα" 'Greek MS Word needs style name on greek. "Table Grid" is the english one
objTable.Cell(1, 1).Range.Text = "Description"
objTable.Cell(1, 2).Range.Text = "Date"
objTable.Cell(1, 3).Range.Text = "Count"
objTable.Cell(1, 4).Range.Text = "Notes"
objTable.Cell(2, 1).Range.Text = "Hello there i am"+Chr(13)+"a big text"+Chr(13)+"but i am far bigger than you"
objTable.Cell(2, 2).Range.Text = "10/11/2018"
objTable.Cell(2, 3).Range.Text = "10.000"
objTable.Cell(2, 4).Range.Text = "nothing here"
objSelection.Tables(1).Select
objSelection.Collapse wdCollapseEnd
objDoc.SaveAs("C:UsersGeorgeAppDataLocalTempAAA - 13213908551013013192846.docx")
objWord.Quit
With this script, i get 
What i want to achieve is: 
In order to get the table which is in the second image, i did the following steps from MS Word. Right click in the table - > Auto Adjust - > Auto Adjust with the contents, then again, Right click in the table - > Auto Adjust -> Auto Adjust in the window.
So, i guessed recording these steps via "Create Macro" action from MS Word will give me the answer. It "gave" me these lines:
objSelection.Tables(1).AutoFitBehavior (wdAutoFitContent)
objSelection.Tables(1).AutoFitBehavior (wdAutoFitWindow)
I added them in my script, right before "Save as..." but there is not any changes to the table. So....I was wondering, is there anything more should i do, or do i do something wrong? I am not familiar with VBA.
OS: Windows 10 x64
MS-Office: Microsoft Office Professional Plus 2016
EDIT - Answer:
objSelection.Tables(1).AutoFitBehavior (1) 'word VBA does not support enumerations
objSelection.Tables(1).Rows.Alignment = 1
vba vbscript ms-word word-vba
i am trying to develop a print function in my Java Swing Desktop Application. All i need is a basic printing, so there is no need of an external library or something. My app, is only for windows, so there is nothing to stop me for using a VBS file in order to generate a Word (.docx) document and just print it.
The thing is that i need to print a table. My app generates, the following script (ignore the fields, texts and etc).
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
objWord.Visible = False
Set objSelection = objWord.Selection
Set objSection = objDoc.Sections(1)
objDoc.Sections(1).Footers(1).Range.ParagraphFormat.Alignment = 0
objDoc.Sections(1).Footers(1).Range.Bold = True
objDoc.Sections(1).Footers(1).Range.Text = ""
objSelection.Font.Bold = True
objSelection.Font.Underline = True
objSelection.ParagraphFormat.Alignment = 1
objSelection.Font.Size = "14"
objSelection.TypeText "I am a title"
objSelection.TypeParagraph()
objSelection.Font.Underline = False
objSelection.Font.Size = "12"
Const NUMBER_OF_ROWS = 2
Const NUMBER_OF_COLUMNS = 4
Set objRange = objDoc.Range()
objrange.Collapse wdCollapseEnd
objDoc.Tables.Add objRange, NUMBER_OF_ROWS, NUMBER_OF_COLUMNS
Set objTable = objDoc.Tables(1)
objTable.Style = "Πλέγμα Πίνακα" 'Greek MS Word needs style name on greek. "Table Grid" is the english one
objTable.Cell(1, 1).Range.Text = "Description"
objTable.Cell(1, 2).Range.Text = "Date"
objTable.Cell(1, 3).Range.Text = "Count"
objTable.Cell(1, 4).Range.Text = "Notes"
objTable.Cell(2, 1).Range.Text = "Hello there i am"+Chr(13)+"a big text"+Chr(13)+"but i am far bigger than you"
objTable.Cell(2, 2).Range.Text = "10/11/2018"
objTable.Cell(2, 3).Range.Text = "10.000"
objTable.Cell(2, 4).Range.Text = "nothing here"
objSelection.Tables(1).Select
objSelection.Collapse wdCollapseEnd
objDoc.SaveAs("C:UsersGeorgeAppDataLocalTempAAA - 13213908551013013192846.docx")
objWord.Quit
With this script, i get 
What i want to achieve is: 
In order to get the table which is in the second image, i did the following steps from MS Word. Right click in the table - > Auto Adjust - > Auto Adjust with the contents, then again, Right click in the table - > Auto Adjust -> Auto Adjust in the window.
So, i guessed recording these steps via "Create Macro" action from MS Word will give me the answer. It "gave" me these lines:
objSelection.Tables(1).AutoFitBehavior (wdAutoFitContent)
objSelection.Tables(1).AutoFitBehavior (wdAutoFitWindow)
I added them in my script, right before "Save as..." but there is not any changes to the table. So....I was wondering, is there anything more should i do, or do i do something wrong? I am not familiar with VBA.
OS: Windows 10 x64
MS-Office: Microsoft Office Professional Plus 2016
EDIT - Answer:
objSelection.Tables(1).AutoFitBehavior (1) 'word VBA does not support enumerations
objSelection.Tables(1).Rows.Alignment = 1
vba vbscript ms-word word-vba
vba vbscript ms-word word-vba
edited Nov 11 at 11:42
asked Nov 10 at 11:41
George Zougianos
359210
359210
Put them before the collapse statement
– Freeflow
Nov 10 at 13:47
@Freeflow i have already tried to do it. The result is the same.
– George Zougianos
Nov 10 at 13:51
I tried objSelection.Tables(1).Select objSelection.Tables(1).AutoFitBehavior (wdAutoFitContent) objSelection.Tables(1).AutoFitBehavior (wdAutoFitWindow) objSelection.Collapse wdCollapseEnd and it adjusts the size of the table just fine on my PC Office 365 64 bit Word 2016.
– Freeflow
Nov 10 at 21:55
@Freeflow Could you upload the full code in pastebin? Also did you try this specific text content as mine? Also, how do you run the script? Maybe inside from MS-Word it has different behavior than double-clicking on it.
– George Zougianos
Nov 10 at 21:59
1
On further investigation I found that vbscript does not use the Word enumerations so the wdAutofitXXXX enumerations are being translated as 0 and the autofit behaviour is wdAutoFitFixed.. If you change wdAutoFitContent to 1 or wdAutoFitWidow to 2 the table should auto fit. BUT, you only need one of those lines.
– Freeflow
Nov 11 at 11:21
|
show 5 more comments
Put them before the collapse statement
– Freeflow
Nov 10 at 13:47
@Freeflow i have already tried to do it. The result is the same.
– George Zougianos
Nov 10 at 13:51
I tried objSelection.Tables(1).Select objSelection.Tables(1).AutoFitBehavior (wdAutoFitContent) objSelection.Tables(1).AutoFitBehavior (wdAutoFitWindow) objSelection.Collapse wdCollapseEnd and it adjusts the size of the table just fine on my PC Office 365 64 bit Word 2016.
– Freeflow
Nov 10 at 21:55
@Freeflow Could you upload the full code in pastebin? Also did you try this specific text content as mine? Also, how do you run the script? Maybe inside from MS-Word it has different behavior than double-clicking on it.
– George Zougianos
Nov 10 at 21:59
1
On further investigation I found that vbscript does not use the Word enumerations so the wdAutofitXXXX enumerations are being translated as 0 and the autofit behaviour is wdAutoFitFixed.. If you change wdAutoFitContent to 1 or wdAutoFitWidow to 2 the table should auto fit. BUT, you only need one of those lines.
– Freeflow
Nov 11 at 11:21
Put them before the collapse statement
– Freeflow
Nov 10 at 13:47
Put them before the collapse statement
– Freeflow
Nov 10 at 13:47
@Freeflow i have already tried to do it. The result is the same.
– George Zougianos
Nov 10 at 13:51
@Freeflow i have already tried to do it. The result is the same.
– George Zougianos
Nov 10 at 13:51
I tried objSelection.Tables(1).Select objSelection.Tables(1).AutoFitBehavior (wdAutoFitContent) objSelection.Tables(1).AutoFitBehavior (wdAutoFitWindow) objSelection.Collapse wdCollapseEnd and it adjusts the size of the table just fine on my PC Office 365 64 bit Word 2016.
– Freeflow
Nov 10 at 21:55
I tried objSelection.Tables(1).Select objSelection.Tables(1).AutoFitBehavior (wdAutoFitContent) objSelection.Tables(1).AutoFitBehavior (wdAutoFitWindow) objSelection.Collapse wdCollapseEnd and it adjusts the size of the table just fine on my PC Office 365 64 bit Word 2016.
– Freeflow
Nov 10 at 21:55
@Freeflow Could you upload the full code in pastebin? Also did you try this specific text content as mine? Also, how do you run the script? Maybe inside from MS-Word it has different behavior than double-clicking on it.
– George Zougianos
Nov 10 at 21:59
@Freeflow Could you upload the full code in pastebin? Also did you try this specific text content as mine? Also, how do you run the script? Maybe inside from MS-Word it has different behavior than double-clicking on it.
– George Zougianos
Nov 10 at 21:59
1
1
On further investigation I found that vbscript does not use the Word enumerations so the wdAutofitXXXX enumerations are being translated as 0 and the autofit behaviour is wdAutoFitFixed.. If you change wdAutoFitContent to 1 or wdAutoFitWidow to 2 the table should auto fit. BUT, you only need one of those lines.
– Freeflow
Nov 11 at 11:21
On further investigation I found that vbscript does not use the Word enumerations so the wdAutofitXXXX enumerations are being translated as 0 and the autofit behaviour is wdAutoFitFixed.. If you change wdAutoFitContent to 1 or wdAutoFitWidow to 2 the table should auto fit. BUT, you only need one of those lines.
– Freeflow
Nov 11 at 11:21
|
show 5 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
You could try setting the widths of the columns yourself. Something like this perhaps:
Set objTable = objDoc.Tables(1)
With objTable
.Style = "Πλέγμα Πίνακα"
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
.Columns(1).Width = 40
.Columns(2).Width = 20
.Columns(3).Width = 15
.Columns(4).Width = 25
.Cell(1, 1).Range.Text = "Description"
.Cell(1, 2).Range.Text = "Date"
.Cell(1, 3).Range.Text = "Count"
.Cell(1, 4).Range.Text = "Notes"
.Cell(2, 1).Range.Text = "Hello there i am"+Chr(13)+"a big text"+Chr(13)+"but i am far bigger than you"
.Cell(2, 2).Range.Text = "10/11/2018"
.Cell(2, 3).Range.Text = "10.000"
.Cell(2, 4).Range.Text = "nothing here"
.AutoFitBehavior (wdAutoFitWindow)
End With
You wil have to experiment with the percentages of course before you think it looks good..
What i get is this. i.imgur.com/JyKcDvG.png Working with percentage could work for me, but as it seems it does not work either.
– George Zougianos
Nov 10 at 20:45
Sizing the columns in a Word table seems like a nightmare.. I'm not sure, but perhaps you need to set the.PreferredWidthType = wdPreferredWidthPercentfor each column aswell, so.Columns(1).PreferredWidthType = wdPreferredWidthPercent...Columns(4).PreferredWidthType = wdPreferredWidthPercent
– Theo
Nov 11 at 11:03
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You could try setting the widths of the columns yourself. Something like this perhaps:
Set objTable = objDoc.Tables(1)
With objTable
.Style = "Πλέγμα Πίνακα"
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
.Columns(1).Width = 40
.Columns(2).Width = 20
.Columns(3).Width = 15
.Columns(4).Width = 25
.Cell(1, 1).Range.Text = "Description"
.Cell(1, 2).Range.Text = "Date"
.Cell(1, 3).Range.Text = "Count"
.Cell(1, 4).Range.Text = "Notes"
.Cell(2, 1).Range.Text = "Hello there i am"+Chr(13)+"a big text"+Chr(13)+"but i am far bigger than you"
.Cell(2, 2).Range.Text = "10/11/2018"
.Cell(2, 3).Range.Text = "10.000"
.Cell(2, 4).Range.Text = "nothing here"
.AutoFitBehavior (wdAutoFitWindow)
End With
You wil have to experiment with the percentages of course before you think it looks good..
What i get is this. i.imgur.com/JyKcDvG.png Working with percentage could work for me, but as it seems it does not work either.
– George Zougianos
Nov 10 at 20:45
Sizing the columns in a Word table seems like a nightmare.. I'm not sure, but perhaps you need to set the.PreferredWidthType = wdPreferredWidthPercentfor each column aswell, so.Columns(1).PreferredWidthType = wdPreferredWidthPercent...Columns(4).PreferredWidthType = wdPreferredWidthPercent
– Theo
Nov 11 at 11:03
add a comment |
up vote
0
down vote
You could try setting the widths of the columns yourself. Something like this perhaps:
Set objTable = objDoc.Tables(1)
With objTable
.Style = "Πλέγμα Πίνακα"
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
.Columns(1).Width = 40
.Columns(2).Width = 20
.Columns(3).Width = 15
.Columns(4).Width = 25
.Cell(1, 1).Range.Text = "Description"
.Cell(1, 2).Range.Text = "Date"
.Cell(1, 3).Range.Text = "Count"
.Cell(1, 4).Range.Text = "Notes"
.Cell(2, 1).Range.Text = "Hello there i am"+Chr(13)+"a big text"+Chr(13)+"but i am far bigger than you"
.Cell(2, 2).Range.Text = "10/11/2018"
.Cell(2, 3).Range.Text = "10.000"
.Cell(2, 4).Range.Text = "nothing here"
.AutoFitBehavior (wdAutoFitWindow)
End With
You wil have to experiment with the percentages of course before you think it looks good..
What i get is this. i.imgur.com/JyKcDvG.png Working with percentage could work for me, but as it seems it does not work either.
– George Zougianos
Nov 10 at 20:45
Sizing the columns in a Word table seems like a nightmare.. I'm not sure, but perhaps you need to set the.PreferredWidthType = wdPreferredWidthPercentfor each column aswell, so.Columns(1).PreferredWidthType = wdPreferredWidthPercent...Columns(4).PreferredWidthType = wdPreferredWidthPercent
– Theo
Nov 11 at 11:03
add a comment |
up vote
0
down vote
up vote
0
down vote
You could try setting the widths of the columns yourself. Something like this perhaps:
Set objTable = objDoc.Tables(1)
With objTable
.Style = "Πλέγμα Πίνακα"
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
.Columns(1).Width = 40
.Columns(2).Width = 20
.Columns(3).Width = 15
.Columns(4).Width = 25
.Cell(1, 1).Range.Text = "Description"
.Cell(1, 2).Range.Text = "Date"
.Cell(1, 3).Range.Text = "Count"
.Cell(1, 4).Range.Text = "Notes"
.Cell(2, 1).Range.Text = "Hello there i am"+Chr(13)+"a big text"+Chr(13)+"but i am far bigger than you"
.Cell(2, 2).Range.Text = "10/11/2018"
.Cell(2, 3).Range.Text = "10.000"
.Cell(2, 4).Range.Text = "nothing here"
.AutoFitBehavior (wdAutoFitWindow)
End With
You wil have to experiment with the percentages of course before you think it looks good..
You could try setting the widths of the columns yourself. Something like this perhaps:
Set objTable = objDoc.Tables(1)
With objTable
.Style = "Πλέγμα Πίνακα"
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
.Columns(1).Width = 40
.Columns(2).Width = 20
.Columns(3).Width = 15
.Columns(4).Width = 25
.Cell(1, 1).Range.Text = "Description"
.Cell(1, 2).Range.Text = "Date"
.Cell(1, 3).Range.Text = "Count"
.Cell(1, 4).Range.Text = "Notes"
.Cell(2, 1).Range.Text = "Hello there i am"+Chr(13)+"a big text"+Chr(13)+"but i am far bigger than you"
.Cell(2, 2).Range.Text = "10/11/2018"
.Cell(2, 3).Range.Text = "10.000"
.Cell(2, 4).Range.Text = "nothing here"
.AutoFitBehavior (wdAutoFitWindow)
End With
You wil have to experiment with the percentages of course before you think it looks good..
answered Nov 10 at 14:48
Theo
2,8011518
2,8011518
What i get is this. i.imgur.com/JyKcDvG.png Working with percentage could work for me, but as it seems it does not work either.
– George Zougianos
Nov 10 at 20:45
Sizing the columns in a Word table seems like a nightmare.. I'm not sure, but perhaps you need to set the.PreferredWidthType = wdPreferredWidthPercentfor each column aswell, so.Columns(1).PreferredWidthType = wdPreferredWidthPercent...Columns(4).PreferredWidthType = wdPreferredWidthPercent
– Theo
Nov 11 at 11:03
add a comment |
What i get is this. i.imgur.com/JyKcDvG.png Working with percentage could work for me, but as it seems it does not work either.
– George Zougianos
Nov 10 at 20:45
Sizing the columns in a Word table seems like a nightmare.. I'm not sure, but perhaps you need to set the.PreferredWidthType = wdPreferredWidthPercentfor each column aswell, so.Columns(1).PreferredWidthType = wdPreferredWidthPercent...Columns(4).PreferredWidthType = wdPreferredWidthPercent
– Theo
Nov 11 at 11:03
What i get is this. i.imgur.com/JyKcDvG.png Working with percentage could work for me, but as it seems it does not work either.
– George Zougianos
Nov 10 at 20:45
What i get is this. i.imgur.com/JyKcDvG.png Working with percentage could work for me, but as it seems it does not work either.
– George Zougianos
Nov 10 at 20:45
Sizing the columns in a Word table seems like a nightmare.. I'm not sure, but perhaps you need to set the
.PreferredWidthType = wdPreferredWidthPercent for each column aswell, so .Columns(1).PreferredWidthType = wdPreferredWidthPercent .. .Columns(4).PreferredWidthType = wdPreferredWidthPercent– Theo
Nov 11 at 11:03
Sizing the columns in a Word table seems like a nightmare.. I'm not sure, but perhaps you need to set the
.PreferredWidthType = wdPreferredWidthPercent for each column aswell, so .Columns(1).PreferredWidthType = wdPreferredWidthPercent .. .Columns(4).PreferredWidthType = wdPreferredWidthPercent– Theo
Nov 11 at 11:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238577%2ftable-autofitbehavior-does-not-work-in-word-document%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Put them before the collapse statement
– Freeflow
Nov 10 at 13:47
@Freeflow i have already tried to do it. The result is the same.
– George Zougianos
Nov 10 at 13:51
I tried objSelection.Tables(1).Select objSelection.Tables(1).AutoFitBehavior (wdAutoFitContent) objSelection.Tables(1).AutoFitBehavior (wdAutoFitWindow) objSelection.Collapse wdCollapseEnd and it adjusts the size of the table just fine on my PC Office 365 64 bit Word 2016.
– Freeflow
Nov 10 at 21:55
@Freeflow Could you upload the full code in pastebin? Also did you try this specific text content as mine? Also, how do you run the script? Maybe inside from MS-Word it has different behavior than double-clicking on it.
– George Zougianos
Nov 10 at 21:59
1
On further investigation I found that vbscript does not use the Word enumerations so the wdAutofitXXXX enumerations are being translated as 0 and the autofit behaviour is wdAutoFitFixed.. If you change wdAutoFitContent to 1 or wdAutoFitWidow to 2 the table should auto fit. BUT, you only need one of those lines.
– Freeflow
Nov 11 at 11:21