=================================== CODIGO VBA PARA EXCEL - MACRO GUARDAR POLIZA =================================== INSTRUCCIONES: 1. Descarga tu archivo Excel 2. Guarda como: Formulario_Polizas.xlsm (IMPORTANTE: extensión .xlsm) 3. Abre Excel y presiona ALT + F11 4. En la ventana de la izquierda, busca "ThisWorkbook" 5. Haz clic derecho en "ThisWorkbook" > Insertar > Módulo 6. Se abrirá un editor en blanco 7. COPIA Y PEGA TODO EL CÓDIGO DE ABAJO: =================================== CODIGO VBA - PEGAR EN EL MODULO =================================== Option Explicit Const URL_SERVIDOR = "https://segurosrss.cl/guardar_poliza.php" 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 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 envío 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 On Error GoTo ErrorHandler xmlhttp.Open "POST", URL_SERVIDOR, False xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" xmlhttp.Send postData If xmlhttp.Status = 200 Then MsgBox "Poliza guardada correctamente!", vbInformation Range("C2:C10").ClearContents Range("C2").Select 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 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 =================================== PASOS PARA CREAR EL BOTON =================================== 1. En Excel, ve a INSERTAR > FORMAS 2. Dibuja un rectangulo donde quieras el boton 3. Escribe: "GUARDAR POLIZA" 4. Haz clic derecho en el boton > ASIGNAR MACRO 5. Selecciona "GuardarPoliza" > OK 6. Listo! OPCION: Si quieres que se cree automaticamente, agrega esta macro en el mismo modulo: Sub Auto_Open() Dim btn As Object Dim ws As Worksheet Set ws = ThisWorkbook.Sheets(1) Set btn = ws.Shapes.AddShape(1, 180, 270, 200, 40) With btn .Name = "BtnGuardar" .TextFrame.Characters.Text = "GUARDAR POLIZA" .TextFrame.Characters.Font.Bold = True .TextFrame.Characters.Font.Size = 14 .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 End Sub =================================== ÜLTIMOS PASOS: 1. Guarda el archivo (Ctrl+S) 2. Cierra Excel y vuelve a abrirlo 3. Excel te preguntara si "Habilitar contenido" > Di SI 4. Completa los campos en las celdas C2:C10 5. Haz clic en el boton GUARDAR POLIZA 6. Listo! Veras mensaje de confirmacion ===================================