+ Reply to Thread
Results 1 to 4 of 4

Excel Workbook is in Use. Please try again later

  1. #1
    Registered User
    Join Date
    08-29-2017
    Location
    Houston, USA
    MS-Off Ver
    11
    Posts
    1

    Excel Workbook is in Use. Please try again later

    Hello,

    Following marco is conducted in Microsoft Word. VBA has three modules.

    Module 1 opens windows explorer browser and prompts the user to select an excel file which is later used in Module 2. See code below.

    Sub openDialog()
    Dim fd As Office.FileDialog

    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    With fd

    .AllowMultiSelect = False

    ' Set the title of the dialog box.
    .Title = "Please select the file."

    ' Clear out the current filters, and add our own.
    .Filters.Clear
    .Filters.Add "All Files", "*.*"

    ' Show the dialog box. If the .Show method returns True, the
    ' user picked at least one file. If the .Show method returns
    ' False, the user clicked Cancel.
    If .Show = True Then
    txtFileName = .SelectedItems(1) 'replace txtFileName with your textbox

    End If
    End With
    End Sub


    Module 2 uses the file selected and performs a bulk find and replace function. This macro replaces some predefined words from a *.doc document with values of a cell from an excel-sheet. See Code below

    Sub BulkFindReplace()
    Application.ScreenUpdating = True
    Dim xlApp As Object, xlWkBk As Object, StrWkBkNm As String, StrWkSht As String
    Dim bStrt As Boolean, iDataRow As Long, bFound As Boolean
    Dim xlFList As String, xlRList As String, i As Long, Rslt
    StrWkBkNm = txtFileName
    StrWkSht = "Protocol" 'or type Sheet1
    If Dir(StrWkBkNm) = "" Then
    MsgBox "Cannot find the designated workbook: " & StrWkBkNm, vbExclamation
    Exit Sub
    End If
    ' Test whether Excel is already running.
    On Error Resume Next
    bStrt = False ' Flag to record if we start Excel, so we can close it later.
    Set xlApp = GetObject(, "Excel.Application")
    'Start Excel if it isn't running
    If xlApp Is Nothing Then
    Set xlApp = CreateObject("Excel.Application")
    If xlApp Is Nothing Then
    MsgBox "Can't start Excel.", vbExclamation
    Exit Sub
    End If
    ' Record that we've started Excel.
    bStrt = True
    End If
    On Error GoTo 0
    'Check if the workbook is open.
    bFound = False
    With xlApp
    'Hide our Excel session
    If bStrt = True Then .Visible = False
    For Each xlWkBk In .Workbooks
    If xlWkBk.FullName = StrWkBkNm Then ' It's open
    Set xlWkBk = xlWkBk
    bFound = True
    Exit For
    End If
    Next
    ' If not open by the current user.
    If bFound = False Then
    ' Check if another user has it open.
    If IsFileLocked(StrWkBkNm) = True Then
    ' Report and exit if true
    MsgBox "The Excel workbook is in use." & vbCr & "Please try again later.", vbExclamation, "File in use"
    If bStrt = True Then .Quit
    Exit Sub
    End If
    ' The file is available, so open it.
    Set xlWkBk = .Workbooks.Open(FileName:=StrWkBkNm)
    If xlWkBk Is Nothing Then
    MsgBox "Cannot open:" & vbCr & StrWkBkNm, vbExclamation
    If bStrt = True Then .Quit
    Exit Sub
    End If
    End If
    ' Process the workbook.
    With xlWkBk.Worksheets(StrWkSht)
    ' Find the last-used row in column A.
    ' Add 1 to get the next row for data-entry.
    iDataRow = .Cells(.Rows.Count, 1).End(-4162).Row ' -4162 = xlUp
    ' Output the captured data.
    For i = 1 To iDataRow
    ' Skip over empty fields to preserve the underlying cell contents.
    If Trim(.Range("A" & i)) <> vbNullString Then
    xlFList = xlFList & "|" & Trim(.Range("A" & i))
    xlRList = xlRList & "|" & Trim(.Range("B" & i))
    End If
    Next
    End With
    If bFound = False Then xlWkBk.Close False
    If bStrt = True Then .Quit
    End With
    ' Release Excel object memory
    Set xlWkBk = Nothing: Set xlApp = Nothing
    'Process each word from the F/R List
    For i = 1 To UBound(Split(xlFList, "|"))
    With ActiveDocument.Range
    With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .MatchWholeWord = True
    .MatchCase = False 'or True
    .Wrap = wdFindContinue
    .Text = Split(xlFList, "|")(i)
    '.Execute
    'To automatically change the found text:
    '• comment-out/delete the previous line and the Do While Loop
    '• uncomment the next two lines
    .Replacement.Text = Split(xlRList, "|")(i)
    .Execute Replace:=wdReplaceAll
    End With
    'Ask the user whether to change the found text
    'Do While .Find.Found
    '.Duplicate.Select
    'Rslt = MsgBox("Replace this instance of:" & vbCr & _
    'Split(xlFList, "|")(i) & vbCr & "with:" & vbCr & _
    'Split(xlRList, "|")(i), vbYesNoCancel)
    'If Rslt = vbCancel Then Exit Sub
    'If Rslt = vbYes Then .Text = Split(xlRList, "|")(i)
    '.Collapse wdCollapseEnd
    '.Find.Execute
    'Loop
    End With
    Next
    Application.ScreenUpdating = True
    End Sub

    Function IsFileLocked(strFileName As String) As Boolean
    On Error Resume Next
    Open strFileName For Binary Access Read Write Lock Read Write As #1
    Close #1
    IsFileLocked = Err.Number
    Err.Clear
    End Function

    Module 3 calls Module 1 then Module 2. See code below.
    Sub totalCall()

    Call openDialog

    Call BulkFindReplace

    End Sub


    After running Module 3, I get the following error message: "Excel Workbook is in Use. Please try again later." I made sure that the excel workbook is closed and also tried restarting the computer.


    Could someone please help me with this? Thank you.

  2. #2
    Valued Forum Contributor
    Join Date
    03-16-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    371

    Re: Excel Workbook is in Use. Please try again later

    Please Login or Register  to view this content.
    This is triggering the error message. Can you post the code for UDF IsFileLocked?
    Design everything to be as simple as possible, but no simpler.

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,410

    Re: Excel Workbook is in Use. Please try again later

    Sat 63 - welcome to the forum!

    Unfortunately your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Excel Workbook is in Use. Please try again later

    Cross-posted at: http://www.vbaexpress.com/forum/show...ce-using-Excel

    Not only that, nowhere have you acknowledged where you got the Module 2 code (which I wrote)!

    Please read Excel Forum's Cross-Posting policy in rule 8: http://www.excelforum.com/forum-rule...rum-rules.html
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA code to update query from a password protected excel workbook and then close workbook
    By MrChipper in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-14-2017, 07:41 AM
  2. Replies: 1
    Last Post: 08-14-2015, 04:53 AM
  3. [SOLVED] Save the split the workbook file type as Excel Binary Workbook From Run Macro
    By breadwinner in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-25-2013, 05:09 AM
  4. [SOLVED] Copying data from a closed workbook into an open workbook ignoring excel filter?
    By reach78 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 09-17-2013, 12:31 AM
  5. Help required to copy data from one Excel Workbook to different Excel's workbook
    By vishalmchavan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-05-2013, 07:07 AM
  6. Copy Excel workbook with multiple sheets into a new Excel workbook on one sheet
    By madmancpa2000 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 03-04-2013, 04:28 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1