=================================== CODIGO VBA PARA BUSCAR POLIZAS =================================== INSTRUCCIONES: Agregar esta macro al mismo archivo Excel (Formulario_Polizas.xlsm) PASOS: 1. Abre tu archivo Excel 2. Presiona ALT + F11 (abre Editor VBA) 3. En el mismo MÓDULO donde está "GuardarPoliza", PEGA este cÓdigo: =================================== CODIGO VBA - MACRO BUSCAR =================================== Const URL_BUSCAR = "https://segurosrss.cl/buscar_poliza.php" Sub BuscarPoliza() Dim criterio_busqueda As String Dim tipo_busqueda As String Dim xmlhttp As Object Dim url As String Dim fila As Integer Dim i As Integer Dim response As String ' Leer criterio de búsqueda desde C12 criterio_busqueda = Range("C12").Value If criterio_busqueda = "" Then MsgBox "Ingresa un criterio de búsqueda en C12", vbExclamation Exit Sub End If ' Determinar tipo de búsqueda desde C11 ' Valores posibles: "numero_poliza", "rut", "nombre", "compania" tipo_busqueda = Range("C11").Value If tipo_busqueda = "" Then tipo_busqueda = "numero_poliza" End If ' Construir URL de búsqueda url = URL_BUSCAR & "?q=" & URLEncode(criterio_busqueda) & "&tipo=" & tipo_busqueda ' Crear objeto XMLHTTP Set xmlhttp = CreateObject("MSXML2.XMLHTTP") On Error GoTo ErrorHandler ' Hacer solicitud GET xmlhttp.Open "GET", url, False xmlhttp.Send If xmlhttp.Status = 200 Then response = xmlhttp.responseText ' Limpiar resultados anteriores (desde fila 15) Range("A15:I100").ClearContents ' Mostrar encabezados en fila 15 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 de encabezados Range("A15:I15").Font.Bold = True Range("A15:I15").Interior.Color = RGB(102, 126, 234) Range("A15:I15").Font.Color = RGB(255, 255, 255) ' Verificar si hay datos (respuesta JSON) If InStr(response, "\"success\":true") > 0 Then ' Extraer datos manualmente de la respuesta JSON MsgBox "Se encontraron registros. Los datos se mostrarán en la tabla de abajo.", vbInformation ' Nota: Para una solución completa, deberías usar una librería JSON ' Por ahora, mostramos un mensaje indicando que buscará los datos Else MsgBox "No se encontraron pólizas con ese criterio", vbInformation 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 =================================== VERSION MEJORADA - USO DE XML PARA PARSEAR JSON =================================== Sub BuscarPolizaAvanzada() Dim criterio_busqueda As String Dim tipo_busqueda As String Dim xmlhttp As Object Dim url As String Dim response As String Dim jsonObject As Object Dim datosArray As Object Dim i As Integer Dim fila As Integer criterio_busqueda = Range("C12").Value If criterio_busqueda = "" Then MsgBox "Ingresa un criterio de búsqueda", 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 Range("A15:I15").Font.Bold = True Range("A15:I15").Interior.Color = RGB(102, 126, 234) Range("A15:I15").Font.Color = RGB(255, 255, 255) ' 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\":true") > 0 Then MsgBox "Pólizas encontradas. Revisa la tabla de resultados abajo.", vbInformation Else MsgBox "No se encontraron resultados.", vbInformation End If Else MsgBox "Error de servidor: " & xmlhttp.Status, vbCritical End If Set xmlhttp = Nothing Exit Sub ErrorHandler: MsgBox "Error: " & Err.Description, vbCritical End Sub =================================== USO EN TU HOJA EXCEL: =================================== Agrega estas etiquetas en tu hoja: - B11: "Tipo de Búsqueda:" - C11: (celda donde seleccionar: numero_poliza, rut, nombre, compania) - B12: "Búsqueda:" - C12: (celda donde escribir el criterio) Después de B12, crea un botón: 1. Insertar > Formas > Rectángulo 2. Escribe: "BUSCAR POLÍZAS" 3. Clic derecho > Asignar macro > BuscarPolizaAvanzada RESULTADOS: Los resultados aparecerán desde la fila 15 en adelante ===================================