Advertisement
Guest User

Untitled

a guest
Mar 11th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Dia, Mes, Ano As Integer
  2. Public Data, Out_Path As String
  3. Public myf As Boolean
  4. Public Sub main()
  5.  
  6.     'T_PDF_File = "C:\Users\Judah\Desktop\19-6-1990\DO-19-06-1990FL-1.pdf" 'Path & MyFile
  7.    Each_Page = False 'Se "True", cria um txt para cada página do PDF
  8.    
  9.     Dim AC_PD As Acrobat.AcroPDDoc
  10.     Dim AC_Hi As Acrobat.AcroHiliteList
  11.     Dim AC_PG As Acrobat.AcroPDPage
  12.     Dim AC_PGTxt As Acrobat.AcroPDTextSelect
  13.    
  14.     Dim OS_FSO As Object
  15.     Dim OS_TxtFile As Object
  16.        
  17.     Dim Data, Path As String
  18.     Dim MyFile, MyDir As String
  19.    
  20.     Dim sqline As Variant
  21.    
  22.     Out_Path = "C:\Users\Judah\Desktop\OUT\" 'Escolha a pasta para a saída dos txts
  23.    
  24.     Path = "C:\Lala\"
  25.        
  26.     MyDir = Dir(Path, vbDirectory)
  27.        
  28.     Do Until MyDir <> "." And MyDir <> ".."
  29.         MyDir = Dir()
  30.     Loop
  31.    
  32.     Dia = Left(MyDir, 2)
  33.     If Len(Replace(Dia, "-", "")) = 1 Then
  34.         Dia = Left(MyDir, 1)
  35.         Mes = Mid(MyDir, 3, 2)
  36.         If Len(Replace(Mes, "-", "")) = 1 Then
  37.             Mes = Mid(MyDir, 3, 1)
  38.         End If
  39.     Else:
  40.         Dia = Left(MyDir, 2)
  41.         Mes = Mid(MyDir, 4, 2)
  42.         If Len(Replace(Mes, "-", "")) = 1 Then
  43.             Mes = Mid(MyDir, 4, 1)
  44.         End If
  45.     End If
  46.    
  47.     m = 3
  48.     o = 1
  49.            
  50.     Ano = Right(MyDir, 4)
  51.    
  52.     Data = Dia & "-" & Mes & "-" & Ano
  53.        
  54.     'Call cria_pasta 'Função tipo main para criação de subpastas
  55.    
  56.     Do Until MyDir = ""
  57.                
  58.         Dia = Left(MyDir, 2)
  59.         If Len(Replace(Dia, "-", "")) = 1 Then
  60.             Dia = Left(MyDir, 1)
  61.             Mes = Mid(MyDir, 3, 2)
  62.             If Len(Replace(Mes, "-", "")) = 1 Then
  63.                 Mes = Mid(MyDir, 3, 1)
  64.             End If
  65.         Else:
  66.             Dia = Left(MyDir, 2)
  67.             Mes = Mid(MyDir, 4, 2)
  68.             If Len(Replace(Mes, "-", "")) = 1 Then
  69.                 Mes = Mid(MyDir, 4, 1)
  70.             End If
  71.         End If
  72.                
  73.         Ano = Right(MyDir, 4)
  74.        
  75.         Data = Dia & "-" & Mes & "-" & Ano
  76.                
  77.         MyFile = Dir(Path & MyDir & "\")
  78.    
  79.         If MyFile <> "" Then
  80.             'myf = False
  81.            Call testa_file(MyFile)
  82.         End If
  83.        
  84. '        If MyFile <> "" Then
  85. '            Call cria_pasta
  86. '        End If
  87.        If myf = False Then
  88.             Do Until MyFile = ""
  89.            
  90.                 Set OS_FSO = CreateObject("Scripting.filesystemobject")
  91.                
  92.                 Dim Ct_Page As Long
  93.                 Dim i As Long, j As Long, k As Long
  94.                 Dim T_Str As String
  95.                
  96.                 Dim Hld_Txt As Variant
  97.                
  98.                 Set AC_PD = New Acrobat.AcroPDDoc
  99.                 Set AC_Hi = New Acrobat.AcroHiliteList
  100.                 AC_Hi.Add 0, 32767
  101.                
  102.                 With AC_PD
  103.                    
  104.                     .Open Path & MyDir & "\" & MyFile
  105.                     Ct_Page = .GetNumPages
  106.                    
  107.     '                If Ct_Page = -1 Then 'conta a quantidade de páginas
  108.    '                    MsgBox "Pages Cannot determine in PDF file '" & T_PDF_File & "'"
  109.    '                    .Close
  110.    '                    GoTo h_end
  111.    '                End If
  112.                    
  113.     '                If Each_Page = False Then
  114.    '                    Set OS_TxtFile = OS_FSO.createtextfile(Out_Path & Replace(MyFile, ".pdf", ".txt")) 'Ano & "\" & Mes & "\" & Data & "\" & Replace(MyFile, ".pdf", "") & ".txt") 'cria o arquivo txt
  115.    '                End If
  116.                    
  117.                     For i = 1 To Ct_Page
  118.                        
  119.                         T_Str = ""
  120.                         Set AC_PG = .AcquirePage(i - 1)
  121.                        
  122.                         Set AC_PGTxt = AC_PG.CreateWordHilite(AC_Hi)
  123.                        
  124.                         If Not AC_PGTxt Is Nothing Then
  125.                        
  126.                             With AC_PGTxt
  127.                            
  128.                                 For j = 0 To .GetNumText - 1
  129.                                     'T_Str = T_Str & .GetText(j)
  130.                                    sqline = sqline & .GetText(j)
  131.                                     t1 = Replace(sqline, Chr(10), "")
  132.                                 Next j
  133.                                
  134.                             End With
  135.                            
  136.                         End If
  137.                        
  138.                         Call exportasql(t1, MyFile)
  139.                         t1 = ""
  140.                         sqline = ""
  141.     '                    If T_Str = "" Then T_Str = "No text found in page " & i
  142.    '
  143.    '                    If Each_Page = True Then
  144.    '                        Set OS_TxtFile = OS_FSO.createtextfile(Out_Path & "\Page-" & i & ".txt")
  145.    '                        OS_TxtFile.write t1
  146.    '                        OS_TxtFile.Close
  147.    '                        Set OS_TxtFile = Nothing
  148.    '                    Else
  149.    '                        T_Str = vbCrLf & vbCrLf & "Text In Page - " & i & vbCrLf & vbCrLf & T_Str
  150.    '                        OS_TxtFile.write t1
  151.    '                    End If
  152.                    Next i
  153.                    
  154.                     'If Each_Page = False Then OS_TxtFile.Close
  155.                    .Close
  156.                    
  157.                 End With
  158.                
  159.                 MyFile = Dir(Path & MyDir & "\")
  160.                            
  161.                 For n = 1 To o
  162.                     MyFile = Dir()
  163.                 Next
  164.                
  165.                 o = o + 1
  166.            
  167.             Loop
  168.         End If
  169.                    
  170.         MyDir = Dir(Path, vbDirectory)
  171.        
  172.         For l = 1 To m
  173.             MyDir = Dir()
  174.         Next
  175.        
  176.         m = m + 1
  177.         o = 1
  178.        
  179.         'Dia = Dia + 1
  180.        'Call cria_pasta
  181.    
  182.     Loop
  183.    
  184. h_end:
  185.    
  186.     Set OS_TxtFile = Nothing
  187.     Set OS_FSO = Nothing
  188.     Set AC_PGTxt = Nothing
  189.     Set AC_PG = Nothing
  190.     Set AC_Hi = Nothing
  191.     Set AC_PD = Nothing
  192.  
  193. End Sub
  194.  
  195. Function cria_pasta()
  196.            
  197.     If Dia <> 32 Then
  198.         If Ano <> tAno Then 'Checa se já existe a subpasta do ano a ser rodado
  199.            Call cria_ano(Ano)
  200.         End If
  201.         If Mes <> tMes Then 'Checa se já existe a subpasta do mes a ser rodado
  202.            Call cria_mes(Mes)
  203.         End If
  204.         If Dia <> tDia Then 'Checa se já existe a subpasta do dia a ser rodado
  205.            Data = Dia & "-" & Mes & "-" & Ano
  206.             Call cria_dia(Data)
  207.         End If
  208.        
  209.         'Iguala variáveis temporárias às atuais para serem utilizadas quando a função for chamada novamente
  210.        tDia = Dia
  211.         tMes = Mes
  212.         tAno = Ano
  213.    
  214.     End If
  215.  
  216. End Function
  217.  
  218. Function cria_ano(ByVal vAno As String)
  219.    
  220.     nPath = Out_Path & vAno & "\"
  221.    
  222.     If Dir(nPath, vbDirectory) = "" Then
  223.         MkDir nPath
  224.     End If
  225.    
  226. End Function
  227. Function cria_mes(ByVal vMes As String)
  228.    
  229.     nPath = Out_Path & Ano & "\" & vMes & "\"
  230.    
  231.     If Dir(nPath, vbDirectory) = "" Then
  232.         MkDir nPath
  233.     End If
  234.    
  235. End Function
  236. Function cria_dia(ByVal vDia As String)
  237.    
  238.     nPath = Out_Path & Ano & "\" & Mes & "\" & vDia & "\"
  239.    
  240.     If Dir(nPath, vbDirectory) = "" Then
  241.         MkDir nPath
  242.     End If
  243.    
  244. End Function
  245. Function exportasql(ByVal line As Variant, ByVal file As String)
  246.        
  247.     Dim conexao As ADODB.Connection
  248.     Set conexao = New ADODB.Connection
  249.     Dim cquery, cquery2 As Variant
  250.    
  251.     strconexao = "Provider=SQLOLEDB.1;" & _
  252.                 "Persist Security Info=False;" & _
  253.                 "Initial Catalog=OD;" & _
  254.                 "Data Source=DESKTOP-GG4VVAA\;" & _
  255.                 "User ID=sa;Password=asdf1234;"
  256.  
  257.     conexao.ConnectionString = strconexao
  258.     conexao.Open
  259.    
  260.     cquery = "INSERT INTO [dbo].[TEST1] "
  261.     cquery = cquery & "([ANO]"
  262.     cquery = cquery & ",[MES]"
  263.     cquery = cquery & ",[DIA]"
  264.     cquery = cquery & ",[NOME_ARQ]"
  265.     cquery = cquery & ",[CONTEUDO])"
  266.     cquery2 = "VALUES"
  267.     cquery2 = cquery2 & "(" & Ano
  268.     cquery2 = cquery2 & "," & Mes
  269.     cquery2 = cquery2 & "," & Dia
  270.     cquery2 = cquery2 & ",'" & file & "'"
  271.     cquery2 = cquery2 & ",'" & Replace(line, "'", "") & "')" 'Replace(line, "'", "")
  272.                                                                                
  273.     conexao.Execute cquery & cquery2 '& line & "')"
  274.    conexao.Close
  275.  
  276. End Function
  277. Function testa_file(ByVal file As String)
  278.    
  279.     Dim conexao As ADODB.Connection
  280.     Set conexao = New ADODB.Connection
  281.     Dim cquery, cquery2 As Variant
  282.     Dim RST As New ADODB.Recordset
  283.    
  284.     strconexao = "Provider=SQLOLEDB.1;" & _
  285.                 "Persist Security Info=False;" & _
  286.                 "Initial Catalog=OD;" & _
  287.                 "Data Source=DESKTOP-GG4VVAA\;" & _
  288.                 "User ID=sa;Password=asdf1234;"
  289.  
  290.     conexao.ConnectionString = strconexao
  291.     conexao.Open
  292.     'file = "LALALALA.pdf"
  293.    Set RST = conexao.Execute("SELECT [NOME_ARQ] FROM [OD].[dbo].[TEST1] WHERE [NOME_ARQ] = '" & file & "'")
  294.    
  295.     'file1 = RST("NOME_ARQ")
  296.        
  297.    
  298.     If RST.BOF = True Then
  299.         myf = False
  300.     Else:
  301.         myf = True
  302.     End If
  303.    
  304.     RST.Close
  305.        
  306. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement