Results 1 to 15 of 15
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sunrise, Florida
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Get Active Document Name (Word 2K3)

    Tried doing a search for this to no avail.

    From Access, I create an RTF document that is named using the system date (i.e. "16-Feb-09.rtf"). The document itself might have several paragraphs (in ascending order) that begin with dates that ARE NOT equal to the date that was used to name the RTF file. I would like to have a macro that, when I execute it in Word, will delete all the paragraphs beginning with the first date it finds, up to the paragraph that begins with the date used to name the RTF file. I've attached a sample RTF file. Any help is appreciated. Thanks!
    Attached Files Attached Files

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Get Active Document Name (Word 2K3)

    Try this macro:

    Sub RemoveOldText()
    Dim par As Paragraph
    Dim lngStart As Long
    Dim lngEnd As Long
    Dim strText As String
    Dim dtmStart As Date
    Dim dtmEnd As Date
    Dim strName As String
    Dim p As Long

    lngStart = -1
    strName = ActiveDocument.Name
    p = InStr(strName, ".")
    dtmEnd = CDate(Left(strName, p - 1))
    For Each par In ActiveDocument.Paragraphs
    strText = par.Range.Text
    p = InStr(strText, "-")
    If p > 0 Then
    If lngStart < 0 Then
    lngStart = par.Range.Start
    End If
    dtmStart = CDate(Left(strText, p - 1))
    If dtmStart = dtmEnd Then
    lngEnd = par.Range.Start - 1
    Exit For
    End If
    End If
    Next par
    ActiveDocument.Range(lngStart, lngEnd).Delete
    End Sub

    I haven't stress-tested it, so try it out on copies of your RTF documents first.

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sunrise, Florida
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Get Active Document Name (Word 2K3)

    Like always, Hans! Thanks!!!!

    Tried it but it it gives me a Syntax Error and points to this line of code: "If lngStart < 0 Then"

    Will wait for you. Thanks, again.

  4. #4
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sunrise, Florida
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Get Active Document Name (Word 2K3)

    Hans ... disregard the previous reply. The line that is pointing to is : "dtmStart = CDate(Left(strText, p - 1))"... and the error is Data Type Mysmatch. Thanks!

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Get Active Document Name (Word 2K3)

    That means that your document looks different from the sample document that you posted...

  6. #6
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sunrise, Florida
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Get Active Document Name (Word 2K3)

    You are absolutely right. The doc I attached previously only contained an excerpt of the actual doc. I have attached the "actual" one hoping that you have time to review it. Thanks!!!
    Attached Files Attached Files

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Get Active Document Name (Word 2K3)

    This new version assumes that the paragraphs with dates will begin with a tab (or another character) before the date.

    Sub RemoveOldText()
    Dim par As Paragraph
    Dim lngStart As Long
    Dim lngEnd As Long
    Dim strText As String
    Dim dtmStart As Date
    Dim dtmEnd As Date
    Dim strName As String
    Dim p As Long

    lngStart = -1
    strName = ActiveDocument.Name
    p = InStr(strName, ".")
    dtmEnd = CDate(Left(strName, p - 1))
    For Each par In ActiveDocument.Paragraphs
    strText = par.Range.Text
    p = InStr(strText, "-")
    If p > 0 Then
    strText = Mid(strText, 2, p - 2)
    If IsDate(strText) Then
    If lngStart < 0 Then
    lngStart = par.Range.Start
    End If
    dtmStart = CDate(strText)
    If dtmStart = dtmEnd Then
    lngEnd = par.Range.Start - 1
    Exit For
    End If
    End If
    End If
    Next par
    ActiveDocument.Range(lngStart, lngEnd).Delete
    End Sub

  8. #8
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sunrise, Florida
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Get Active Document Name (Word 2K3)

    U gonna kill me, Hans. The macro works great. I didn't tell you, and I think you didn't notice, that the doc has more than one page. Could you review the code so I can apply it on a "by-page" basis? Thanks!!!

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Get Active Document Name (Word 2K3)

    That makes it much more complicated, I will have to look at it later.

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Get Active Document Name (Word 2K3)

    Try this version:

    Sub RemoveOldText()
    Dim par As Paragraph
    Dim lngStart As Long
    Dim lngEnd As Long
    Dim strText As String
    Dim dtmStart As Date
    Dim dtmEnd As Date
    Dim strName As String
    Dim p As Long

    lngStart = -1
    strName = ActiveDocument.Name
    p = InStr(strName, ".")
    dtmEnd = CDate(Left(strName, p - 1))
    For Each par In ActiveDocument.Paragraphs
    strText = par.Range.Text
    p = InStr(strText, "-")
    If p > 0 Then
    strText = Mid(strText, 2, p - 2)
    If IsDate(strText) Then
    If lngStart < 0 Then
    lngStart = par.Range.Start
    End If
    dtmStart = CDate(strText)
    If dtmStart = dtmEnd Then
    lngEnd = par.Range.Start - 1
    ActiveDocument.Range(lngStart, lngEnd).Delete
    lngStart = -1
    End If
    End If
    End If
    Next par
    End Sub

  11. #11
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sunrise, Florida
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hans ... your solution hit the nail on the head! Again, thanks for your guidance. Not only did it work like a charm; it helped me learn some more about VBA. Take care!

  12. #12
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sunrise, Florida
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Back again, Hans.

    I have attached two files; one has the macro I messed-up with and the other a sample file where the macro would be executed. It worked fine yesterday but today it's giving me the following:
    Run-time error '4608'
    Value out of range
    When I click on "debug", it highlights this portion of the code:
    ActiveDocument.Range(lngStart, lngEnd).Delete
    I've gone through it line by line but, like a blind leading a blind, I'm lost as to what the problem might be. Hope you have the time to review and advise. BTW, I changed the name of the Sub. Thanks!!!
    Attached Files Attached Files

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Once again, each line in your document is a separate paragraph. You remove manual line breaks but leave the superfluous paragraph ends. So the code fails.

  14. #14
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sunrise, Florida
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are right and I'm sorry for taking so much of your time. I really don't know where I screwed up and, if I could find it, probably wouldn't be able to fix it. If you have a moment some time in the future, please review the documents I have attached: "_initial_26-Feb-09.rtf" and "_final_26-Feb-09.rtf". The first is the document that I get from Access; the second is the document I would like to have once I run a macro. The final version doesn't have "page breaks"; there is no white space between lines or paragraphs; the tabs have been reset to "0.75"; the paragraphs are formated the way they should be; and all paragraphs that began with a date not equal to the date in the file name were deleted. The problem is that, try as I may, can find the way to accomplish everything with a macro. Thanks again, and take care!

    BTW, "_initial_" or "_final_" would not appear in the actual file name. Also, the date in the file name uses hyphens, not underscores. The latter are modified when the files are uploaded.
    Attached Files Attached Files

  15. #15
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Since the document is apparently an Access report exported to RTF, why not do the processing in Access? It shouldn't be hard to create a query that only exports data for the current date, and you might write code in Access that splits the text into chunks of less than 2,000 characters. And since you mentioned that the split was needed because you wanted to upload the data to a database, why not do that directly from Access instead of using .RTF as intermediary step?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •