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 this table



What i want to achieve is: this.



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









share|improve this question
























  • 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















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 this table



What i want to achieve is: this.



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









share|improve this question
























  • 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













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 this table



What i want to achieve is: this.



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









share|improve this question















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 this table



What i want to achieve is: this.



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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..






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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..






share|improve this answer





















  • 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















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..






share|improve this answer





















  • 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













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..






share|improve this answer












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..







share|improve this answer












share|improve this answer



share|improve this answer










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 = wdPreferredWidthPercent for 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










  • 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
















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word