Una famiglia di prodotti software per l'elaborazione di testo Microsoft per la creazione di documenti Web, messaggi di posta elettronica e stampa.
Ciao peppeZH,
ho caricato un file Word di esempio qui.
Il testo si presenta così:
dove le parentesi quadre indicano la presenza di segnalibri. Per nasconderle:
File > Opzioni
Impostazioni avanzate
Sezione: Visualizzazione del contenuto del documento
[_] Mostra segnalibri
Per immettere la data iniziale c'è un pulsante nascosto e per farlo comparire occorre passare alla visualizzazione Mostra tutto. Così:
Quindi doppioclic sul pulsante macro Aggiorna.
Il documento contiene i seguenti segnalibri e codici di campo:
Prestare attenzione al fatto che il testo nei segnalibri si conclude con uno spazio nascosto, necessario al fine della corretta esecuzione della macro AggiornaValori.
Nel codice Visual Basic ho usato una routine di Matthews Patrick che in base ai test che ho fatto fornisce risultati più attendibili della funzione non documentata di Excel DATA.DIFF, come per esempio nel caso delle date 31/1/2006 1/3/2006. Ma l'argomento è rognoso e non si sa mai...
Questo il codice della macro AggiornaValori richiamata dal pulsante macro Aggiorna:
' Modulo: modMacros
'
Option Explicit
Public Sub AggiornaValori()
On Error GoTo ErrH
Const cstrBmkDataInizio = "DataInizio"
Const cstrBmkAnni = "Anni"
Const cstrBmkMesi = "Mesi"
Const cstrBmkGiorni = "Giorni"
Const cstrBmkTotaleGiorni = "TotaleGiorni"
Dim Doc As Word.Document
Dim Bmks As Word.Bookmarks
Dim strInizio As String
Dim dtmInizio As Date
Dim dtmFine As Date
Dim lngAA As Long
Dim lngMM As Long
Dim lngGG As Long
Dim lngTGG As Long
dtmFine = Date
strInizio = InputBox("Immetti la data iniziale.", "Aggiorna valori")
If Len(strInizio) = 0 Then GoTo ExtP
If Not IsDate(strInizio) Then
MsgBox "'" & strInizio & "' non è una data valida." _
, vbOKOnly Or vbExclamation _
, "Ops!..."
GoTo ExtP
End If
dtmInizio = DateValue(strInizio)
If dtmInizio > dtmFine Then
MsgBox Format$(dtmInizio, "d mmmm yyyy") & " > Data oggi." _
, vbOKOnly Or vbExclamation _
, "Ops!..."
GoTo ExtP
End If
' ************************************************************
' http://www.vbaexpress.com/kb/getarticle.php?kb\_id=866
' ************************************************************
Dim Date1 As Date
Dim Date2 As Date
Dim TestYear As Long
Dim TestMonth As Long
Dim TestDay As Long
Dim TargetDate As Date
Dim Last1 As Date
Dim Last2 As Date
Date1 = dtmInizio
Date2 = dtmFine
If Year(Date2) > Year(Date1) Then
If Month(Date2) = Month(Date1) Then
If Day(Date2) >= Day(Date1) Then
TestYear = DateDiff("yyyy", Date1, Date2)
Else
TestYear = DateDiff("yyyy", Date1, Date2) - 1
End If
ElseIf Month(Date2) > Month(Date1) Then
TestYear = DateDiff("yyyy", Date1, Date2)
Else
TestYear = DateDiff("yyyy", Date1, Date2) - 1
End If
Else
TestYear = 0
End If
TestMonth = (DateDiff("m" _
, DateSerial(Year(Date1), Month(Date1), 1) _
, DateSerial(Year(Date2), Month(Date2), 1) _
) + IIf(Day(Date2) >= Day(Date1), 0, -1)) Mod 12
If Day(Date2) >= Day(Date1) Then
TestDay = Day(Date2) - Day(Date1)
Else
Last1 = DateSerial(Year(Date2), Month(Date2), 0)
Last2 = DateSerial(Year(Date2), Month(Date2) + 1, 0)
TargetDate = DateSerial(Year(Date2), Month(Date2) - 1, Day(Date1))
If Last2 = Date2 Then
If TestMonth = 11 Then
TestMonth = 0
TestYear = TestYear + 1
Else
TestMonth = TestMonth + 1
End If
Else
TestDay = DateDiff("d" _
, IIf(TargetDate > Last1, Last1, TargetDate) _
, Date2)
End If
End If
' ************************************************************
lngTGG = dtmFine - dtmInizio
lngAA = TestYear
lngMM = TestMonth
lngGG = TestDay
Set Doc = ThisDocument
Set Bmks = Doc.Bookmarks
With Bmks
With .Item(cstrBmkDataInizio)
Doc.Range(.Start, .End - 1).Text = Format$(dtmInizio, "dddd d mmmm yyyy")
End With
With .Item(cstrBmkTotaleGiorni)
Doc.Range(.Start, .End - 1).Text = Format$(lngTGG, "#,##0")
End With
With .Item(cstrBmkAnni)
Doc.Range(.Start, .End - 1).Text = Format$(lngAA, "#,##0")
End With
With .Item(cstrBmkMesi)
Doc.Range(.Start, .End - 1).Text = Format$(lngMM, "#,##0")
End With
With .Item(cstrBmkGiorni)
Doc.Range(.Start, .End - 1).Text = Format$(lngGG, "#,##0")
End With
End With
ExtP:
Set Bmks = Nothing
Set Doc = Nothing
Exit Sub
ErrH:
With Err
MsgBox .Description, vbCritical Or vbOKOnly, "Errore #" & .Number
End With
Resume ExtP
End Sub