简体   繁体   English

Outlook VBA —修剪不可打印的字符

[英]Outlook VBA — Trimming non-printable characters

I am having a real struggle trying to get a string tied down to just what I need in Outlook VBA. 我一直在努力将字符串绑定到我在Outlook VBA中所需要的东西。 I have tried 100 different things from all over the internet, including here. 我已经尝试了Internet上所有100种不同的方法,包括这里。

I use the following: 我使用以下内容:

CString = LTrim$(RTrim$(Mid(itm.Body, InStr(1, itm.Body, CCMark) + Len(CCMark), _
            InStr(InStr(1, itm.Body, CCMark) + Len(CCMark), itm.Body, CCMark) - (InStr(1, itm.Body, CCMark) + Len(CCMark)))))

CCMark = "C/" CCMark =“ C /”

But since the body of the email contains: C/ test C/, the variable (CString) goes to " test ". 但是由于电子邮件的正文包含:C / test C /,因此变量(CString)转到“ test”。

I have tried using a second variable with Trim, RTrim, LTrim, Trim$, LTrim$, and RTrim$. 我尝试将第二个变量与Trim,RTrim,LTrim,Trim $,LTrim $和RTrim $一起使用。 Additionally, I've tried REPLACE with double space and single space. 此外,我尝试过使用双空格和单空格替换。 I have tried the TrimAll function that is popular on the internet that tries to find the different Chr() values as well as vbTab, etc. None of those seem to replace anything. 我尝试了TrimAll函数,该函数在Internet上很流行,它试图查找不同的Chr()值以及vbTab等。这些函数似乎都不能替代任何东西。

String stays the same. 字符串保持不变。

Is this a fixed-length vs. variable-length ("sizable") string issue? 这是固定长度还是可变长度(“可调整大小”)字符串问题? I have not found a way to convert from fixed-length to variable. 我还没有找到一种从定长转换为变量的方法。 Passing through a function has not worked. 传递功能无效。

Any advice as to how I can come out with "test" as the result? 关于如何进行“测试”结果的任何建议?

Thank you very much for your help. 非常感谢您的帮助。

To put it simply: the string handling functions in VBA do in fact work. 简单地说:VBA中的字符串处理功能实际上可以正常工作。 If your string has space characters (meaning specifically the character with code point 32) at the start or end of the input string then they will be removed. 如果您的字符串在输入字符串的开头或结尾处有空格字符(特别是带有代码点32的字符),则将其删除。

"Fixed-length" strings exist only if you specifically declare them using special syntax: 仅当使用特殊语法专门声明“固定长度”字符串时,才存在:

Dim eightCharString As String(8)  ' This is a fixed-length string

None of the VBA string functions return fixed length strings. VBA字符串函数均未返回固定长度的字符串。 So unless you declared CString as a fixed length string using the above notation, that is not the issue. 因此,除非您使用上述符号将CString声明为固定长度的字符串,否则不会出现问题。

Logically the only possibility is that the characters you think are spaces are not in fact spaces. 从逻辑上讲,唯一的可能性是您认为空格的字符实际上不是空格。 A highly likely candidate in an email is that they are HTML non-breaking space characters, or code point 0xA0 (decimal 160). 电子邮件中极有可能的候选对象是它们是HTML不间断空格字符或代码点0xA0(十进制160)。 By far the easiest way to replace multiple characters in a source string is using a regular expression. 到目前为止,替换源字符串中多个字符的最简单方法是使用正则表达式。

Here is the typical regex-based trim function. 这是典型的基于正则表达式的修剪函数。 The two patterns as constructed below are 下面构造的两个模式是

Start pattern: "^[\u0009\u0020\u00A0]*"
End pattern:   "[\u0009\u0020\u00A0]*$"

If you want to find and remove additional space characters just add their code point values to the list in the function below. 如果要查找和删除其他空格字符,只需将其代码点值添加到以下功能的列表中。 For example, to consider line feeds and carriage returns to be spaces that you want to trim, add the code points 10 and 13 to the list. 例如,要考虑换行符和回车符是要修剪的空格,请将代码点10和13添加到列表中。

Code: 码:

Private m_RxTrimStart As RegExp
Private m_RxTrimEnd   As RegExp 

Public Function RxTrim(ByRef Source As String) As String 

    ' Only create and compile the RegExp objects once
    If m_RxTrimStart Is Nothing Then

        ' A verbose way of constructing the regex object so you
        ' can see exactly what's going on  
        Dim spaceCodePoints As Variant, vCodePoint
        Dim strSpaceClass As String, strHexPoint As String

        ' Add additional code points here if desired
        spaceCodePoints = Array(9, 32, 160)
        strSpaceClass = "["
        For Each vCodePoint In spaceCodePoints
            ' Assemble a four-character hex code point 
            strHexPoint   = Hex$(CLng(vCodePoint))
            strHexPoint   = String("0", 4 - Len(strHexPoint)) & strHexPoint
            strSpaceClass = strSpaceClass & "\u" & strHexPoint
        Next 
        strSpaceClass = strSpaceClass & "]*" 

        ' Start anchor + spaces character class
        Set m_RxTrimStart = New RegExp
        m_RxTrimStart.Pattern = "^" & strSpaceClass

        ' Spaces character class + end anchor
        Set m_RxTrimEnd   = New RegExp
        m_RxTrimEnd.Pattern   = strSpaceClass & "$"
    End If

    RxTrim = m_RxTrimEnd.Replace(m_RxTrimStart.Replace(Source, ""), "")

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM