Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/mdiago/VeriFactu/llms.txt

Use this file to discover all available pages before exploring further.

Using VeriFactu’s COM Interop layer, you can submit VERI*FACTU invoices to the AEAT directly from Excel or Access macros, without leaving the Office environment. The COM objects mirror the full .NET API — including blockchain chaining, certificate signing, and live AEAT communication — so no additional infrastructure or web service knowledge is required.

Prerrequisitos

Before writing any VBA code, ensure the following:
  • Bitness match: Excel/Access 32-bit → register Verifactu.Com.x86.dll. Excel/Access 64-bit → register Verifactu.Com.x64.dll. See Visión General for registration instructions.
  • Digital certificate: A valid AEAT-accepted digital certificate (.pfx file or Windows certificate store entry) must be available. Configure it via VfSettings (see below).
  • Administrator rights used for registration: The DLL must have been registered with RegAsm.exe as Administrator before opening Excel. (regsvr32 does not work for managed .NET assemblies; see Visión General for the correct command.)

Añadir la referencia en VBA

For early binding (type checking and IntelliSense), add the VeriFactu type library as a VBA reference:
1

Abrir el editor VBA

Press Alt+F11 in Excel or Access to open the Visual Basic Editor.
2

Menú Herramientas > Referencias

In the VBA editor menu, click Tools → References. Scroll through the list until you find Verifactu (it appears after registration). Check the checkbox next to it.
3

Aceptar

Click OK. The reference is now added to your VBA project and you will get full IntelliSense for all Verifactu.* types.
If “Verifactu” does not appear in the list, the DLL has not been registered or the bitness does not match your Office installation. See Visión General for registration details.

Configurar el certificado

Before sending any invoices, configure the digital certificate once using VfSettings. Call cfg.Save to persist the settings to disk — subsequent calls will pick them up automatically.
Sub ConfigurarCertificado()
    Dim cfg As New Verifactu.VfSettings
    ' Certificado desde archivo .pfx
    cfg.CertificatePath = "C:\MiCertificado.pfx"
    cfg.CertificatePassword = "mi_password"
    ' Rutas de almacenamiento (opcional — usa rutas por defecto si se omite)
    cfg.InboxPath = "C:\Verifactu\Inbox\"
    cfg.OutboxPath = "C:\Verifactu\Outbox\"
    cfg.BlockchainPath = "C:\Verifactu\Blockchain\"
    cfg.InvoicePath = "C:\Verifactu\Invoices\"
    ' Guardar configuración
    cfg.Save
    MsgBox "Configuración guardada correctamente."
End Sub
Alternatively, you can identify the certificate by its thumbprint from the Windows certificate store instead of a file path:
cfg.CertificateThumbprint = "A1B2C3D4E5F6..."

Ejemplo VBA: Enviar una factura

The following macro creates a standard invoice (F1), adds a 21% VAT tax line, and submits it to the AEAT:
Sub EnviarFactura()
    ' Configuración del certificado
    Dim cfg As New Verifactu.VfSettings
    cfg.CertificatePath = "C:\MiCertificado.pfx"
    cfg.CertificatePassword = "mi_password"
    cfg.Save

    ' Crear factura
    Dim inv As New Verifactu.VfInvoice
    inv.InvoiceID = "FAC-2024-001"
    inv.InvoiceDate = CDate("15/11/2024")
    inv.SellerID = "B72877814"
    inv.SellerName = "MI EMPRESA SL"
    inv.BuyerID = "B44531218"
    inv.BuyerName = "CLIENTE SL"
    inv.Text = "SERVICIOS PROFESIONALES"
    inv.InvoiceType = "F1"

    ' Añadir línea de IVA (base 1000 €, tipo 21%, cuota 210 €)
    Dim taxItem As New Verifactu.VfTaxItem
    taxItem.TaxBase = 1000
    taxItem.TaxRate = 21
    taxItem.TaxAmount = 210
    inv.InsertTaxItem taxItem

    ' Enviar a la AEAT
    Dim result As Verifactu.IVfInvoiceResult
    Set result = inv.Send()

    If result.ResultCode = "0" Then
        MsgBox "Factura enviada correctamente." & vbCrLf & "CSV: " & result.CSV
    Else
        MsgBox "Error " & result.ResultCode & ": " & result.ResultMessage
    End If
