字VB - 错误'5941'和for-loop [英] Word VB - error '5941' and for-loop

查看:363
本文介绍了字VB - 错误'5941'和for-loop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题。

首先,我有错误运行时错误5941收集的请求成员不存在。在错误之后选择结束时,代码将实际执行所期望的操作。我以为我在错误5941 ,但它没有帮助。他们建议在代码中添加Application.Templates.LoadBuildingBlocks。第二,我试图将textboxCounter添加到Set TBs( 0 )= UserForm1.Controls(TextBox_ 1 ):所以我不需要声明文本框1 - 10,所以我可以有我需要的测试框。我试过把它放进去,但是这不起作用。不知道我在做什么错。

 选项显式


Private Sub AddLine_Click )

Application.Templates.LoadBuildingBlocks

Dim theTextbox As Object
Dim textboxCounter As Long

For textboxCounter = 1 To Amount
Set theTextbox = UserForm1.Controls.Add(Forms.TextBox.1,Test& textboxCounter,True)
使用文本框
.Name =TextBox_& textboxCounter
.Width = 200
.Left = 70
.Top = 30 * textboxCounter
End With


下一个

Dim theLabel As Object
Dim labelCounter As Long

For labelCounter = 1 To Amount
Set theLabel = UserForm1.Controls.Add(Forms.Label.1 ,Test& labelCounter,True)
With theLabel
.Caption =Image& labelCounter
.Left = 20
.Width = 50
.Top = 30 * labelCounter
End With

With UserForm1
.Height =金额* 30 + 100


结尾用CommandButton1
.Top =金额* 30 + 40


用CommandButton2
.Top =金额* 30 + 40
结束于


下一个


结束Sub

Sub CommandButton1_Click()

Application.Templates.LoadBuildingBlocks


Dim Textbox1 As Object
Dim Textbox2 As Object
Dim Textbox3 As Object
Dim Textbox4 As Object
Dim Textbox5 As Object
Dim Textbox6 As Object
Dim Textbox7 As Object
Dim Textbox8 As Object
Dim Textbox9 As Object
Dim Textbox10 As Object
$ b $ Dim i

