Ecco: questa la devo fare in tempi rapidi.
Creare una tabella di 35 colonne e 31 righe.
Questo comando dà al foglio un orientamento orizzontale.
ActiveDocument.PageSetup.Orientation = wdOrientLandscape
e ho trovato anche l'inverso, quello che lo riporta verticale:
ActiveDocument.PageSetup.Orientation = wdOrientPortrait.
Comunque il foglio mi serve Landscape.
Ora tracciamo la tabella tutta a celle uguali.
Ne inizio la registrazione cercando di ricordare le modalità applicate prima a causa del "mouse invalido" che si crea durante la registrazione delle macro...
Ecco tutta la macro:
Sub tabella()
'
' tabella Macro
' Macro registrata il 23/11/2018 da Windows User
'
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=31, NumColumns _
:=35, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitWindow
With Selection.Tables(1)
If .Style <> "Griglia tabella" Then
.Style = "Griglia tabella"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
End Sub
Cominciamo a spezzettarla...
Ecco, ho individuato un codice minimo che aggiunge una tabella a un foglio:
Sub tabella()
'
' tabella Macro
' Macro registrata il 23/11/2018 da Windows User
'
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=31, NumColumns _
:=35, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitWindow
End Sub
Devo vedere cosa significa
DefaultTableBehavior...
Rivedo il codice completo:
Sub tabella()
'
' tabella Macro
' Macro registrata il 23/11/2018 da Windows User
'
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=31, NumColumns _
:=35, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitWindow
With Selection.Tables(1)
If .Style <> "Griglia tabella" Then
.Style = "Griglia tabella"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
End Sub
Vediamo il form per la creazione della tabella dove c'è la scelta fra varie opzioni, alle quali sicuramente si riferisce gran parte del codice "non essenziale".
Mi pare che avevo scelto "Adatta alla pagina", ma adesso ci vado in modo sistematico: andiamo con queste impostazioni di default:
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Griglia tabella" Then
.Style = "Griglia tabella"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Ora modifico "Larghezza fissa colonne" che porto da "Auto" a 1.5:
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
.Columns.PreferredWidth = CentimetersToPoints(1.5)
If .Style <> "Griglia tabella" Then
.Style = "Griglia tabella"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Ora modifico la seconda voce: scelgo "Adatta al contenuto":
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitContent
With Selection.Tables(1)
If .Style <> "Griglia tabella" Then
.Style = "Griglia tabella"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Mentre in quelle precedenti era:
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
Scegliendo l'opzione "Adatta alla pagina":
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitWindow
With Selection.Tables(1)
If .Style <> "Griglia tabella" Then
.Style = "Griglia tabella"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Ecco.
Schiacciando "Griglia tabella" si apre questa finestra:
...nella quale sono comprese queste istruzioni:
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Tabella a colori 1" Then
.Style = "Tabella a colori 1"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Ormai è quasi tutto chiaro.