Sub CreaFileWord2()
'Dim wdApp As Object
Dim wdDoc As Object
Dim startDate As Date
Dim i As Integer
Dim numRows As Integer
Dim tbl As Object
Dim row As Object
Dim col As Object
Dim inputDate As Date
Dim headerRange As Object
' Apre una finestra di dialogo per inserire la data di partenza
inputDate = InputBox("Inserisci la data di partenza (formato: gg/mm/aaaa)", "Data di
partenza", Day(Date) & "/" & Month(Date) & "/" & Year(Date))
If Not IsDate(inputDate) Then
MsgBox "Data non valida. Esecuzione interrotta."
Exit Sub
End If
' Imposta la data di inizio per il mese
startDate = DateSerial(Year(inputDate), Month(inputDate), 1)
' Calcola il numero di giorni nel mese
numRows = Day(DateSerial(Year(inputDate), Month(inputDate) + 1, 0))
nomemese = MonthName(Month(Date))
' Crea un nuovo documento Word
Set wdDoc = Documents.Add
With wdDoc.PageSetup
.LeftMargin = InchesToPoints(0.5)
.RightMargin = InchesToPoints(0.5)
.TopMargin = InchesToPoints(0.2)
.BottomMargin = InchesToPoints(0)
End With
Set tbl = wdDoc.Tables.Add(wdDoc.Range, numRows + 1, 3)
' Imposta la larghezza della prima colonna (ad esempio, 1 pollice)
Set col = tbl.Columns(1)
col.Width = InchesToPoints(1)
' Imposta la larghezza della seconda colonna (ad esempio, 4 pollici)
Set col = tbl.Columns(2)
col.Width = InchesToPoints(4)
' Imposta la larghezza della terza colonna (ad esempio, 0.5 pollici)
Set col = tbl.Columns(3)
col.Width = InchesToPoints(2)
' Imposta l'altezza delle righe
' Inserisci intestazioni della tabella
tbl.Cell(1, 1).Range.Text = "DATA"
tbl.Range.ParagraphFormat.Alignment = 1 ' Centrato
tbl.Cell(1, 2).Range.Text = "OPERAZIONE"
tbl.Range.ParagraphFormat.Alignment = 1 ' Centrato
tbl.Cell(1, 3).Range.Text = "EURO"
tbl.Range.ParagraphFormat.Alignment = 1 ' Centrato
' Popola la tabella con le date
For i = 2 To numRows + 1
ActiveDocument.Tables(1).Rows(1).Height = 10
tbl.Cell(i, 1).Range.Text = Format(startDate, "dd/mm/yyyy")
tbl.Cell(i, 1).Range.ParagraphFormat.Alignment = 1 ' Centro
tbl.Cell(i, 1).Range.Font.Size = 8
startDate = startDate + 1
Next i
tbl.Borders.Enable = True
' Imposta il documento come solo lettura
'wdDoc.Protect Type:=wdAllowOnlyReading, NoReset:=True
End Sub
Creare una tabella al volo su Word