Dim TBs(9)As Object
Set TBs(0)= UserForm1.Controls(TextBox_1) :Set TBs(1)= UserForm1.Controls(TextBox_2):Set TBs(2)= UserForm1.Controls(Text (TextBox_4):Set TBs(4)= UserForm1.Controls(TextBox_5):Set TBs(5)= UserForm1.Controls(TextBox_6 )
Set TBs(6)= UserForm1.Controls(TextBox_7):Set TBs(7)= UserForm1.Controls(TextBox_8):Set TBs(8)= UserForm1.Controls(TextBox_9)
Set TBs(9)= UserForm1.Controls(TextBox_10):

For i = 0 To Amount
With ActiveDocument
如果.Bookmarks(href& amp ; i + 1).Range =.jpg然后
.Bookmarks(href& i + 1).Range _
.InsertBefore TBs(i)
.Bookmarks(src & i + 1).Range _
.InsertBefore TBs(i)
.Bookmarks(alt& i + 1).Range _
.InsertBefore TBs(i)
End If
End With
Next

UserForm1.Hide

Selection.Find.ClearFormatting
Selection.Find.Replacement。 ClearFormatting

With Selection.Find
.Text =.jpg
.Replacement.Text =.jpg

.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

结尾Selection.HomeKey单位:= wdLine
Selection.Find.Execute替换:= wdReplaceAll



Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

With Selection.Find
.Text =/
.Replacement.Text =/

.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

结尾Selection.HomeKey单位:= wdLine
Selection.Find.Execute替换:= wdReplaceAll



Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

With Selection.Find
.Text = .jpg.jpg
.Replacement.Text =.jpg

.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

结尾Selection.HomeKey单位:= wdLine
Selection.Find.Execute替换:= wdReplaceAll

End Sub


解决方案

我做错了。我在设置零开始的文本框时忘了。我在我的For语句中添加了一个金额 - 1,现在可以正常工作了。它正在我的文档上搜索另一个不存在的字段。


I have two questions.

First, I have the error "Run-time error '5941' The requested member of the collection does not exist." When selecting "End" after the error the code will actually do what's intended. I thought I found a solution at Error 5941 but it doesn't help. They suggested adding "Application.Templates.LoadBuildingBlocks" to the code.

Second, I am attempting to add "textboxCounter" to "Set TBs(0) = UserForm1.Controls("TextBox_1"):" So I don't need to declare textboxes 1 - 10 so I can have as many test boxes I need. I've tried just placing it in but that is not working. Not sure what I am doing wrong.

Option Explicit    


Private Sub AddLine_Click()

Application.Templates.LoadBuildingBlocks

Dim theTextbox As Object
Dim textboxCounter As Long

For textboxCounter = 1 To Amount
    Set theTextbox = UserForm1.Controls.Add("Forms.TextBox.1", "Test" & textboxCounter, True)
    With theTextbox
        .Name = "TextBox_" & textboxCounter
        .Width = 200
        .Left = 70
        .Top = 30 * textboxCounter
    End With


Next

Dim theLabel As Object
Dim labelCounter As Long

For labelCounter = 1 To Amount
    Set theLabel = UserForm1.Controls.Add("Forms.Label.1", "Test" & labelCounter, True)
    With theLabel
        .Caption = "Image" & labelCounter
        .Left = 20
        .Width = 50
        .Top = 30 * labelCounter
    End With

    With UserForm1
        .Height = Amount * 30 + 100
    End With

    With CommandButton1
        .Top = Amount * 30 + 40
    End With

    With CommandButton2
        .Top = Amount * 30 + 40
    End With


Next


End Sub

Sub CommandButton1_Click()

Application.Templates.LoadBuildingBlocks


Dim Textbox1 As Object
Dim Textbox2 As Object
Dim Textbox3 As Object
Dim Textbox4 As Object
Dim Textbox5 As Object
Dim Textbox6 As Object
Dim Textbox7 As Object
Dim Textbox8 As Object
Dim Textbox9 As Object
Dim Textbox10 As Object

Dim i

Dim TBs(9) As Object
Set TBs(0) = UserForm1.Controls("TextBox_1"): Set TBs(1) = UserForm1.Controls("TextBox_2"): Set TBs(2) = UserForm1.Controls("TextBox_3")
Set TBs(3) = UserForm1.Controls("TextBox_4"): Set TBs(4) = UserForm1.Controls("TextBox_5"): Set TBs(5) = UserForm1.Controls("TextBox_6")
Set TBs(6) = UserForm1.Controls("TextBox_7"): Set TBs(7) = UserForm1.Controls("TextBox_8"): Set TBs(8) = UserForm1.Controls("TextBox_9")
Set TBs(9) = UserForm1.Controls("TextBox_10"):

For i = 0 To Amount
    With ActiveDocument
        If .Bookmarks("href" & i + 1).Range = ".jpg" Then
            .Bookmarks("href" & i + 1).Range _
            .InsertBefore TBs(i)
            .Bookmarks("src" & i + 1).Range _
            .InsertBefore TBs(i)
            .Bookmarks("alt" & i + 1).Range _
            .InsertBefore TBs(i)
        End If
    End With
Next

UserForm1.Hide

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting

    With Selection.Find
        .Text = ".jpg "
        .Replacement.Text = ".jpg"

        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.HomeKey Unit:=wdLine
    Selection.Find.Execute Replace:=wdReplaceAll



        Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting

    With Selection.Find
        .Text = "/ "
        .Replacement.Text = "/"

        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.HomeKey Unit:=wdLine
    Selection.Find.Execute Replace:=wdReplaceAll



    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting

    With Selection.Find
        .Text = ".jpg.jpg"
        .Replacement.Text = ".jpg"

        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.HomeKey Unit:=wdLine
    Selection.Find.Execute Replace:=wdReplaceAll

End Sub

解决方案

I have discovered what I was doing wrong. I forgot when setting the textboxes they started at Zero. I put an Amount - 1 in my For i statement and it is now working without error. It was searching for another field on my document that didn't exist.

这篇关于字VB - 错误'5941'和for-loop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