Results 1 to 4 of 4
  1. #1
    Star Lounger
    Join Date
    Sep 2007
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post

    Red face Word 2003: Runtime error when closing document using an "on entry" event

    I am having issues with what should be a relatively simple macro, but I consistently get a Run Time error 4198 when trying to close a document. The macro works in debug mode, but not in runtime, so it has me somewhat perplexed.

    Just some background -- we have a lot of forms that have to be filled out in this office (law office), and I create generic forms and then use macros to speed up the process a bit. I found it simplest to just create a document that consists of a word table that contains all the basic information for each lawyer and their assistants and many different macros access that information. I am creating a pretty standard form, and it requires some lawyer information, so I am trying to create a macro that simply asks the user for the lawyer's initials and then opens the document with the table and copies the information. That's all fine, but when it is time to close that document, I get a runtime error. Again this is a locked form using the "on entry" event to run this macro - I'm not sure if that is part of the problem?

    If anyone could offer some suggestions, that would be great.

    Here is the code I have:
    Sub ElectronicBankingWithdrawal()
    'This macro goes with the Electronic Banking Withdrawal form located with the Accounting forms
    'Section A of the Electronic Banking Withdrawal form

    Dim strDocName As String
    Dim strLawyer As String
    Dim txtLawyer As String
    Dim txtEmail As String
    Dim strLawyerInit As String
    Dim txtLawyerInit As String
    Dim txtSenderDegree As String
    strLawyerInit = InputBox("Please enter the Responsible Lawyer's Initials", "Responsible Lawyer S.A. 2")

    If strLawyerInit = "" Then
    GoTo Bye
    End If
    strDocName = ActiveDocument
    On Error GoTo 0
    'open the lawyer list
    Documents.Open FileName:=STRMACROFILES & "MRLawyerNames.doc", ConfirmConversions:=False, ReadOnly:=True, addtorecentfiles:=False
    Application.ScreenUpdating = False
    'set lawyer initials to lowercase
    txtLawyerInit = LCase(strLawyerInit)
    Documents("MRLawyerNames.doc").Activate
    'Find the lawyer initials
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = UCase(txtLawyerInit)
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    'if not found, offer options
    If Selection.Find.Found = False Then
    MsgBox ("The lawyer initials you entered were not found. Please try again.")
    Documents("MRLawyerNames.doc").Close savechanges:=wdDoNotSaveChanges
    Documents(strDocName).Activate
    Exit Sub
    End If
    'store the lawyer's info in variables
    With Selection
    .MoveRight unit:=wdCell
    txtLawyer = .Text
    .MoveRight unit:=wdCell
    txtSenderDegree = .Text
    .MoveRight unit:=wdCell
    .MoveRight unit:=wdCell
    .MoveRight unit:=wdCell
    txtEmail = .Text
    .MoveRight unit:=wdCell
    End With
    Documents(strDocName).Activate
    With ActiveDocument
    .FormFields("Lawyer").Result = txtLawyer
    If Len(txtSenderDegree) > 2 Then
    .FormFields("Degree").Result = ", " & txtSenderDegree
    End If
    .FormFields("email").Result = txtEmail
    .FormFields("check1").Select
    End With
    Documents("MRLawyernames.doc").Close savechanges:=wdDoNotSaveChanges
    Documents(strDocName).Activate
    Bye:
    Exit Sub
    Oops:
    ErrorMsg = MsgBox(ERRMSG1 & Chr(34) & Err.Number & " - " & Err.Description & Chr(34) & vbCr & vbCr & ERRMSG2, vbOKOnly + vbCritical)
    Exit Sub
    End Sub

    Thank you so much.

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi Maverick,

    The the following. In addition to some streamlining, I've added a bit more error checking and send all runtime errors now sent to your 'Oops' line. I suspect the problem may have related to ambiguity about what was selected/active or to a second attempt to close the 'MRLawyerNames.doc' document if the initials hadn't been found. Note that I've assumed that your txtLawyer, txtSenderDegree & txtEmail strings are in columns 2, 3 & 6, respectively, of the lookup table. If they're not, you'll need to change the cell references. Note also how I've collected those references; this technique avoids picking up the end-of-cell markers.
    Code:
    Sub ElectronicBankingWithdrawal()
    Application.ScreenUpdating = False
    'This macro goes with the Electronic Banking Withdrawal form located with the Accounting forms
    'Section A of the Electronic Banking Withdrawal form
    Dim strInit As String, DocTmp As Document, DocNow As Document, Rng As Range
    Dim txtLawyer As String, txtSenderDegree As String, txtEmail As String
    'get lawyer initials in uppercase
    strInit = Trim(UCase(InputBox("Please enter the Responsible Lawyer's Initials", "Responsible Lawyer S.A. 2")))
    If strInit = "" Then GoTo Bye
    Set DocNow = ActiveDocument
    On Error GoTo Oops
    'open the lawyer list
    Set DocTmp = Documents.Open(FileName:=STRMACROFILES & "MRLawyerNames.doc", _
          ConfirmConversions:=False, ReadOnly:=True, AddToRecentFiles:=False)
    'Find the lawyer initials
    With DocTmp.Content
      With .Find
        .ClearFormatting
        .Text = strInit
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = True
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      'if not found, offer options
      If .Find.Found = True And .Duplicate.Information(wdWithInTable) = True Then
      'store the lawyer's info in variables
        With .Duplicate.Rows(1)
          Set Rng = .Cells(2).Range
          Rng.End = Rng.End - 1
          txtLawyer = Trim(Rng.Text)
          Set Rng = .Cells(3).Range
          Rng.End = Rng.End - 1
          txtSenderDegree = Trim(Rng.Text)
          Set Rng = .Cells(6).Range
          Rng.End = Rng.End - 1
          txtEmail = Trim(Rng.Text)
        End With
        DocTmp.Close SaveChanges:=wdDoNotSaveChanges
      Else
        MsgBox ("The lawyer initials you entered were not found. Please try again.")
        DocTmp.Close SaveChanges:=wdDoNotSaveChanges
        GoTo Bye
      End If
    End With
    With DocNow
      .Activate
      .FormFields("Lawyer").Result = txtLawyer
      If Len(txtSenderDegree) > 2 Then
        .FormFields("Degree").Result = ", " & txtSenderDegree
      End If
      .FormFields("email").Result = txtEmail
      .FormFields("check1").Select
    End With
    GoTo Bye
    Oops:
    ErrorMsg = MsgBox(ERRMSG1 & Chr(34) & Err.Number & " - " & Err.Description & Chr(34) & vbCr & vbCr & ERRMSG2, vbOKOnly + vbCritical)
    Bye:
    Set Rng = Nothing: Set DocTmp = Nothing: Set DocNow = Nothing
    Application.ScreenUpdating = True
    End Sub
    PS: When posting code, please use code tags.
    Last edited by macropod; 2012-05-15 at 01:05.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    Star Lounger
    Join Date
    Sep 2007
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    Hi Macropod,

    Thank you so much for the code. I have never really used ranges before, and therefore I shy away from the code (even if it does cut out the typing considerably). Unfortunately though, I am still experiencing the same problem -- it errors out on the doctmp.close.

    I'm assuming it has something to do with me running this macro through the "on entry" event of a formfield as I have numerous other macros that do something similar and I have no issues with them? Hopefully this issue can be resolved -- any other ideas?

    Thanks so much.

    Michelle

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    There's an article about this here: http://support.microsoft.com/kb/212706, but I'm not sure the technique will work with an on-entry macro.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. The Following User Says Thank You to macropod For This Useful Post:

    maverik (2012-05-17)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •