分割函数中的VBA类型不匹配
我已经编写了一个代码,需要从如下字符串中获取数字:“3/1”。我需要将这两个数字作为整数存储在两个不同的变量中。我已将此代码分为两类编写: 这个函数是我在类中的拆分函数(BigOne) 在main类中,我有一个调用此函数的函数,该函数拆分数字以获得我想要的值。然而,我得到了一个“类型不匹配错误”,我无法检测出这种类型不匹配的原因分割函数中的VBA类型不匹配,vba,excel,Vba,Excel,我已经编写了一个代码,需要从如下字符串中获取数字:“3/1”。我需要将这两个数字作为整数存储在两个不同的变量中。我已将此代码分为两类编写: 这个函数是我在类中的拆分函数(BigOne) 在main类中,我有一个调用此函数的函数,该函数拆分数字以获得我想要的值。然而,我得到了一个“类型不匹配错误”,我无法检测出这种类型不匹配的原因 Public Function get_MaxChars(pInput As String) As Integer 'declaration of variables
Public Function get_MaxChars(pInput As String) As Integer
'declaration of variables
Dim gen As cBigOne
Dim values As String
'Main code
pInput = CStr(pInput)
Debug.Print (pInput)
values = gen.SplitValues(pInput, "/")
get_MaxChars = CInt(values(0))
End Function
因此,我无法理解为什么它不能正常工作,并且我得到了类型不匹配错误。因为,我相信我传递的每一个类型都是相同的。
SplitValues
返回一个字符串数组,您试图将其分配给一个字符串。尝试将值变暗为String()
调用SplitValues
时仍然会遇到问题,因为您尚未创建类的实例,只是说gen
将是一个实例。在将gen设置为cBigOne
后,您应该将gen设置为新的cBigOne
我也遇到了同样的问题。然后我发现必须将数组声明为VARIANT
这是我的代码,我将Outlook中的每封邮件的附件保存到指定的文件夹中
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
Dim objOL As Outlook.Application
Dim arr As Variant
Dim i As Integer
Dim ns As Outlook.NameSpace
Dim itm As MailItem
Dim m As Outlook.MailItem
Dim j As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
Dim fs As FileSystemObject
Dim mldat As Date
Dim strsndr As String
'On Error Resume Next
Set ns = Application.Session
arr = Split(EntryIDCollection, ",")
'*******************************************************************************************
' Set the Attachment folder.
strFolderpath = "z:\dropbox (AAA-DZ)\25_Attach\"
' Check each selected item for attachments. If attachments exist,
' save them to the strFolderPath folder and strip them from the item.
For ij = 0 To UBound(arr)
Set itm = ns.GetItemFromID(arr(ij))
If itm.Class = olMail Then
With itm
' This code only strips attachments from mail items.
' If objMsg.class=olMail Then
' Get the Attachments collection of the item.
Set objAttachments = .Attachments
lngCount = objAttachments.Count
strDeletedFiles = ""
If lngCount > 0 Then
' We need to use a count down loop for removing items
' from a collection. Otherwise, the loop counter gets
' confused and only every other item is removed.
Set fs = New FileSystemObject
For i = lngCount To 1 Step -1
' Save attachment before deleting from item.
' Get the file name.
strFile = Right("0000" + Trim(Str$(Year(.SentOn))), 4) + "_" + Right("00" + Trim(Str$(Month(.SentOn))), 2) + "_" + Right("00" + Trim(Str$(Day(.SentOn))), 2) + "_" + Right("00" + Trim(Str$(Hour(.SentOn))), 2) + "_" + Right("00" + Trim(Str$(Minute(.SentOn))), 2) + "_" + Right("00" + Trim(Str$(Second(.SentOn))), 2) + "_" + .SenderEmailAddress + "_" + .Parent + "_" + objAttachments.Item(i).FileName
' Combine with the path to the Temp folder.
strFile = strFolderpath & strFile
' Save the attachment as a file.
If Left(objAttachments.Item(i).FileName, 5) <> "image" Then
objAttachments.Item(i).SaveAsFile strFile
' Delete the attachment.
objAttachments.Item(i).Delete
'write the save as path to a string to add to the message
'check for html and use html tags in link
If .BodyFormat <> olFormatHTML Then
strDeletedFiles = strDeletedFiles & vbCrLf & "<file://" & strFile & ">"
Else
strDeletedFiles = strDeletedFiles & "<br>" & "<a href='file://" & _
strFile & "'>" & strFile & "</a>"
End If
Else
objAttachments.Item(i).Delete
End If
Next i
' Adds the filename string to the message body and save it
' Check for HTML body
If Len(strDeletedFiles) > 5 Then
If .BodyFormat <> olFormatHTML Then
.Body = vbCrLf & "The file(s) were saved to " & strDeletedFiles & vbCrLf & .Body
Else
.HTMLBody = "<p>" & "The file(s) were saved to " & strDeletedFiles & "</p>" & .HTMLBody
End If
.Save
End If
Else
objAttachments.Item(1).Delete
End If
End With
End If
Next ij
ExitSub:
Set objAttachments = Nothing
Set objSelection.Item(ij) = Nothing
Set objSelection = Nothing
Set objOL = Nothing
'********************************************************************************************
Set ns = Nothing
Set itm = Nothing
Set m = Nothing
End Sub
Private子应用程序\u NewMailEx(ByVal entrydcollection作为字符串)
将objOL设置为Outlook.Application
作为变体的Dim-arr
作为整数的Dim i
将ns设置为Outlook.NameSpace
将itm设置为邮件项
将m设置为Outlook.MailItem
Dim j尽可能长
暗计数等于长
作为字符串的Dim strFile
将strFolderpath设置为字符串
将strDeletedFile设置为字符串
将fs设置为文件系统对象
Dim mldat作为日期
Dim strsndr As字符串
'出现错误时,请继续下一步
Set ns=Application.Session
arr=拆分(EntrydCollection,“,”)
'*******************************************************************************************
'设置附件文件夹。
strFolderpath=“z:\dropbox(AAA-DZ)\25\u附加\”
'检查每个选定项目的附件。如果存在附件,
'将它们保存到strFolderPath文件夹并从项目中删除。
对于ij=0至UBound(arr)
设置itm=ns.GetItemFromID(arr(ij))
如果itm.Class=olMail,则
使用itm
'此代码仅从邮件项目中删除附件。
'如果objMsg.class=olMail,则
'获取项目的附件集合。
设置objAttachments=.Attachments
lngCount=objAttachments.Count
strDeletedFiles=“”
如果lngCount>0,则
“我们需要使用倒计时循环来删除项目
“从一个集合中。否则,循环计数器将
“混乱,只有其他项目被删除。
Set fs=New FileSystemObject
对于i=lngCount到1步骤-1
'在从项目中删除之前保存附件。
'获取文件名。
strFile=Right(“0000”+Trim(Str$(Year(.SentOn)))、4)+“+”Right(“00”+Trim(Str$(Month(.SentOn)))、2)+“+”Right(“00”+Trim(Str$(Day(.SentOn)))、2)+”Right(“00”+Trim(Str$(Hour(.SentOn))、2)+“00”+Right(“00”+Trim(Str Minute(.Minute(.SentOn))、2)+“00”+Trim(Str$)、secondary(.sendon+.Parent+“”+objAttachments.Item(i).文件名
'与临时文件夹的路径合并。
strFile=strFolderpath&strFile
'将附件另存为文件。
如果左(objAttachments.Item(i).FileName,5)“image”,则
objAttachments.Item(i).SaveAsFile strFile
'删除附件。
附件。第(i)项。删除
'将另存为路径写入要添加到消息的字符串
'检查html并在链接中使用html标记
如果.BodyFormat是HTML格式,那么
strDeletedFiles=strDeletedFiles&vbCrLf&“
其他的
strDeletedFiles=strDeletedFiles&“
”&”
如果结束
其他的
附件。第(i)项。删除
如果结束
接下来我
'将文件名字符串添加到消息正文并保存它
'检查HTML正文
如果Len(strDeletedFiles)>5,则
如果.BodyFormat是HTML格式,那么
.Body=vbCrLf&“文件已保存到”&strDeletedFiles&vbCrLf&.Body
其他的
.HTMLBody=“”和“文件已保存到”&strDeletedFile&“”和.HTMLBody
如果结束
拯救
如果结束
Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
Dim objOL As Outlook.Application
Dim arr As Variant
Dim i As Integer
Dim ns As Outlook.NameSpace
Dim itm As MailItem
Dim m As Outlook.MailItem
Dim j As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
Dim fs As FileSystemObject
Dim mldat As Date
Dim strsndr As String
'On Error Resume Next
Set ns = Application.Session
arr = Split(EntryIDCollection, ",")
'*******************************************************************************************
' Set the Attachment folder.
strFolderpath = "z:\dropbox (AAA-DZ)\25_Attach\"
' Check each selected item for attachments. If attachments exist,
' save them to the strFolderPath folder and strip them from the item.
For ij = 0 To UBound(arr)
Set itm = ns.GetItemFromID(arr(ij))
If itm.Class = olMail Then
With itm
' This code only strips attachments from mail items.
' If objMsg.class=olMail Then
' Get the Attachments collection of the item.
Set objAttachments = .Attachments
lngCount = objAttachments.Count
strDeletedFiles = ""
If lngCount > 0 Then
' We need to use a count down loop for removing items
' from a collection. Otherwise, the loop counter gets
' confused and only every other item is removed.
Set fs = New FileSystemObject
For i = lngCount To 1 Step -1
' Save attachment before deleting from item.
' Get the file name.
strFile = Right("0000" + Trim(Str$(Year(.SentOn))), 4) + "_" + Right("00" + Trim(Str$(Month(.SentOn))), 2) + "_" + Right("00" + Trim(Str$(Day(.SentOn))), 2) + "_" + Right("00" + Trim(Str$(Hour(.SentOn))), 2) + "_" + Right("00" + Trim(Str$(Minute(.SentOn))), 2) + "_" + Right("00" + Trim(Str$(Second(.SentOn))), 2) + "_" + .SenderEmailAddress + "_" + .Parent + "_" + objAttachments.Item(i).FileName
' Combine with the path to the Temp folder.
strFile = strFolderpath & strFile
' Save the attachment as a file.
If Left(objAttachments.Item(i).FileName, 5) <> "image" Then
objAttachments.Item(i).SaveAsFile strFile
' Delete the attachment.
objAttachments.Item(i).Delete
'write the save as path to a string to add to the message
'check for html and use html tags in link
If .BodyFormat <> olFormatHTML Then
strDeletedFiles = strDeletedFiles & vbCrLf & "<file://" & strFile & ">"
Else
strDeletedFiles = strDeletedFiles & "<br>" & "<a href='file://" & _
strFile & "'>" & strFile & "</a>"
End If
Else
objAttachments.Item(i).Delete
End If
Next i
' Adds the filename string to the message body and save it
' Check for HTML body
If Len(strDeletedFiles) > 5 Then
If .BodyFormat <> olFormatHTML Then
.Body = vbCrLf & "The file(s) were saved to " & strDeletedFiles & vbCrLf & .Body
Else
.HTMLBody = "<p>" & "The file(s) were saved to " & strDeletedFiles & "</p>" & .HTMLBody
End If
.Save
End If
Else
objAttachments.Item(1).Delete
End If
End With
End If
Next ij
ExitSub:
Set objAttachments = Nothing
Set objSelection.Item(ij) = Nothing
Set objSelection = Nothing
Set objOL = Nothing
'********************************************************************************************
Set ns = Nothing
Set itm = Nothing
Set m = Nothing
End Sub