Word Userform在第一个文档中打开/卸载后,在第二个当前活动的文档中无法打开

时间:2016-04-05 17:10:20

标签: vba ms-word word-vba userform

标题真的说明了一切,但这就是我的情况:我设置了一个用户表单来收集用户输入,然后在宏中使用该输入并执行它。这本身就像我想要的那样。打开多个文档时会出现问题。

举例说明:我有两份文件,' doc a'和' doc b'。我打开这两个文档,然后选择' doc a',使用show userform宏打开用户窗体,输入我的数据,然后点击“好”'或者'取消' (两者都设置为在单击后卸载用户窗体)。宏运行,然后我选择' doc b'做同样的事。但是,这一次,当我运行我的'show userform'宏观,' doc a'选中并在那里打开用户窗体。

这似乎是一个非常基本的问题,但我还没有找到任何修复方法。把'卸下我'后#&无法在我的按钮单击子项中工作,我尝试创建一个卸载宏并从这些子系统中调用它,但两者都不适合我。有什么想法吗? (另外,虽然我已经在这里 - 有没有任何好的技巧可以使用最近填充的数据自动填充Userform?不是在打开/关闭单词之间,我已经看到了一些解决方案,但只是在单词中是开放的,我在活动文件之间切换)

Option Explicit
Option Compare Text

Private Sub UserForm_Initialize()
Folder_Name = ""
Tag_Name = ""
Checklist.Value = True
Site_Report.Value = False
Space_Check.Value = False
End Sub

Public Sub Okay_Click()
folder = Folder_Name.Text
tag = Tag_Name.Text
tagtxt = Tag_Name.Text & "[0-9]{1,}"
tagnum = Len(Tag_Name.Text)
If Checklist.Value = True Then
    report_type = "cl"
Else
    report_type = "sr"
End If
If Space_Check.Value = True Then
    space = "yes"
Else
    space = "no"
End If    
If Len(Folder_Name.Text) > 0 Then    
Application.Run "Mass_Hyperlink_v_5_0"
Application.Run "UnloadIt"    
Else   
Application.Run "UnloadIt"   
End If   
Unload Me
End Sub

Private Sub Cancel_Click()
Application.Run "UnloadIt"
Unload Me 
End Sub

我不认为问题出在这个用户形式使用的宏上(它本身运行良好,虽然代码可能有点陈词滥调),但这里的代码是好的衡量标准:

Option Explicit
Option Compare Text

Public tag As String
Public tagtxt As String
Public tagnum As String
Public folder As String
Public space As String
Public report_type As String

Public Sub Mass_Hyperlink_v_5_0()

Dim fileName As String
Dim filePath As String
Dim rng As Word.Range
Dim rng2 As Word.Range
Dim fileType As String
Dim start As String
Dim temp As String

Application.ScreenUpdating = False

fileType = "jpg"

If space = "Yes" Then
    start = "%20("
    Else: start = "("
End If

If report_type = "cl" Then
    folder = "..\Images\" & folder
    Set rng = ActiveDocument.Range
    Else: folder = folder
End If

If report_type = "sr" Then
    folder = "Images\" & folder
    Set rng = Selection.Range
    Else: folder = folder
End If

    Set rng2 = rng.Duplicate

'tagtxt = tag & "[0-9]{1,}"

If Len(rng) > 0 And report_type = "sr" Then
    With rng.Find
        .Text = tagtxt
        .Forward = False
        .MatchWildcards = True
        .Wrap = wdFindStop
            Do While .Execute(findText:=tagtxt) = True
            If rng.InRange(rng2) Then
                rng.Select
                'Selection.start = Selection.start + Len(tag)
                Selection.start = Selection.start + tagnum
                'ActiveDocument.Range(Selection.start - Len(tag), Selection.start).Delete
                ActiveDocument.Range(Selection.start - tagnum, Selection.start).Delete
                fileName = Selection.Text
                filePath = folder & "\" & Hyperlinker.Tag_Name.Text & start & fileName & ")" & "." & fileType
                ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, address:= _
                filePath, SubAddress:="", ScreenTip:="", TextToDisplay:= _
                (Hyperlinker.Tag_Name.Text & Selection.Text)
            Else
                Exit Sub
            End If
            rng.Collapse wdCollapseStart
        Loop
    End With
End If
If report_type = "cl" Then
    With rng.Find
        .Text = tagtxt
        .Forward = False
        .MatchWildcards = True
        .Wrap = wdFindStop
            Do While .Execute(findText:=tagtxt) = True
            If rng.InRange(rng2) Then
                rng.Select
                'Selection.start = Selection.start + Len(tag)
                Selection.start = Selection.start + tagnum
                'ActiveDocument.Range(Selection.start - Len(tag), Selection.start).Delete
                ActiveDocument.Range(Selection.start - tagnum, Selection.start).Delete
                fileName = Selection.Text
                filePath = folder & "\" & Hyperlinker.Tag_Name.Text & start & fileName & ")" & "." & fileType
                ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, address:= _
                filePath, SubAddress:="", ScreenTip:="", TextToDisplay:= _
                (Hyperlinker.Tag_Name.Text & Selection.Text)
            Else
                Exit Sub
            End If
            rng.Collapse wdCollapseStart
        Loop
    End With
 End If
Application.ScreenUpdating = True
End Sub

Sub Show_Linker()
Hyperlinker.Show
Hyperlinker.Folder_Name.SetFocus
End Sub

Sub UnloadIt()
Unload Hyperlinker
End Sub

1 个答案:

答案 0 :(得分:1)

在VBA中使用UserForms可能很棘手,因为它们实际上是一种类。由于VBA试图使一切变得异常简单,因此类不明显,也不是如何正确使用它们。有些情况下,他们成为粗心大意的陷阱。

因此,VBA使您可以使用UserForm类的实例,而无需声明和实例化新对象,这通常是类对象的情况。结果是对象可能“徘徊”并导致意外行为,例如您正在看到的。

使用UserForm的更正确的方法可能看起来更多的工作(代码到类型和复杂性),但它有助于保持事物的排序。实际上,这种方法理论上允许您为各种文档使用单独的UserForm。

Dim frmHyperlinker as Hyperlinker
Set frmHyperlinker = New Hyperlinker
frmHyperlinker.Folder_Name.SetFocus
frmHyperlinker.Show
'Execution waits...
'Now you're done with it, so clean up
Unload frmHyperlinker
Set frmHyperlinker = Nothing

本讨论中的答案涉及更多技术细节,尽管该问题的主题与您的不同:Add Public Methods to a Userform Module in VBA