Accessing an open Word doc from Excel causes "Locked for Editing" dialog to appear

gravanoc

Active Member
Joined
Oct 20, 2015
Messages
348
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I'm experiencing an issue with exporting the contents of a Word document to PDF. After pasting a table from Outlook into Excel, certain parts of the table are retained, then the new table is pasted into Word. The Word document is saved using a specific filename, the date, and a number to preserve uniqueness. Depending on a True/False condition set by a checkbox, the program will either create a PDF from the Word document or it will stop so the user can edit the table further. In the latter case, a button appears that will finish the remaining steps of exporting the PDF via Word to a specified folder.

If there is no further editing to be done to the Word document, the program runs without incident by creating both the Word doc & PDF, but if the PDF export is done separately, then Excel will appear to lock up. In reality a message appears stating that "XXX.docx is locked for editing", and gives a list of options. I would like to avoid this.

I noticed that the part of my script checking for an existing instance of Word doesn't find it if it is open, so it always defaults to using CreateObject. This seems to be the problem, but it could be something else. I've included both subroutines here, but it's the second one causing the issue.

VBA Code:
Sub CopyToWord(ByVal rngTable As Range)

    Dim wordApp As Object, wordDoc As Object
    Dim path As String, fileName As String
    Dim iteration As Long
    Dim currDate As Date
    
    pathWord = wksProcess.Range("H11").Value
    pathPDF = wksProcess.Range("H12").Value
    currDate = wksProcess.Range("CurrDate").Value
    
    If Day(currDate) < Day(Now) Then
        wksProcess.Range("Iteration") = 1
        wksProcess.Range("CurrDate") = Format(Now, "mm/dd/yyyy")
    End If
    
    currDate = wksProcess.Range("CurrDate").Value
    iteration = wksProcess.Range("Iteration").Value
    fileName = "Export " & Format(currDate, "mm-dd-yyyy") & " - " & iteration
    
    wksProcess.Range("Iteration").Value = iteration + 1

    On Error Resume Next
    Set wordApp = GetObject("Word.Application")
    If Err.Number <> 0 Then
        Set wordApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    
    wordApp.Visible = True
    Set wordDoc = wordApp.Documents.Add
    rngTable.Copy
    
    wordApp.Activate
    wordDoc.Activate
    
    wordApp.Selection.PasteExcelTable True, False, True
    wordDoc.SaveAs2 fileName:=pathWord & Application.PathSeparator & fileName, FileFormat:=wdFormatDocumentDefault
    
    If wksProcess.Range("Conversion") = True Then
        wordDoc.SaveAs2 fileName:=pathPDF & Application.PathSeparator & fileName, FileFormat:=wdFormatPDF
        wordApp.Quit
        If wksProcess.Range("Messages") = False Then MsgBox "Word & PDF files successfully created in the specified folders.", vbOKOnly
    Else
        wksProcess.cmdPDF.Enabled = True
        If wksProcess.Range("Messages") = False Then MsgBox "Word file successfully created in the specified folder.  " _
              & "To export the Word table to PDF please press the Export to PDF button when ready.", vbOKOnly
    End If
    
    Set wordApp = Nothing
    
End Sub

