' ======================================== ' CODIGO VBA COMPLETO PARA EL MODULO ' ======================================== ' TODO ESTO VA EN UN SOLO MODULO DE EXCEL ' ======================================== Option Explicit ' URLs de los servidores Const URL_SERVIDOR = "https://segurosrss.cl/guardar_poliza.php" Const URL_BUSCAR = "https://segurosrss.cl/buscar_poliza.php" ' ======================================== ' MACRO 1: GUARDAR POLIZA ' ======================================== Sub GuardarPoliza() Dim nombre As String Dim rut As String Dim telefono As String Dim correo As String Dim numero_poliza As String Dim compania As String Dim monto As Double Dim deducible As Double Dim patente As String ' Leer datos desde las celdas C2 a C10 nombre = Range("C2").Value rut = Range("C3").Value telefono = Range("C4").Value correo = Range("C5").Value numero_poliza = Range("C6").Value compania = Range("C7").Value monto = Range("C8").Value deducible = Range("C9").Value patente = Range("C10").Value ' Validar campos obligatorios If nombre = "" Or rut = "" Or numero_poliza = "" Or compania = "" Then MsgBox "Campos obligatorios: Nombre, RUT, N° Póliza y Compañía", vbExclamation Exit Sub End If ' Preparar datos para enviar Dim xmlhttp As Object Set xmlhttp = CreateObject("MSXML2.XMLHTTP") Dim postData As String postData = "nombre=" & URLEncode(nombre) & _ "&rut=" & URLEncode(rut) & _ "&telefono=" & URLEncode(telefono) & _ "&correo=" & URLEncode(correo) & _ "&numero_poliza=" & URLEncode(numero_poliza) & _ "&compania=" & URLEncode(compania) & _ "&monto=" & monto & _ "&deducible=" & deducible & _ "&patente=" & URLEncode(patente) ' Enviar datos al servidor On Error GoTo ErrorHandler xmlhttp.Open "POST", URL_SERVIDOR, False xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" xmlhttp.Send postData ' Procesar respuesta If xmlhttp.Status = 200 Then Dim response As String response = xmlhttp.responseText If InStr(response, "success") > 0 Then MsgBox "Poliza guardada correctamente!", vbInformation ' Limpiar formulario Range("C2:C10").ClearContents Range("C2").Select Else MsgBox "Error al guardar: " & response, vbCritical End If Else MsgBox "Error: " & xmlhttp.Status & " - " & xmlhttp.statusText, vbCritical End If Set xmlhttp = Nothing Exit Sub ErrorHandler: MsgBox "Error de conexión: " & Err.Description, vbCritical Set xmlhttp = Nothing End Sub ' ======================================== ' MACRO 2: BUSCAR POLIZAS ' ======================================== Sub BuscarPolizaAvanzada() Dim criterio_busqueda As String Dim tipo_busqueda As String Dim xmlhttp As Object Dim url As String Dim response As String criterio_busqueda = Range("C12").Value If criterio_busqueda = "" Then MsgBox "Ingresa un criterio de búsqueda en C12", vbExclamation Exit Sub End If tipo_busqueda = IIf(Range("C11").Value <> "", Range("C11").Value, "numero_poliza") ' Limpiar tabla anterior Range("A15:I100").ClearContents ' Añadir encabezados Range("A15").Value = "ID" Range("B15").Value = "Nombre" Range("C15").Value = "RUT" Range("D15").Value = "Teléfono" Range("E15").Value = "N° Póliza" Range("F15").Value = "Compañía" Range("G15").Value = "Monto" Range("H15").Value = "Deducible" Range("I15").Value = "Patente" ' Formato encabezados With Range("A15:I15") .Font.Bold = True .Interior.Color = RGB(102, 126, 234) .Font.Color = RGB(255, 255, 255) End With ' Realizar búsqueda url = URL_BUSCAR & "?q=" & URLEncode(criterio_busqueda) & "&tipo=" & tipo_busqueda Set xmlhttp = CreateObject("MSXML2.XMLHTTP") On Error GoTo ErrorHandler xmlhttp.Open "GET", url, False xmlhttp.Send If xmlhttp.Status = 200 Then response = xmlhttp.responseText If InStr(response, "success") > 0 Then MsgBox "Búsqueda completada. Revisa los resultados abajo.", vbInformation Else MsgBox "No se encontraron pólizas.", vbInformation End If Else MsgBox "Error de servidor: " & xmlhttp.Status, vbCritical End If Set xmlhttp = Nothing Exit Sub ErrorHandler: MsgBox "Error: " & Err.Description, vbCritical Set xmlhttp = Nothing End Sub ' ======================================== ' FUNCION: CODIFICAR URL ' ======================================== Function URLEncode(s As String) As String Dim i As Integer Dim c As String Dim result As String result = "" For i = 1 To Len(s) c = Mid(s, i, 1) Select Case c Case " " result = result & "+" Case "a" To "z", "A" To "Z", "0" To "9", "-", "_", "." result = result & c Case Else result = result & "%" & Format(Asc(c), "00X") End Select Next i URLEncode = result End Function ' ======================================== ' FUNCION: CREAR BOTON AUTOMATICO ' ======================================== Sub Auto_Open() On Error Resume Next Dim btn1 As Object Dim btn2 As Object Dim ws As Worksheet Set ws = ThisWorkbook.Sheets(1) ' Boton 1: GUARDAR Set btn1 = ws.Shapes.AddShape(1, 200, 280, 200, 40) With btn1 .Name = "BtnGuardar" .TextFrame.Characters.Text = "GUARDAR POLIZA" .TextFrame.Characters.Font.Bold = True .TextFrame.Characters.Font.Size = 12 .TextFrame.Characters.Font.Color = RGB(255, 255, 255) .Fill.ForeColor.RGB = RGB(102, 126, 234) .Line.Color.RGB = RGB(102, 126, 234) .OnAction = "GuardarPoliza" End With ' Boton 2: BUSCAR Set btn2 = ws.Shapes.AddShape(1, 420, 280, 200, 40) With btn2 .Name = "BtnBuscar" .TextFrame.Characters.Text = "BUSCAR POLIZAS" .TextFrame.Characters.Font.Bold = True .TextFrame.Characters.Font.Size = 12 .TextFrame.Characters.Font.Color = RGB(255, 255, 255) .Fill.ForeColor.RGB = RGB(76, 175, 80) .Line.Color.RGB = RGB(76, 175, 80) .OnAction = "BuscarPolizaAvanzada" End With On Error GoTo 0 End Sub ' ======================================== ' FIN DEL CODIGO VBA ' ========================================