在第一个文档中打开/卸载后,Word Userform将无法在第二个当前处于活动状态的文档中打开 [英] Word Userform won't open in second, currently active document after opened/unloaded in first document

查看:106
本文介绍了在第一个文档中打开/卸载后,Word Userform将无法在第二个当前处于活动状态的文档中打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题确实说明了一切,但这是我的情况:我设置了一个用户窗体来收集用户输入,然后在宏中使用该输入并执行它.就其本身而言,它的工作方式完全符合我的要求.打开多个文档时出现问题.

The headline really says it all, but here's my situation: I have a userform set up to collect user input, then uses that input in a macro and executes it. That, in itself, works exactly like I want it to. The problem comes when more than one document is open.

为了说明:我有两个文档,"doc a"和"doc b".我打开两个文档,然后选择"doc a",使用show userform宏打开用户窗体,输入我的数据,然后单击确定"或取消"(将两者都设置为在单击时卸载用户窗体).宏运行,然后选择"doc b"执行相同的操作.但是,这一次,当我运行我的"show userform"宏时,选择了"doc a"并在那里打开了用户窗体.

To illustrate: I have two documents, 'doc a' and 'doc b'. I open both documents, then select 'doc a', open the userform using a show userform macro, input my data, and hit either 'Okay' or 'Cancel' (both of which are set to unload the userform once clicked). The macro runs, and then I select 'doc b' to do the same. This time, however, when I run my 'show userform' macro, 'doc a' is selected and the userform is opened there.

这似乎是一个非常基本的问题,但我无法找出任何修复方法.在将卸载我"放置在按钮单击子中失败后,我尝试创建一个卸载宏并从这些子中调用它,但都对我不起作用.有什么想法吗? (此外,虽然我已经在这里了-有什么好技巧可以用最新填充的数据自动填充用户窗体?不是在打开/关闭单词之间,我已经看到了一些解决方案,而是在单词打开的同时,以及我在活动文档之间切换)

This seems like a pretty basic issue, but I haven't been able to figure out any fixes. After putting 'unload me' failed to work in my button-click subs, I tried creating an unload macro and calling it from those subs instead, but neither is working for me. Any thoughts? (Also, while I'm already here- are there any good tricks to autofill the Userform with the most recently filled data? Not between opening/closing word, which I've seen some solutions for, but just while word is open, and I'm switching between active documents)

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

我认为问题不在于此用户窗体使用的宏(它可以很好地运行,尽管代码可能有点笨拙),但是下面的代码可以很好地解决此问题:

I don't think the issue is with the macros that this userform uses (it runs fine on its own, though the code is likely a bit hackneyed), but here's the code for good measure:

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

推荐答案

在VBA中使用UserForms可能很棘手,因为它们实际上是一种Class.由于VBA试图使所有事情变得异常简单,所以类不是显而易见的,也不是如何正确使用它们的.在某些情况下,它们会成为无意识者的陷阱.

Working with UserForms in VBA can be tricky, because they're actually a kind of Class. Since VBA tries to make everything exceptionally simple, classes are not obvious, nor is how to work with them correctly. There are situations where they become traps for the unwary.

因此,VBA使您可以使用UserForm类的实例,而无需像通常使用类对象那样声明和实例化新对象.结果是该对象可以徘徊"并引起意外的行为,如您所见.

So VBA makes it possible for you to work with an instance of a UserForm class without you needing to declare and instantiate a new object, as would normally be the case with a class object. The result being that the object can "hang around" and cause unexpected behavior, such as you're seeing.

使用UserForm的更正确的方法似乎需要做更多的工作(代码的类型和复杂性),但是它有助于使事情保持排序.实际上,从理论上讲,这种方法将使您可以为各种文档使用单独的UserForm.

The more correct way to work with a UserForm may seem like a lot more work (code to type and complexity), but it helps to keep things sorted. Indeed, this approach would theoretically allow you to have a separate UserForm for various documents.

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

尽管该问题的主题与您的问题有所不同,但在此讨论中还有一个答案会涉及更多技术细节:

There's an Answer in this discussion that goes into more technical detail, although the topic of that question is different from yours: Add Public Methods to a Userform Module in VBA

这篇关于在第一个文档中打开/卸载后,Word Userform将无法在第二个当前处于活动状态的文档中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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