End Sub

Tipos de factura disponibles (InvoiceType)

The InvoiceType property corresponds to AEAT list L2. The most common values are:
ValorDescripción
F1Factura (default)
F2Factura simplificada (ticket)
F3Factura emitida en sustitución de facturas simplificadas
R1Rectificativa (error fundado en derecho)
R2Rectificativa (art. 80.3)
R3Rectificativa (art. 80.4)
R4Rectificativa (resto)
R5Rectificativa simplificada

Ejemplo VBA: Factura con recargo de equivalencia

Sub EnviarFacturaConRecargo()
    Dim cfg As New Verifactu.VfSettings
    cfg.CertificatePath = "C:\MiCertificado.pfx"
    cfg.CertificatePassword = "mi_password"
    cfg.Save

    Dim inv As New Verifactu.VfInvoice
    inv.InvoiceID = "FAC-2024-002"
    inv.InvoiceDate = CDate("15/11/2024")
    inv.SellerID = "B72877814"
    inv.SellerName = "MI EMPRESA SL"
    inv.BuyerID = "12345678A"
    inv.BuyerName = "CLIENTE AUTÓNOMO"
    inv.Text = "VENTA DE MERCANCÍAS"
    inv.InvoiceType = "F1"

    ' IVA 21% + recargo de equivalencia 5.2%
    Dim taxItem As New Verifactu.VfTaxItem
    taxItem.TaxBase = 500
    taxItem.TaxRate = 21
    taxItem.TaxAmount = 105
    taxItem.TaxRateSurcharge = 5.2
    taxItem.TaxAmountSurcharge = 26
    inv.InsertTaxItem taxItem

    Dim result As Verifactu.IVfInvoiceResult
    Set result = inv.Send()

    If result.ResultCode = "0" Then
        MsgBox "Enviada. CSV: " & result.CSV
    Else
        MsgBox "Error: " & result.ResultCode & " - " & result.ResultMessage
    End If
End Sub

Ejemplo VBA: Anular una factura

To cancel a previously submitted invoice, create a VfInvoice with the same InvoiceID, InvoiceDate, and SellerID, then call Delete():
Sub AnularFactura()
    Dim cfg As New Verifactu.VfSettings
    cfg.CertificatePath = "C:\MiCertificado.pfx"
    cfg.CertificatePassword = "mi_password"
    cfg.Save

    ' La factura de anulación debe contener los datos identificativos
    ' de la factura original (ID, fecha y NIF emisor)
    Dim inv As New Verifactu.VfInvoice
    inv.InvoiceID = "FAC-2024-001"
    inv.InvoiceDate = CDate("15/11/2024")
    inv.SellerID = "B72877814"
    inv.SellerName = "MI EMPRESA SL"
    inv.InvoiceType = "F1"

    ' Enviar anulación
    Dim result As Verifactu.IVfInvoiceResult
    Set result = inv.Delete()

    If result.ResultCode = "0" Then
        MsgBox "Factura anulada. CSV: " & result.CSV
    Else
        MsgBox "Error al anular: " & result.ResultCode & " - " & result.ResultMessage
    End If
End Sub

Ejemplo VBA: Factura rectificativa

Sub EnviarFacturaRectificativa()
    Dim cfg As New Verifactu.VfSettings
    cfg.CertificatePath = "C:\MiCertificado.pfx"
    cfg.CertificatePassword = "mi_password"
    cfg.Save

    Dim inv As New Verifactu.VfInvoice
    inv.InvoiceID = "REC-2024-001"
    inv.InvoiceDate = CDate("20/11/2024")
    inv.SellerID = "B72877814"
    inv.SellerName = "MI EMPRESA SL"
    inv.BuyerID = "B44531218"
    inv.BuyerName = "CLIENTE SL"
    inv.Text = "RECTIFICACIÓN FAC-2024-001"
    inv.InvoiceType = "R1"

    ' Referencia a la factura rectificada
    Dim rectItem As New Verifactu.VfRectificationItem
    rectItem.InvoiceID = "FAC-2024-001"
    rectItem.InvoiceDate = CDate("15/11/2024")
    inv.InsertRectificationItem rectItem

    ' Diferencia de IVA a corregir
    Dim taxItem As New Verifactu.VfTaxItem
    taxItem.TaxBase = -100
    taxItem.TaxRate = 21
    taxItem.TaxAmount = -21
    inv.InsertTaxItem taxItem

    Dim result As Verifactu.IVfInvoiceResult
    Set result = inv.Send()

    If result.ResultCode = "0" Then
        MsgBox "Rectificativa enviada. CSV: " & result.CSV
    Else
        MsgBox "Error: " & result.ResultCode & " - " & result.ResultMessage
    End If
