+ Reply to Thread
Results 1 to 8 of 8

File Permission Errors With VBA Assisted Mail Merge / Excel VBA Userform Access

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    File Permission Errors With VBA Assisted Mail Merge / Excel VBA Userform Access

    Excel userform (uf4_works) with 24 toggle buttons (tglb_xxx_xxxx where xxx_xxxx is variable based on selection) allowing user to select any number of reports for creation (via Word mail merge) or viewing (previously created)
    Here is code from one of the 24 button presses.

    Private Sub tglb_cue_diar_Click()
        If mbEvents Then Exit Sub
        With Me
            Set oBP = .tglb_cue_diar
            sRPT = "CUE-DR.docx"
            button_check ' oBP, sRPT
        End With
    End Sub
    Here is the 'button_check' routine
    Sub button_check()  '(ByVal oBP As Object, sRPT As String)
    'Stop
    'oBP = userform toggle button name (from toggle button click event)
    'sRPT = report (.docx) filename (from toggle button click event)
        Debug.Print oBP.Name
    
        If bpb = 0 Then 'bypass if bypass flag = 1
            'With uf4_works 'the userform
                With oBP
                    'this allows a previouly created report to be viewed.
                    If .BackColor = RGB(0, 153, 211) And .Value = True Then
                        If DocOpen(sRPT) = False Then
                            Set WordApp = CreateObject("word.Application")
                            WordApp.Documents.Open path_name & sRPT
                            WordApp.Visible = True
                            Set WordApp = Nothing
                            .Value = True
                        End If
                        '.BackColor = RGB(102, 0, 204) 'purple
                        Exit Sub
                    ElseIf .BackColor = RGB(0, 153, 211) And .Value = False Then
                        Application.DisplayAlerts = False
                        With GetObject(path_name & sRPT)
                            .Save
                            'objWord.Quit '
                            'Set objWord = Nothing  '
                            .Application.Quit
                        End With
                        Application.DisplayAlerts = True
                        .BackColor = RGB(0, 153, 211)
                        .Value = False
                       ' Exit Sub
                    ElseIf .BackColor = RGB(229, 38, 38) And .Value = True Then
                        mbEvents = True
                        .BackColor = RGB(0, 153, 211)
                        .Value = False
                        mbEvents = False
                        Exit Sub
                    Else
                        If .Value = True Then
                            uf4_works.tb_of_rpt.Value = uf4_works.tb_of_rpt.Value + 1
                        Else
                            uf4_works.tb_of_rpt.Value = uf4_works.tb_of_rpt.Value - 1
                        End If
                    End If
                End With
            'End With
        End If
    End Sub
    Once the user has selected the report(s) they wish to create they can proceed with the creation of the mail merge document creation process by pressing a button on the userform ([PROCEED]) toi launch the code which processes the selections and prepares them for the merge process. Here is that code (greatly editted to exclude the repetitiveness of 24 reports, I have shown only one of those 24)

    Private Sub cb_preview_Click()
    
        Dim path As String, dirname As String, dest1 As String
        Dim riq As Double
        Dim objWord As Object
        Dim objDoc As Object
        Dim oFS As Object
        Dim dfn As String, rtq As String
        Dim ui1 As VbMsgBoxResult
        Dim rptbtn As Object
        Dim cas As Long
        Dim lrow As Long, screw As String
        
        Set objWord = CreateObject("Word.Application")
        Set oFS = CreateObject("Scripting.FileSystemObject")
    
        cas = 0
        
        If FormIsLoaded("uf1_create_wo1") Then Unload uf1_create_wo1
        'Unload uf1_create_wo1
        
        Application.ScreenUpdating = False
        
        If Me.tb_of_rpt = 0 Then
            MsgBox "No reports have been selected.", vbExclamation, "ERROR"
            Exit Sub
        End If
        
        'populate print que
        With ws_th
            .Cells.Clear
           
            'Reg Diamonds
            If Me.tglb_cue_diar.Value = True And Me.tglb_cue_diar.BackColor = RGB(176, 196, 222) Then 'button state is true, color represents file creation is selected for creation
                last_cell
                .Range("A" & lj) = "CUE-DR"
            Else
                '23 other different toggle buttons
            End If
        End With
        
        'create directory
            path = "u:\---\-----\----------------\------\--------\WORKORDERS\"
            dirname = format(ws_vh.Range("B17"), "ddd dd-mmm-yy")
            dest1 = path & dirname
            On Error Resume Next
            MkDir dest1
            On Error GoTo 0
            
        'how many reports in que
            riq = WorksheetFunction.CountA(ws_th.Range("A2:A49"))
            
        'prepare for report creation (mail merge)
        For i = 2 To riq + 1
            Me.tb_cur_rpt = Me.tb_cur_rpt + 1
            rpt_od = ws_th.Range("A" & i)
            
           If rpt_od = "CUE-DR" Then
                Me.tglb_cue_diar.BackColor = RGB(229, 38, 38) 'button state is true, color represents file creation is in progress
                Set oBP = Me.tglb_cue_diar
                sRPT = rpt_od
           Else
                '23 other different toggle buttons
           End If
            
            dfn = rpt_od & ".docx"
            rtq = dest1 & "\" & dfn
            
            If Dir(rtq) <> "" Then 'option to view or print previously created selected report
                ui1 = MsgBox("This report has been previously created on " & Chr(13) & format(oFS.GetFile(rtq).DateCreated, "ddd dd-mm-yy hh:mm:ss") & "." & _
                     Chr(13) & "Press [YES] to recreate the report, or [NO] to view it.", vbQuestion + vbYesNoCancel, dfn & " EXISTS")
                
                If ui1 = vbNo Then
                    Set objDoc = objWord.Documents.Open(rtq)
                    objWord.Visible = True
                ElseIf ui1 = vbCancel Then
                    MsgBox "Cancel me."
                Else
                    dest = 1 'send to new document
                    merge2 i, rpt_od, objWord, dest ', pr, pn, path_name '[module 39]
                    cas = cas + 1
                End If
            
            Else 'create new report
                dest = 1 'send to new document
                merge2 i, rpt_od, objWord, dest ', pr, pn, path_name '[module 39]
                cas = cas + 1
            End If
            
            'not sure what this does. Perhaps open the document after creation? Might be the root of my problem?
            On Error Resume Next
            fn99 = work_fn
            Set wBook = Workbooks(fn99)
            If wBook Is Nothing Then
                'open workbook after having been closed during report making
                Workbooks.Open ("u:\---\-----\----------------\------\--------\DATA\" & fn99)
                'ActiveWorkbook.Windows(1).Visible = False
                Set wBook = Nothing
                On Error GoTo 0
            End If
            On Error GoTo 0
            
            'Following creation, reset selected button to FALSE state and change colour format to reflect the condition that report has been created, saved and accessible by clicking same button in the future
            If rpt_od = "CUE-DR" Then 
                With Me.tglb_cue_diar
                    bpb = 1
                    '.BackColor = RGB(0, 153, 211)
                    .ForeColor = RGB(0, 52, 98)
                    '.Value = False
                End With
            Else
            '23 other different toggle buttons
            End If
            
            ws_th.Range("A" & i) = ""
            
        Next i
        'bpb = 0 ' reset bypass button
        
        If cas > 0 Then
            fn = ws_vh.Range("B23").Value
            Set wb_nwb = Workbooks(fn)
            Set ws_dev = wb_nwb.Worksheets("DEV")
            ws_dev.Range("B6").Value = Now
            MsgBox cas & " report(s) created and saved.", vbInformation, "SUCCESS  " & Now
        End If
        
        'counters
        Me.tb_cur_rpt.Value = Me.tb_cur_rpt.Value - 1
        Me.tb_of_rpt.Value = Me.tb_of_rpt.Value - 1
        ws_th.Columns(1).Clear
        
        'testing purposes, self teaching, ineffewctive at solving posted prioblem 
        'objWord.Quit
        Set objWord = Nothing
        
        Application.ScreenUpdating = True
        
    End Sub
    ... to reduce the length of the post, it's continued in the next reply.

  2. #2
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: File Permission Errors With VBA Assisted Mail Merge / Excel VBA Userform Access

    Continued from first post ...

    Here is the code that that launches the mail merge routine ...
    Sub merge2(ByVal i As Long, ByVal rpt_od As String, objWord As Object, ByVal dest As Long)
    
        Dim oDoc As Object, oDoc2 As Object
        Dim StrSQL As String, fName As String, StrSrc As String, strFilename As String, myPath As String
        Dim qfile As String, st_srchfn As String, wb_qfile2 As Workbook, itype As String, isubresp As String
        'Dim wb_qfile2 As Workbook
        Dim HdFt As Variant
        Dim wdSendToNewDocument
        
        Const wdSendtToNewDocument = 0
        Const wdSendToPrinter = 1
        Const wdFormLetters = 0
        Const wdDirectory = 3
        Const wdMergeSubTypeAccess = 1
        Const wdOpenFormatAuto = 0
        
        work_fn = ws_vh.Range("N2")
        Set wb_nwb = Workbooks(work_fn)
        
        'create workorders folder
        myPath = "u:\---\-----\----------------\------\--------\WORKORDERS\" & format(ws_vh.Range("B17"), "ddd dd-mmm-yy")
        If Dir(myPath, vbDirectory) = "" Then 'if not already created ...
            MkDir myPath
        End If
        
        'close data file
        st_srchfn = "u:\---\-----\----------------\------\--------\DATA\" & ws_vh.Range("N2")
        If wb_nwb Is Nothing Then
            MsgBox wb_nwb & " is NOT open."
        Else
            wb_nwb.Close True 'saves data workbook after TYPE was updated for GS
            With ws_base
                .Range("B24:D24").Value = ws_vh.Range("A57:C57").Value
                .Range("C24").Interior.color = RGB(220, 20, 60)
            End With
        End If
      
        itype = Right(ws_th.Range("A" & i), 2)
        isubresp = Left(ws_th.Range("A" & i), 3)
        
        If itype = "DR" Then
            fName = "u:\---\-----\----------------\------\--------\REPORTS\NG\DR15NG.docx"
        ElseIf itype = "DT" Then
            fName = "u:\---\-----\----------------\------\--------\REPORTS\NG\DT15NG.docx"
        ElseIf itype = "FR" Then
            fName = "u:\---\-----\----------------\------\--------\REPORTS\NG\FR15NG.docx"
        ElseIf itype = "FT" Then
            fName = "u:\---\-----\----------------\------\--------\REPORTS\NG\FT15NG.docx"
        ElseIf itype = "CR" Then
            fName = "u:\---\-----\----------------\------\--------\REPORTS\NG\CR15NG.docx"
        ElseIf itype = "CT" Then
            fName = "u:\---\-----\----------------\------\--------\REPORTS\NG\CT15NG.docx"
        ElseIf itype = "GS" Then
            If isubresp = "HPE" Or isubresp = "HPL" Then
                fName = "u:\---\-----\----------------\------\--------\REPORTS\NG\GS15NG_GSH.docx" 'Passive : Hillside
            Else
                fName = "u:\---\-----\----------------\------\--------\REPORTS\NG\GS15NG_GS.docx" 'Passive : Wloo Park
            End If
        Else
            fName = "u:\---\-----\----------------\------\--------\REPORTS\NG\GS15NG_GM.docx"
        End If
        
        StrSrc = "u:\---\-----\----------------\------\--------\DATA\" & ws_vh.Range("N2")
     
        StrSQL = "SELECT * FROM [DATA$] WHERE [TYPE]='" & itype & "' AND [SIG_CREW]='" & isubresp & "' " & _
            "ORDER BY [STARTS] ASC, [COMPLEX] ASC, [UNIT] ASC"
     
        Set objWord = CreateObject("Word.Application")
        With objWord
            .DisplayAlerts = False
            .Visible = True
            Set oDoc = .Documents.Open(Filename:=fName, ConfirmConversions:=False, _
                ReadOnly:=True, AddToRecentFiles:=False, Visible:=True)
            With oDoc
                With .MailMerge
                    .MainDocumentType = wdFormLetters
                    .Destination = wdSendToNewDocument
                    .SuppressBlankLines = True
                    .OpenDataSource Name:=StrSrc, AddToRecentFiles:=False, LinkToSource:=False, ConfirmConversions:=False, _
                        ReadOnly:=True, format:=wdOpenFormatAuto, Connection:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
                        "User ID=Admin;Data Source=" & StrSrc & ";Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";", _
                        SQLStatement:=StrSQL, SQLStatement1:="", SubType:=wdMergeSubTypeAccess
                    .Execute Pause:=False
                End With
                .Close savechanges:=False   '.close false
            End With
            .DisplayAlerts = True
            
            'page break routine only for sports reports
            If (Left(itype, 1) <> "G") And (itype <> "DT") Then   'exclude GS reports
                With .activedocument
                    If .Sections.count > 1 Then
                        For Each HdFt In .Sections(.Sections.count).Headers
                            If HdFt.Exists Then
                                HdFt.Range.FormattedText = .Sections(1).Headers(HdFt.index).Range.FormattedText
                                HdFt.Range.Characters.Last.Delete
                            End If
                        Next
                        For Each HdFt In .Sections(.Sections.count).Footers
                            If HdFt.Exists Then
                                HdFt.Range.FormattedText = .Sections(1).Footers(HdFt.index).Range.FormattedText
                                HdFt.Range.Characters.Last.Delete
                            End If
                        Next
                    End If
                    Do While .Sections.count > 1
                        .Sections(1).Range.Characters.Last.Delete
                        DoEvents
                    Loop
                    .Range.Characters.Last.Delete
                End With
            End If
        
        End With
        
        Set oDoc2 = objWord.activedocument
    
        'save newly created document
        With oDoc2
            myPath = "u:\---\-----\----------------\------\--------\WORKORDERS\" & format(ws_vh.Range("B17"), "ddd dd-mmm-yy")
            .SaveAs myPath & "\" & rpt_od & ".docx"
            If dest = 2 Then
                .PrintOut
            End If
            .Close savechanges:=False 'close (this line was
            objWord.Quit
            Set objWord = Nothing
    
            button_check ' oBP, sRPT 
    
        End With
    
        Set oDoc = Nothing: Set oDoc2 = Nothing ': Set objWord = Nothing
    
    End Sub
    So, that's the lead in to the problem.

    This for the most part works wonderfully. The selection of the reports, the setup for their creation, and the mail merge process are working well. When the mail merge is complete, the report is saved, and Word closes for that instance before being reopened upon the creation of the next report. And so that routine continues as long as reports are being created from the cue. These newly created files are saved, and following saving, at any point thereafter, thehose mail merge documents can be opened and edited, and then saved wihoout any error. I had been d=getting file permission errors when I corrected my mistake of not quiting objWord and setting it to nothing after the completion of each report creation. Back on the userform though the button state for the selected and recently created report is TRUE with formatting indicating file creation in progress. In an effort to reset the button to a FALSE state and change it's formatting to reflect on the report having been successfully created and saved, i included the line in orange.

    This line effectively resets the button, but, after doing so, that file becomes locked again with file permission conflicts whenever that file is edited thereafter and attempted to be saved. So, if it don't do a 'button_check' to reset the button to the preferred state, the created file is not read only (locked) and can be edited and easily saved. With button check executed, those same files become read only (locked) and I can't save any edits regardless how the file was accessed.

    This is one of the last remaining "significant" hurdles remaining in a 4 year project. I would be greatly indebted to anyone that can help solve the problem. I figure since it took me more than an hour to post this, the solution will likely be super simple! But however simple it is, it's not that simple for me to figure out. I need your help!

  3. #3
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: File Permission Errors With VBA Assisted Mail Merge / Excel VBA Userform Access

    Even with button_check out of the picture I get errors.
    I don't know whats going on. Sometimes I get errors, sometimes I don't. I have a hard to recreating the scenario, I don't know at what point the file is being "locked". I may have to take this to a paid solution.

  4. #4
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: File Permission Errors With VBA Assisted Mail Merge / Excel VBA Userform Access

    Further testing.
    Created all reports (mail merge docs). No Word instances open or visible.
    Closed up Excel.
    None of the 8 reports the Excel app created can be saved after opening, even if there were no changes.
    "Word cannot complete the save due to a file permission error."

  5. #5
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: File Permission Errors With VBA Assisted Mail Merge / Excel VBA Userform Access

    And, I have just realized that this problem isn't just exclusive to the reports created by this Excel VBA driven mail merge.
    Other totally unassociated .docx files on that drive cannot be saved after opening due to file permission errors.

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

    Re: File Permission Errors With VBA Assisted Mail Merge / Excel VBA Userform Access

    Quote Originally Posted by Jenn68 View Post
    Other totally unassociated .docx files on that drive cannot be saved after opening due to file permission errors.
    In that case, your problems most likely have nothing to do with Word or Excel, but with your network configuration. That said, you might try repairing the Office installation (via Windows Control Panel > Programs > Programs & Features > Microsoft Office (version) > Change > Repair).
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: File Permission Errors With VBA Assisted Mail Merge / Excel VBA Userform Access

    Thanks Paul, but would that be a solution for the same problem two totally different computers? Its only for that reason I suspect the files. I don't experience the issue in post #5 after I restart my computer, it appears (and it's difficult to define because I have yet to pinpoint the circumstances as to when I have this problem) that these files don't exhibit the problem (although the docx. created by mail merge through the Excel VBA are consistently locked.)

    For example, those files I tried to edit and save from my work computer (onto a thuimbdrive that I work from between computers) yesterday that gave me problems. I have no problems with while working today at home with a fresh session.
    Last edited by Jenn68; 06-15-2019 at 08:24 AM.

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

    Re: File Permission Errors With VBA Assisted Mail Merge / Excel VBA Userform Access

    The fact that multiple computers are affected, plus documents other than reports created by your mail merge are affected certainly points in the direction of a network configuration issue (e.g. with user profiles and/or a network fault). Your ability to edit the document on a thumb drive reinforces that view.

+ 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. Create mail merge using excel VBA userform
    By emina002 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-06-2016, 09:24 AM
  2. Running Word Mail Merge from Excel 2010 - Mail Merge workbook fails
    By pl05.lau@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-05-2015, 07:35 AM
  3. Replies: 3
    Last Post: 05-08-2014, 07:11 PM
  4. Replies: 2
    Last Post: 01-22-2013, 11:37 AM
  5. [SOLVED] RUN a mail merge (Word file) from a USERFORM in EXCEL
    By alopecito in forum Excel Programming / VBA / Macros
    Replies: 29
    Last Post: 09-18-2012, 09:41 PM
  6. Macro errors before save of mail merge document
    By pjbassdc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-06-2011, 02:35 PM
  7. [SOLVED] Re: There should be a mail merge feature between excel and access.
    By Vira-SJH in forum Excel General
    Replies: 0
    Last Post: 01-03-2006, 02:25 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