Sub ExportFileToPDF()

    Dim wordApp As Object, wordDoc As Object
    Dim iteration As Long
    Dim pathWord As String, pathPDF As String
    Dim fileName As String

    pathWord = wksProcess.Range("H11").Value
    pathPDF = wksProcess.Range("H12").Value
    
    currDate = wksProcess.Range("CurrDate").Value
    iteration = wksProcess.Range("Iteration") - 1
    fileName = "Export " & Format(currDate, "mm-dd-yyyy") & " - " & iteration
    
    On Error Resume Next
    Set wordApp = GetObject("Word.Application")
    If Err.Number <> 0 Then
        Set wordApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    
    wordApp.Activate
    ' The next line is where it locks up and that dialog with options appears.
    Set wordDoc = wordApp.Documents.Open(pathWord & Application.PathSeparator & fileName & ".docx")
    wordDoc.Activate

    wordDoc.SaveAs2 fileName:=pathPDF & Application.PathSeparator & fileName, FileFormat:=wdFormatPDF
    If wksProcess.Range("Messages") = False Then MsgBox "PDF exported to specified folder.", vbOKOnly
    Set wordApp = Nothing
    
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi gravanoc. U can trial these changes. HTH. Dave
Code:
Sub CopyToWord(ByVal rngTable As Range)

    Dim wordApp As Object, wordDoc As Object
    Dim path As String, fileName As String
    Dim iteration As Long
    Dim currDate As Date
    
    pathWord = wksProcess.Range("H11").Value
    pathPDF = wksProcess.Range("H12").Value
    currDate = wksProcess.Range("CurrDate").Value
    
    If Day(currDate) < Day(Now) Then
        wksProcess.Range("Iteration") = 1
        wksProcess.Range("CurrDate") = Format(Now, "mm/dd/yyyy")
    End If
    
    currDate = wksProcess.Range("CurrDate").Value
    iteration = wksProcess.Range("Iteration").Value
    fileName = "Export " & Format(currDate, "mm-dd-yyyy") & " - " & iteration
    
    wksProcess.Range("Iteration").Value = iteration + 1

    On Error Resume Next
    Set wordApp = GetObject("Word.Application")
    If Err.Number <> 0 Then
        Set wordApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    
    'wordApp.Visible = True
    Set wordDoc = wordApp.Documents.Add
    rngTable.Copy
    
    wordApp.Activate
    wordDoc.Activate
    
    wordApp.Selection.PasteExcelTable True, False, True
    Application.CutCopyMode = False
    wordDoc.SaveAs2 fileName:=pathWord & Application.PathSeparator & fileName, FileFormat:=wdFormatDocumentDefault
    
    If wksProcess.Range("Conversion") = True Then
        wordDoc.SaveAs2 fileName:=pathPDF & Application.PathSeparator & fileName, FileFormat:=wdFormatPDF
        'wordApp.Quit
        If wksProcess.Range("Messages") = False Then MsgBox "Word & PDF files successfully created in the specified folders.", vbOKOnly
    Else
        wksProcess.cmdPDF.Enabled = True
        If wksProcess.Range("Messages") = False Then MsgBox "Word file successfully created in the specified folder.  " _
              & "To export the Word table to PDF please press the Export to PDF button when ready.", vbOKOnly
    End If
    Set wordDoc = Nothing
    wordApp.Quit
    Set wordApp = Nothing
    
End Sub

Sub ExportFileToPDF()

    Dim wordApp As Object, wordDoc As Object
    Dim iteration As Long
    Dim pathWord As String, pathPDF As String
    Dim fileName As String

    pathWord = wksProcess.Range("H11").Value
    pathPDF = wksProcess.Range("H12").Value
    
    currDate = wksProcess.Range("CurrDate").Value
    iteration = wksProcess.Range("Iteration") - 1
    fileName = "Export " & Format(currDate, "mm-dd-yyyy") & " - " & iteration
    
    On Error Resume Next
    Set wordApp = GetObject("Word.Application")
    If Err.Number <> 0 Then
        Set wordApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    
    wordApp.Activate
    ' The next line is where it locks up and that dialog with options appears.
    Set wordDoc = wordApp.Documents.Open(pathWord & Application.PathSeparator & fileName & ".docx")
    wordDoc.Activate

    wordDoc.SaveAs2 fileName:=pathPDF & Application.PathSeparator & fileName, FileFormat:=wdFormatPDF
    If wksProcess.Range("Messages") = False Then MsgBox "PDF exported to specified folder.", vbOKOnly
    Set wordDoc = Nothing
    wordApp.Quit
    Set wordApp = Nothing
    
End Sub
 
Upvote 0
Missed the edit. Maybe add...
Code:
wordDoc.Close
before the...
Code:
Set wordDoc = Nothing
in both subs. Dave
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,589
Members
449,174
Latest member
chandan4057

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top