Re: Object variable or With block variable not set

Tech-Archive recommends: Fix windows errors by optimizing your registry



On 6月4日, 上午11時30分, hon123456 <peterhon...@xxxxxxxxxxxx> wrote:
Dear all,

I have the following code:

Private Function MakeDoc(nl As IXMLDOMNodeList, elemName As String) As
DOMDocument40
Dim doc As New MSXML2.DOMDocument
doc.LoadXml "<" & elemName & ">"

Dim n As IXMLDOMNode

For Each n In nl
Select Case n.nodeType
Case NODE_ATTRIBUTE
doc.documentElement.setAttribute n.nodeName,
n.nodeValue
Case NODE_ELEMENT, NODE_TEXT
doc.documentElement.appendChild n
End Select
Next

Set MakeDoc = doc
End Function

When it run this line
doc.documentElement.appendChild n

VB complains Object variable or With block variable not set

What's wrong with my code . Thanks.

Sorry The Code should be

Private Function MakeDoc(nl As IXMLDOMNodeList, elemName As String) As
DOMDocument40
Dim doc As New MSXML2.DOMDocument40
doc.LoadXml "<" & elemName & ">"

Dim n As IXMLDOMNode

For Each n In nl
Select Case n.nodeType
Case NODE_ATTRIBUTE
doc.documentElement.setAttribute n.nodeName,
n.nodeValue
Case NODE_ELEMENT, NODE_TEXT
doc.documentElement.appendChild n
End Select
Next

Set MakeDoc = doc
End Function
.



Relevant Pages

  • Re: Object variable or With block variable not set
    ... can't you get a reference to the original document via the first node of the node list? ... > Private Function MakeDoc ... > Dim doc As New MSXML2.DOMDocument ... > Dim n As IXMLDOMNode ...
    (microsoft.public.vb.general.discussion)
  • Object variable or With block variable not set
    ... DOMDocument40 ... Dim doc As New MSXML2.DOMDocument ... Dim n As IXMLDOMNode ... Set MakeDoc = doc ...
    (microsoft.public.vb.general.discussion)
  • Re: Object variable or With block variable not set
    ... elemName As String) As ... Dim doc As New MSXML2.DOMDocument ... Dim n As IXMLDOMNode ... Set MakeDoc = doc ...
    (microsoft.public.vb.general.discussion)
  • Re: Object variable or With block variable not set
    ... elemName As String) As ... DOMDocument40 ... Dim doc As New MSXML2.DOMDocument40 ...
    (microsoft.public.vb.general.discussion)
  • Re: Save User Information
    ... Function SetCustomProperty(strPropName As String, intPropType _ ... Dim doc As Document, prp As Property ... Set cnt = dbs.Containers!Databases ' Define Container object. ... Set doc = cnt.Documents!UserDefined ' Define Document object. ...
    (microsoft.public.access.modulesdaovba)