Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visio VBA:嵌套循环中的参数无效_Vba_Nested Loops_Visio - Fatal编程技术网

Visio VBA:嵌套循环中的参数无效

Visio VBA:嵌套循环中的参数无效,vba,nested-loops,visio,Vba,Nested Loops,Visio,在MicrosoftVisioProfessional2010中,我已经隔离了在这个小代码片段中遇到的错误。页面上有一个包含2个形状的容器,我想在另一个循环中遍历这些形状。但我一直得到一个无效的参数错误 我尝试的解决方案是顶部块,但它只适用于内部循环的相同定义。在外循环的第二次迭代过程中似乎有什么变化,但我不确定。我觉得这与定义For-Each循环的方式有关 Sub Nested_Loop_Error() Dim a As Variant Dim b As Variant

在MicrosoftVisioProfessional2010中,我已经隔离了在这个小代码片段中遇到的错误。页面上有一个包含2个形状的容器,我想在另一个循环中遍历这些形状。但我一直得到一个无效的参数错误

我尝试的解决方案是顶部块,但它只适用于内部循环的相同定义。在外循环的第二次迭代过程中似乎有什么变化,但我不确定。我觉得这与定义For-Each循环的方式有关

Sub Nested_Loop_Error()

    Dim a As Variant
    Dim b As Variant
    Dim lngs() As Long

    'This Works
    lngs = ActiveDocument.Pages(1).Shapes.ItemFromID(1).ContainerProperties.GetMemberShapes(visContainerFlagsDefault)
    For a = 0 To 1
        For Each b In lngs
            'Do nothing
        Next b
    Next a


    'This does not work
    For a = 0 To 1
        For Each b In ActiveDocument.Pages(1).Shapes.ItemFromID(1).ContainerProperties.GetMemberShapes(visContainerFlagsDefault)
            MsgBox "In Loop for a=" & a
        Next b
    Next a

End Sub
编辑: 我一直在玩弄它,并让它发挥作用,但我真正感兴趣的是它为什么起作用。当a=1时,第二个代码块失败,在docMyDoc.Pages行中给出了一个无效参数

下面的代码显示了使用变量或文档变量在循环中定义ActiveDocument的区别。使用调试器,我看不出docMyDoc或varMyDoc的定义方式有什么不同

Sub Nested_Loop_Error2()
    Dim a As Variant
    Dim b As Variant
    Dim docMyDoc As Visio.Document
    Dim varMyDoc As Variant

    'This works
    For a = 0 To 1
        Set varMyDoc = ActiveDocument
        For Each b In varMyDoc.Pages(1).Shapes.ItemFromID(1).ContainerProperties.GetMemberShapes(visContainerFlagsDefault)
            MsgBox "Using variant, a=" & a
        Next b
    Next a

    'This does not work
    For a = 0 To 1
        Set docMyDoc = ActiveDocument
        For Each b In docMyDoc.Pages(1).Shapes.ItemFromID(1).ContainerProperties.GetMemberShapes(visContainerFlagsDefault)
            MsgBox "Using document, a=" & a
        Next b
    Next a
End Sub

我的计算机上没有Visio,但您确定第一个嵌套循环有效吗

我对
lngs=ActiveDocument.Pages(1).
中的
Dim lngs()表示怀疑,只要

Excel VBA在尝试使用
Dim arr()存储
arr=Array(1,2)
时将抛出“类型不匹配”错误,只要
。最好将lngs作为变量进行调整,即使您知道它是一个长时间返回的数组


第二个嵌套循环在理论上起作用。

使用变量类型对编译器没有多大帮助:名为“b”的变量应为Long类型,“a”的变量应为Integer类型

这就是说,您没有使用“a”变量,而是重复两次您在内部循环(Msgbox)中所做的操作,但没有其他更改

此外,您需要引用ID为b的形状,而您没有这样做

还有一个技巧:不要以变量的类型命名,而要以它们的语义命名

我认为您打算做的事情与以下示例类似:

在这里,vsoShape变量将首先引用一个形状,然后引用另一个形状。即使你的页面中有更多的形状,它也能工作

这是集合和For-Each循环的优点:集合是作为其他对象列表组成的特殊对象。它们有自己的方法,如Item、Count和快捷方式,比如在括号之间使用数字从集合中检索单个对象(如第(1)页)

对每个对象所做的是遍历集合中的所有对象(或数组中的所有值)

出于您的目的,我将尝试以下总体结构:

dim oPage as Visio.Page
dim oShape as Visio.Shape
dim oInnerShape as Visio.Shape

For each oPage In ActiveDocument.Pages
  For each oShape in oPage.Shapes
      If oShape.Master.Name = "xxx" Then       ' You can check the type of the shape
          For each oInnerShape In oShape
             ' set and compute width and height
          Next oInnerShape
          ' set and compute width and height of the containing shape
       End If
  Next oShape
  ' Rearrange shapes
Next oPage
可以在遍历形状时构造一个存储形状ID、宽度和高度的数组,然后使用该数组重新排列形状


关于,

首先,lngs的值是多少?如果您现在在有
什么都不做
注释的地方“做了什么”,会发生什么?如果没有一个
循环的外部
,第二个代码段是否可以工作?内部循环本身可以工作,甚至在外部循环的第一次迭代中也可以工作。但是在外循环的第二次迭代中失败了。顶层块在嵌套循环中使用和不使用实际语句。为什么需要外部FOR..NEXT?您是否正在尝试遍历文档中的所有页面?而且。。。您是否调试了代码,但在第二个嵌套的“For each b…”行中粘贴代码时失败了?你有没有在没有Msgbox的情况下测试过它,所以它也“什么都不做”?我正在迭代页面,然后是页面上的泳道,然后是泳道中的容器,等等。我已经填充了所有内容,但是页面上的对象太多,自动定位无法正常工作,所以我正在编写一些代码来正确组织所有内容。我把这个问题归结为一个bug,因为它应该可以工作,除非定义一个变量作为一个变量会让它神奇地工作得更好。至少您应该能够在“局部变量”窗口中看到差异,因为varMyDoc必须是Variant/Document类型,而docMyDoc必须是Document类型,对吗?此代码在形状中迭代一次。OP的问题似乎是“在形状中循环两次”。为什么内部循环会再次抛出错误?@Floris,好吧,谢谢你,我不明白a变量的用途。不过,我不会对变量使用Variant类型。我测试了最后一个代码片段,它可以运行两次、三次和更多次(我已经测试了多达十次)。由于Visio 2003具有不同的对象模型,我无法测试有问题的代码。我已编辑了我的答案。我知道我没有回答错误发生的原因。我正在给出一个我认为应该有效的解决方案。也许我错了,但我真的不相信这个问题提供了准确的信息。
dim oPage as Visio.Page
dim oShape as Visio.Shape
dim oInnerShape as Visio.Shape

For each oPage In ActiveDocument.Pages
  For each oShape in oPage.Shapes
      If oShape.Master.Name = "xxx" Then       ' You can check the type of the shape
          For each oInnerShape In oShape
             ' set and compute width and height
          Next oInnerShape
          ' set and compute width and height of the containing shape
       End If
  Next oShape
  ' Rearrange shapes
Next oPage