Tuesday, December 24, 2024

VBA Limitations

VBA (Visual Basic for Applications) is tightly integrated with the host application in which it runs (e.g., Microsoft Word, Excel, or PowerPoint).

  • Dependency on the Host Application: VBA cannot function independently. It requires a host application to be open because the VBA environment is part of that application. For example, to run VBA code written for Excel, you must have Excel open.
  • No Stand-Alone Applications: You cannot create a stand-alone executable (.exe) application with VBA. However, you can mimic a stand-alone application by hiding the host application (e.g., Excel or Word) while displaying user forms created in VBA. This gives the appearance of a separate application, but the host application is still running in the background.
  • VBA Environment Installation: The VBA environment is installed along with the host application (e.g., when you install Microsoft Office). The environment is loaded from your computer's hard disk when you open the host application.
  • Closure of VBA Environment: Since VBA is tied to the host application, closing the host application will also terminate the VBA environment and any running VBA code.

In summary, VBA is not a general-purpose programming platform. It is designed to extend and automate tasks within its host applications, and its functionality ends when the host application is closed.

 

VBA Word - Delete images from document with specific URL

The following VBA code deletes images from document with specific URL:
Sub DeleteImagesWithSpecificURL()
    Dim iShape As InlineShape
    Dim shp As Shape
    Dim doc As Document
    Dim searchURL As String
    
    searchURL = "https://hoven.in/aspnet-core/asp-net-core-course-buy.html"
    
    Set doc = ActiveDocument
    
    ' Check InlineShapes (images in the text flow)
    For Each iShape In doc.InlineShapes
        If iShape.Type = wdInlineShapePicture Then
            If InStr(1, iShape.AlternativeText, searchURL, vbTextCompare) > 0 Then
                iShape.Delete
            End If
        End If
    Next iShape
    
    ' Check Shapes (floating images)
    For Each shp In doc.Shapes
        If shp.Type = msoPicture Then
            If InStr(1, shp.AlternativeText, searchURL, vbTextCompare) > 0 Then
                shp.Delete
            End If
        End If
    Next shp
End Sub

Hot Topics