高手救我!!!
  用VC写了个Activex,并写了个MFC应用程序,并写了个VBA脚本.工作基本正常(这是最可恨的),有一个很不明白的地方,该VBA插入在Word中工作完全正确,能正确判断各个元素,但单独保存为一个TXT脚本后在应用程序中调用,能工作,却只有一部分能正确判断,有些地方就判断不出来!
    请教各位,原因何在????如何解决?????

解决方案 »

  1.   

    Private Sub CommandButton1_Click()
       Dim wdShapeCenter
       Dim wdWrapTight
       Dim wdColorGray20
       Dim wdColorRed
       Dim wdLineSpace1pt5
       Dim wdAlignParagraphCenter
       
       wdShapeCenter = -999995
       wdWrapTight = 1
       wdColorGray20 = 13421772
       wdColorRed = 255
       wdLineSpace1pt5 = 1
       wdAlignParagraphCenter = 1
    Dim wrd
    Dim score
        Set wrd = Application    On Error Resume Next
        score = 0#    Set wordDocument = ThisDocument
        If Err.Number <> 0 Then
           TextBox2.Text = TextBox2.Text & "没有打开 " & FileName & " ,无法进行判分操作"
            Err.Clear
           '' Document.SetExamResult 1.1, score
            Exit Sub
        End If
        para_count = wordDocument.Paragraphs.Count    ''''段落查找
        i_title = 0
        p1 = 0
        p2 = 0
        p3 = 0
        p4 = 0
        myText1 = "在本月初于拉斯维加斯举行的计算机商业博览会上"
        myText2 = "但对于一些分析人士而言,"
        myText3 = "一位发言人表示,去年首次推出这种产品"
        myText4 = "电脑是为了将笔记本电脑和PDA"    para_count = wordDocument.Paragraphs.Count
        For i = 1 To para_count
            If Len(wordDocument.Paragraphs(i).Range.Text) > 4 Then
                If i_title = 0 Then
                    i_title = i
                End If
            End If        If InStr(wordDocument.Paragraphs(i).Range.Text, myText1) <> 0 Then
                p1 = i
            ElseIf InStr(wordDocument.Paragraphs(i).Range.Text, myText2) <> 0 Then
                p2 = i
            ElseIf InStr(wordDocument.Paragraphs(i).Range.Text, myText3) <> 0 Then
                p3 = i
            ElseIf InStr(wordDocument.Paragraphs(i).Range.Text, myText4) <> 0 Then
                p4 = i
            End If
        Next
    'step1: 添加标题 (居中对齐)
        flag1 = False    If i_title > 0 And i_title < p1 Then
            With wordDocument.Paragraphs(i_title).Range
                If .Text = "写字板电脑" & vbCr _
                And .ParagraphFormat.Alignment = wdAlignParagraphCenter Then
                    If Err.Number <> 0 Then
                        Err.Clear
                    Else
                        flag1 = True
                    End If
                End If
            End With
        End If
      

  2.   

    step_No = 1     '第几步    If flag1 = True Then
            score = score + 3
           TextBox2.Text = TextBox2.Text & "添加标题正确"
           '' Document.SetStepValue step_No, 2, True, 3, "添加标题正确"
        Else
           TextBox2.Text = TextBox2.Text & "添加标题错误"
           '' Document.SetStepValue step_No, 2, False, 3, "添加标题错误"
        End If'step2: 标题格式设置 (黑体,三号, 红色)
        flag2 = False
        If flag1 = True Then
            With wordDocument.Paragraphs(i_title).Range
                If InStr(.Font.NameFarEast, "黑体") <> 0 And .Font.Size = 16 _
                And .Font.Color = wdColorRed Then
                    If Err.Number <> 0 Then
                        Err.Clear
                    Else
                        flag2 = True
                    End If
                End If
            End With
        End If
    ' [_描述信息]
        step_No = 2     '第几步    If flag2 = True Then
            score = score + 3
           TextBox2.Text = TextBox2.Text & "标题格式设置正确"
           '' Document.SetStepValue step_No, 2, True, 3, "标题格式设置正确"
        Else
           TextBox2.Text = TextBox2.Text & "标题格式设置错误"
           '' Document.SetStepValue step_No, 2, False, 3, "标题格式设置错误"
        End If'step3: 段落行距设置 (1.5倍行距)
        flag3 = False
        If para_count > 1 Then
            flag3 = True
        End If
        For i = i_title + 1 To para_count
            With wordDocument.Paragraphs(i)
                If Len(.Range.Text) > 1 Then
                    If .LineSpacingRule <> wdLineSpace1pt5 Then
                            If Err.Number <> 0 Then
                                Err.Clear
                            End If
                            flag3 = False
                            Exit For
                    End If
                End If
            End With
        Next
    ' [_描述信息]
        step_No = 3     '第几步    If flag3 = True Then
            score = score + 3
           TextBox2.Text = TextBox2.Text & "段落行距设置正确"
           '' Document.SetStepValue step_No, 2, True, 3, "段落行距设置正确"
        Else
           TextBox2.Text = TextBox2.Text & "段落行距设置错误"
           '' Document.SetStepValue step_No, 2, False, 3, "段落行距设置错误"
        End If'step4: 第二段分栏 (二栏,栏宽相等,间距2字符)
    ' [段落分栏]
        flag4 = False
        If p2 > 0 Then
            With wordDocument.Paragraphs(p2).Range.PageSetup
                Spacing = 10.5 * 2 '10.5为1个字符的宽度
                If .TextColumns.Count = 2 _
                And .TextColumns(1).Width = .TextColumns(2).Width _
                And Abs(.TextColumns(1).SpaceAfter - Spacing) < 1 Then
                    If Err.Number <> 0 Then
                        Err.Clear
                    Else
                        flag4 = True
                        End If
                End If
            End With
        End If
    '[_描述信息]
        step_No = 4     '第几步    If flag4 = True Then
            score = score + 3
           TextBox2.Text = TextBox2.Text & "第二段分栏正确"
           '' Document.SetStepValue step_No, 2, True, 3, "第二段分栏正确"
        Else
           TextBox2.Text = TextBox2.Text & "第二段分栏错误"
           '' Document.SetStepValue step_No, 2, False, 3, "第二段分栏错误"
        End If'step5: 将最后两段合并
        flag5 = False
        If p4 > 0 Then
            If InStr(wordDocument.Paragraphs(p4).Range.Text, "虽然这种电脑的问世可能不会导致笔记本电脑的消亡") <> 0 Then
                If Err.Number <> 0 Then
                    Err.Clear
                Else
                    flag5 = True
                End If
            End If
        End If
    ' [_描述信息]
        step_No = 5 '第几步    If flag5 = True Then
            score = score + 3
           TextBox2.Text = TextBox2.Text & "将最后两段合并正确"
           '' Document.SetStepValue step_No, 2, True, 3, "将最后两段合并正确"
        Else
           TextBox2.Text = TextBox2.Text & "将最后两段合并错误"
           '' Document.SetStepValue step_No, 2, False, 3, "将最后两段合并错误"
        End If'step6: 查找替换
        flag6 = False
        '[查找替换]
        With wordDocument.Content.Find
            If Err.Number <> 0 Then
                Err.Clear
            Else
                .MatchCase = True
                .Wrap = 0
                .Text = "微软"
                If .Execute = False Then
                    flag6 = True
                End If
                If flag6 = True _
                And wordDocument.Content.Find.Execute("MicroSoft") = True Then
                    Count = 0
                    .Text = "MicroSoft"
                    .Forward = True
                    Do While .Execute = True
                        Count = Count + 1
                    Loop
                    If Count = 7 Then
                        flag6 = True
                    Else
                        flag6 = False
                    End If
                Else
                    flag6 = False
                End If
            End If
        End With'[_描述信息]
        step_No = 6 '第几步    If flag6 = True Then
            score = score + 3
           TextBox2.Text = TextBox2.Text & "查找替换正确"
           '' Document.SetStepValue step_No, 2, True, 3, "查找替换正确"
        Else
           TextBox2.Text = TextBox2.Text & "查找替换错误"
           '' Document.SetStepValue step_No, 2, False, 3, "查找替换错误"
        End If'step7: 第一段底纹设置 (20%-灰)
        flag7 = False
        If p1 > 0 Then
            If wordDocument.Paragraphs(p1).Range.Shading.BackgroundPatternColor = wdColorGray20 Then
                If Err.Number <> 0 Then
                    Err.Number = 0
                Else
                    flag7 = True
                End If
            End If
        End If
    '[_描述信息]
        step_No = 7 '第几步    If flag7 = True Then
            score = score + 3
           TextBox2.Text = TextBox2.Text & "第一段底纹设置正确"
           '' Document.SetStepValue step_No, 2, True, 3, "第一段底纹设置正确"
        Else
           TextBox2.Text = TextBox2.Text & "第一段底纹设置错误"
           '' Document.SetStepValue step_No, 2, False, 3, "第一段底纹设置错误"
        End If'step8: 插入图片 (紧密型,居中对齐)
        flag8 = False
        If wordDocument.Shapes.Count = 1 And p2 > 0 And p3 > 0 Then
            Set myRange = wordDocument.Range(wordDocument.Paragraphs(p2).Range.Start, wordDocument.Paragraphs(p3).Range.End)
            With wordDocument.Shapes(1)
                If .Anchor.InRange(myRange) _
                And .WrapFormat.Type = wdWrapTight _
                And .Left = wdShapeCenter Then
                    If Err.Number <> 0 Then
                        Err.Number = 0
                    Else
                        flag8 = True
                    End If
                End If
            End With
        End If
    '[_描述信息]
        step_No = 8 '第几步    If flag8 = True Then
            score = score + 3
           TextBox2.Text = TextBox2.Text & "插入图片正确"
           '' Document.SetStepValue step_No, 2, True, 3, "插入图片正确"
        Else
           TextBox2.Text = TextBox2.Text & "插入图片错误"
           '' Document.SetStepValue step_No, 2, False, 3, "插入图片错误"
        End If
    '[设置分数]
       '' Document.SetExamResult 1.1, score   '设置分数
       TextBox1.Text = score
    End Sub
      

  3.   

    以上代码在Word中能全部正确判断可以得24分,而在Activex中调用只有"添加标题正确"和"设置段落行间距正确"其余都不能正确判断,只能得5分.另外一组代码在Word中可以得24分,而在Activex中只能得15分,同样有一部分代码不能正确判断!
      

  4.   

    err.description看看是什么错误
    好久没有做vb咯~~接分