Option Compare Database
Option Explicit
Const PATH_NAME As String = "G:\RLCfinance\Agency\Stan\National\Miscellaneous\Testing_New_Database\Strip_NOIs\"
Const PRINT_PATH_NAME As String = "G:\RLCfinance\Agency\Stan\ReportsToMail\"
Declare Function GetDesktopWindow Lib "User32" () As Long
Declare Function OpenClipboard Lib "User32" (ByVal Hwnd As Long) As Long
Declare Function EmptyClipboard Lib "User32" () As Long
Declare Function CloseClipboard Lib "User32" () As Long
Public Sub Split_NOIs(strFileName As String)
Dim objWordApp As Word.Application
Dim objWordDoc As Word.Document
Dim strFN As String
Dim strNFN As String
Dim strOFN As String
Dim strAccount As String
Dim strDate As String
Dim strPDF_path As String
Dim x As Integer
Dim DeskHwnd As Long
Dim junk As Long
Dim intDateLine As Long
Dim strAcctFind As String
Dim lngCount As Long
DeskHwnd = GetDesktopWindow()
Set objWordApp = New Word.Application
strOFN = strFileName
strFN = PATH_NAME & strOFN
With objWordApp
.Visible = False
Select Case Left(strOFN, 3)
Case "100"
intDateLine = 11
If Left(strOFN, 4) = "1000" Then
strAcctFind = "RE:"
Else
strAcctFind = "Account No.:"
End If
Case "200"
intDateLine = 9
strAcctFind = "Account No.:"
Case "201"
intDateLine = 5
strAcctFind = "Account No.:"
Case "300"
intDateLine = 11
strAcctFind = "Account No.:"
Case "301", "601", "701", "702", "802", "850"
intDateLine = 5
strAcctFind = "Account No.:"
Case "400", "401", "404", "801", "901"
intDateLine = 6
strAcctFind = "Account No."
Case "500"
intDateLine = 12
strAcctFind = "Account No.:"
Case "501"
intDateLine = 5
strAcctFind = "Account No.:"
Case "800"
If InStr(1, strOFN, "MDN") > 0 Then
intDateLine = 16
strAcctFind = "Account No.:"
Else
intDateLine = 11
strAcctFind = "Account No.:"
End If
Case Else
Exit Sub
End Select
Set objWordDoc = .Documents.Open(strFN, , True)
.Selection.EndKey unit:=wdStory
.Selection.HomeKey unit:=wdStory
lngCount = Nz([Forms]![frmSplitNOIs]![txtAccounts].Value, 0)
Do Until .Selection.Information(wdNumberOfPagesInDocument) = 1
.Selection.Extend
.Selection.MoveEnd unit:=wdSection, Count:=1
.Selection.Cut
.Documents.Add Visible:=True
.Selection.PasteAndFormat Type:=wdFormatOriginalFormatting
.Selection.MoveLeft unit:=wdCharacter, Count:=1, Extend:=wdExtend
.Selection.Delete unit:=wdCharacter, Count:=1
.Selection.HomeKey unit:=wdStory
.Selection.Find.Execute findtext:=strAcctFind, Forward:=True, Wrap:=wdFindStop
.Selection.EndKey unit:=wdLine
.Selection.MoveLeft unit:=wdWord, Count:=1, Extend:=wdExtend
strAccount = .Selection.Text
.Selection.HomeKey unit:=wdStory
.Selection.MoveDown unit:=wdParagraph, Count:=intDateLine
.Selection.EndKey unit:=wdLine, Extend:=wdExtend
.Selection.MoveLeft unit:=wdCharacter, Count:=1, Extend:=wdExtend
strDate = .Selection.Text
strPDF_path = PATH_NAME & Format(CDate(strDate), "yyyymm") & "\"
Debug.Print strAccount & " - " & strDate
If Dir(strPDF_path, vbDirectory) = "" Then
MkDir strPDF_path
End If
.PrintOut Background:=False
Do
strFN = Dir(PRINT_PATH_NAME & "*" & .ActiveDocument.Name & ".pdf")
Loop Until strFN <> ""
On Error Resume Next
strNFN = strPDF_path & strAccount & "-NOI.pdf"
If Dir(strNFN) <> "" Then
Do
x = x + 1
strNFN = strPDF_path & strAccount & "-NOI_" & Right("000" & x, 2) & ".pdf"
Loop Until Dir(strNFN) = ""
End If
lngCount = lngCount + 1
[Forms]![frmSplitNOIs]![txtDate].Value = strDate
[Forms]![frmSplitNOIs]![txtAccount].Value = strAccount & IIf(x > 0, " - " & x, "")
[Forms]![frmSplitNOIs]![txtAccounts].Value = lngCount
[Forms]![frmSplitNOIs].Repaint
Do
Name PRINT_PATH_NAME & strFN As strNFN
Loop Until (Dir(strPDF_path & strAccount & "-NOI.pdf") <> "") And (Dir(PRINT_PATH_NAME & strFN) = "")
On Error GoTo 0
.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
x = 0
Loop
End With
objWordDoc.Close SaveChanges:=wdDoNotSaveChanges
junk = OpenClipboard(DeskHwnd)
junk = EmptyClipboard()
junk = CloseClipboard()
objWordApp.Quit
Set objWordDoc = Nothing
Set objWordApp = Nothing
End Sub