Compiler error: Sub or function not defined

Compiler error: Sub or function not defined

我正在尝试从子例程中定义和调用函数。当我 运行 子例程时,我得到“ 编译器错误:子程序或函数未定义 ”。为什么会这样?

我要调用的函数是下面的 GetImageHeight,但其他任何函数也是如此。

这里是代码:

Function FileExists(FilePath As String) As Boolean
 
         
    On Error Resume Next
    If Len(FilePath) > 0 Then
        If Not Dir(FilePath, vbDirectory) = vbNullString Then FileExists = True
    End If
    On Error GoTo 0
 
End Function
 
Function IsValidImageFormat(FilePath As String) As Boolean
 
    Dim imageFormats    As Variant
    Dim i               As Integer
 
    imageFormats = Array(".bmp", ".jpg", ".gif", ".tif", ".png")
 
          For i = LBound(imageFormats) To UBound(imageFormats)
                If InStr(1, UCase(FilePath), UCase(imageFormats(i)), vbTextCompare) > 0 Then
            IsValidImageFormat = True
            Exit Function
        End If
    Next I
 
End Function

Sub DeleteImages()
Dim ThisImage As InlineShape
Dim Height As Double
Dim Width As Double
Dim TotalCount As Integer
Dim Count As Integer
Dim Source As String
Dim ImageHeightPx As Double
Dim ImageWidthPx As Double
Dim ImagePath As String
Dim ImageName As String
Dim FileName As String
ImagePath = "C:\Users\User\Image\"

FileName = Mid(ActiveDocument.Name, 1, InStr(1, ActiveDocument.Name, ".") - 1)
Set myStyle = ActiveDocument.Styles.Add(Name:="Replaced Image", Type:=wdStyleTypeCharacter)

TotalCount = ActiveDocument.InlineShapes.Count
ImageCount = 1

For Each ThisImage In ActiveDocument.InlineShapes
    ImageName = FileName & "_IMG_" & Trim(Str(ImageCount))
    MsgBox ImageName
    ThisImage.Select
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Style = "Replaced Image"
    Selection.TypeText Text:="[[[ " & ImageName & " ]]]"
    
    ImageHeightPx = GetImageHeight(ImagePath & ImageName & ".png")
    ImageWidthPx = GetImageWidth(ImagePath & ImageName & ".png")
    
    MsgBox "Height: " & Str(ImageHeightPx)
    MsgBox "Width: " & Str(ImageWidthPx)
    
    ImageCount = ImageCount + 1
Next ThisImage

End Sub

Function GetImageHeight(ImagePath As String) As Variant
 
    Dim imgHeight As Integer
    Dim wia As Object
 
    If FileExists(ImagePath) = False Then Exit Function
    If IsValidImageFormat(ImagePath) = False Then Exit Function
 
    On Error Resume Next
    Set wia = CreateObject("WIA.ImageFile")
    If wia Is Nothing Then Exit Function
    On Error GoTo 0
 
    wia.LoadFile ImagePath
 
    imgHeight = wia.Height
 
    Set wia = Nothing
 
    GetImageHeight = imgHeight
 
End Function
 
Function GetImageWidth(ImagePath As String) As Variant
 
    Dim imgWidth As Integer
    Dim wia As Object
 
    If FileExists(ImagePath) = False Then Exit Function
 
    If IsValidImageFormat(ImagePath) = False Then Exit Function
 
    On Error Resume Next
    Set wia = CreateObject("WIA.ImageFile")
    If wia Is Nothing Then Exit Function
    On Error GoTo 0
 
    wia.LoadFile ImagePath
 
    imgWidth = wia.Width
 
    Set wia = Nothing
 
    GetImageWidth = imgWidth
 
End Function

编辑:用代码替换了屏幕截图。

  1. 检查,您已将源示例中的 FileExists() 和 IsValidImageFormat() 复制到您的模块中。

  2. 检查,您已经为项目select编辑了 WIA 库

要添加 WIA 2.0 库:

  • 从“项目”菜单中单击“组件”(或按 Ctrl-T)。
  • 向下滚动并select Microsoft Windows Image Acquisition Library v2.0 通过在它前面放置一个复选标记。在三个新控件中 出现在工具箱中。