none
VBA macro to extract the date from word document RRS feed

  • Question

  • Hello:

    Is there any way to extract "Date" section from the word through VBA? I know we can do it excel, but don't know if we can do it the word document. Any ideas?

    Thanks,

    Shwetha.

    Friday, April 7, 2017 2:12 PM

Answers

  • As far I understand, you have Word document with text. You have date inside this text. You want to find it and do something with it. Yes that is a possible.

    So in this sample I wrote a 2 corect dates and one wrong one.

    And you can use this code to find a dates in this document:

    Sub Find_Date()
    Const ja$ = "www.VBATools.pl"
    Dim tbl() As String: tbl = Split(ActiveDocument.Range.Text, " ")
    Dim x&, y&, dates As New Collection, dc$
    For x = 1 To UBound(tbl)
        If IsDate(Trim(tbl(x))) = True Then
            y = y + 1
            On Error Resume Next
                dc = dc & y & ". " & tbl(x) & vbCr
                dates.Add tbl(x), CStr(tbl(x))
            On Error GoTo 0
        End If
    Next
    If dates.Count > 0 Then
        Dim czy: czy = MsgBox("You have " & dates.Count & _
            " diferent dates in your document." & vbCr & vbCr & _
            "Do you want to see the list of them?", _
            vbQuestion + vbDefaultButton1 + vbYesNo, ja)
            If czy = vbYes Then MsgBox dc, vbInformation, ja
    Else
        MsgBox "You do not have any dates in this document.", vbInformation, ja
    End If
    End Sub

    Regards


    Oskar Shon, Office System MVP - www.VBATools.pl
    if Helpful; Answer when a problem solved

    • Marked as answer by shwethagc Monday, April 10, 2017 8:11 PM
    Friday, April 7, 2017 6:45 PM
    Answerer

All replies

  • You want Extract date from document text - where?

    Using code from Excel developer, from another word document, from windows VB.Net program.


    Oskar Shon, Office System MVP - www.VBATools.pl
    if Helpful; Answer when a problem solved

    Friday, April 7, 2017 3:00 PM
    Answerer
  • I want to write a VBA macro in the word doc, which extract the "date" section from that doc and compares with the current date. Is that possible?
    Friday, April 7, 2017 4:13 PM
  • As far I understand, you have Word document with text. You have date inside this text. You want to find it and do something with it. Yes that is a possible.

    So in this sample I wrote a 2 corect dates and one wrong one.

    And you can use this code to find a dates in this document:

    Sub Find_Date()
    Const ja$ = "www.VBATools.pl"
    Dim tbl() As String: tbl = Split(ActiveDocument.Range.Text, " ")
    Dim x&, y&, dates As New Collection, dc$
    For x = 1 To UBound(tbl)
        If IsDate(Trim(tbl(x))) = True Then
            y = y + 1
            On Error Resume Next
                dc = dc & y & ". " & tbl(x) & vbCr
                dates.Add tbl(x), CStr(tbl(x))
            On Error GoTo 0
        End If
    Next
    If dates.Count > 0 Then
        Dim czy: czy = MsgBox("You have " & dates.Count & _
            " diferent dates in your document." & vbCr & vbCr & _
            "Do you want to see the list of them?", _
            vbQuestion + vbDefaultButton1 + vbYesNo, ja)
            If czy = vbYes Then MsgBox dc, vbInformation, ja
    Else
        MsgBox "You do not have any dates in this document.", vbInformation, ja
    End If
    End Sub

    Regards


    Oskar Shon, Office System MVP - www.VBATools.pl
    if Helpful; Answer when a problem solved

    • Marked as answer by shwethagc Monday, April 10, 2017 8:11 PM
    Friday, April 7, 2017 6:45 PM
    Answerer
  • Thank you very much for the suggestions. I have another question. I would like run the VBA macro in excel or word continuously in the background even when the document is closed. For example, I want the macro to extract the expiration date (either from word or excel), once it has extracted, monitor it once in a day and compare it to the current date, if expired print Expired in the corresponding document. Is it something that can be done either in Excel or word?

    Thank you very much in advance for any ideas.

    Shwetha.

    Monday, April 10, 2017 8:22 PM
  • Sorry. English is not my 1-st lang so would you be more specific what you mean writing "Expired print from Excel and Word document"?

    Oskar Shon, Office System MVP - www.VBATools.pl
    if Helpful; Answer when a problem solved

    Tuesday, April 11, 2017 9:22 PM
    Answerer
  • Sorry, that's my typo error. I meant to say expired date from Excel or word. Suppose if my excel sheet has a date. I want to write a macro which runs continuously in the background even when the excel sheet is closed. I want that macro to extract the date from the sheet once a day and compare with the current date, if the date is expired, add some comment in the excel. Is it possible with VBA macro?

    Thanks,

    Shwetha

    Wednesday, April 12, 2017 6:01 PM