PDA

View Full Version : VBA changed font size in word table



Simsalabim
10-10-2018, 04:31 AM
Hello,

my code copies some excel data into a word dokument with a table with 4 cloumns and 9 rows. It is important, that the cell size is not changed, but all the input data is in the cell. My solution is, that i check the lenght of the input-data and then reduce the font size in the word table cell only. This is the code for this:


If Len(wbsource.Sheets(1).Cells(j, 3)) < breitSize1 Then
appWord.ActiveDocument.Tables(1).Cell(2, 1).Range.Font.Size = breitFont1

appWord.ActiveDocument.Bookmarks("C1").Range.Text = wbsource.Sheets("Sheet1").Cells(j, 3)

My problem is, it changes the font size of the whole column. Has anybody an idea, why this can happen. I tried this before with another word dokument and it worked, but the actual word dokument is made by somebody else.


Thanks in advance!

macropod
10-10-2018, 02:25 PM
The code you posted applies breitFont1 to only one cell, not to a whole column, and inserts only a single Excel cell at a 'C1' bookmark in the document - without applying any formatting.

gmayor
10-14-2018, 04:44 AM
It looks as though this code is run from Excel and it is not know what the various variables and bookmark refer to.
In order to format only the range in the cell, using Word vba from Excel you need something like


Dim oCell As Object
Set oCell = appWord.ActiveDocument.Tables(1).Cell(2, 1).Range
With oCell
.End = .End - 1 'remove the cell end character from the range
.Text = "This is the content of the range"
.Font.Size = 8 'the font size of the range
End With