繁体   English   中英

如何使用VBA从Excel文件中将&符号写入XML文件?

[英]How to write an ampersand to an XML file from an Excel file using VBA?

首先,当谈到VBA时,我是一个完全新手,但不幸的是我被抛弃了这个代码,我必须处理它......

该应用程序的作用是将信息复制到Excel xlsm文件中,并将其粘贴到XML文件中以供进一步处理。

问题是,直到我在其中一个Excel单元格中点击&符号,这一切都非常顺利,即:

我有一个具有“R&D”的单元格,当我将它传递给XML文件时,我收到以下错误:

Run-time error '91':
Object variable or With block variable not set

请记住,我是VBA完全垃圾。 我尝试更改单元格中的内容为“R && D”,“R & D”,但没有骰子。

我相信单元格的价值来自这行代码:

oCell.Offset(, 0).Value

但是我想要一些帮助,如何逃脱&符号......

非常感谢,如果您需要更多信息,请告诉我。

我为Access编写了以下函数,但它应该适用于Excel。

Function CleanupStr(strXmlValue) As String  
 'description: Replace forbidden char. &'"<> by their Predefined General Entities   
 'author:      Patrick Honorez - www.idevlop.com   
   Dim sValue As String  
   If IsNull(strXmlValue) Then  
     CleanupStr = ""  
   Else  
     sValue = CStr(strXmlValue)  
     sValue = Replace(sValue, "&", "&amp;") 'do ampersand first !  
     sValue = Replace(sValue, "'", "&apos;")  
     sValue = Replace(sValue, """", "&quot;")  
     sValue = Replace(sValue, "<", "&lt;")  
     sValue = Replace(sValue, ">", "&gt;")  
     CleanupStr = sValue  
   End If  
End Function 

您正在寻找的是在字符串中创建html实体的方法,这涉及使用&和任何特殊字符的代码。 '&'是一个特殊的字符本身。

可能有代码需要更改才能执行此操作,但同时在代码替换中

&

&amp;

你应该解决那个特殊的问题。

我在这里找到这两个辅助函数:

Public Function HTMLToCharCodes(ByVal iString As String) As _
    String
    With New MSXML2.DOMDocument30
        .loadXML "<p>" & iString & "</p>"
        HTMLToCharCodes = _
            .selectSingleNode("p").nodeTypedValue
    End With
End Function

Public Function CharCodesToHTML(ByVal iString As String) As _
    String
Dim iXml As New MSXML2.DOMDocument30

    With iXml.createTextNode(iString)
        CharCodesToHTML = .xml
    End With
End Function

暂无
暂无

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

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