End Sub

Resultado de la operación

Both Send() and Delete() return an IVfInvoiceResult object with the following properties:
ResultCode
string
Operation result code. "0" indicates success. Any other value indicates an error:
  • "9001" — Local exception (e.g. configuration error, network unreachable)
  • "9002:..." — SOAP fault returned by the AEAT web service
  • "9009" — Unknown error
  • Other codes — AEAT application-level error codes returned in the response
ResultMessage
string
Human-readable description of the result. "OK" on success; error description on failure.
CSV
string
Código Seguro de Verificación (CSV) assigned by the AEAT upon successful registration. This is the unique identifier for the submitted record on the AEAT side. Empty on failure.
Response
string
The raw XML response body returned by the AEAT web service. Useful for debugging or audit logging.

Obtener el QR de validación

You can generate the AEAT validation QR code for any invoice without sending it:
Sub GenerarQR()
    Dim inv As New Verifactu.VfInvoice
    inv.InvoiceID = "FAC-2024-001"
    inv.InvoiceDate = CDate("15/11/2024")
    inv.SellerID = "B72877814"
    inv.SellerName = "MI EMPRESA SL"
    inv.InvoiceType = "F1"

    Dim taxItem As New Verifactu.VfTaxItem
    taxItem.TaxBase = 1000
    taxItem.TaxRate = 21
    taxItem.TaxAmount = 210
    inv.InsertTaxItem taxItem

    ' Guardar imagen QR en disco
    inv.GetValidateQr "C:\Temp\qr_fac2024001.bmp"

    ' O bien obtener la URL de validación como texto
    Dim url As String
    url = inv.GetUrlValidate()
    MsgBox "URL validación: " & url
End Sub

Validar un NIF contra la AEAT

Sub ValidarNIF()
    Dim inv As New Verifactu.VfInvoice
    Dim errores As String
    errores = inv.GetNifErrors("B72877814", "MI EMPRESA SL")
    If errores = "" Or IsNull(errores) Then
        MsgBox "NIF válido."
    Else
        MsgBox "Errores NIF: " & errores
    End If
End Sub

Uso sin referencia (late binding)

If you prefer not to add a VBA reference (late binding), use CreateObject instead of New:
Sub EnviarFacturaLateBinding()
    Dim cfg As Object
    Set cfg = CreateObject("Verifactu.VfSettings")
    cfg.CertificatePath = "C:\MiCertificado.pfx"
    cfg.CertificatePassword = "mi_password"
    cfg.Save

    Dim inv As Object
    Set inv = CreateObject("Verifactu.VfInvoice")
    inv.InvoiceID = "FAC-2024-003"
    inv.InvoiceDate = CDate("15/11/2024")
    inv.SellerID = "B72877814"
    inv.SellerName = "MI EMPRESA SL"
    inv.BuyerID = "B44531218"
    inv.BuyerName = "CLIENTE SL"
    inv.Text = "SERVICIOS"
    inv.InvoiceType = "F1"

    Dim taxItem As Object
    Set taxItem = CreateObject("Verifactu.VfTaxItem")
    taxItem.TaxBase = 1000
    taxItem.TaxRate = 21
    taxItem.TaxAmount = 210
    inv.InsertTaxItem taxItem

    Dim result As Object
    Set result = inv.Send()

    MsgBox "ResultCode: " & result.ResultCode & " | CSV: " & result.CSV
End Sub
Late binding does not require a VBA reference and works even without the .tlb type library, but you lose IntelliSense and compile-time type checking.

Build docs developers (and LLMs) love