Microsoft Office Forums Button with VBA to attach a file in a protected document

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-05-2019, 05:13 AM
wolfgrrl wolfgrrl is offline Button with VBA to attach a file in a protected document Windows 10 Button with VBA to attach a file in a protected document Office 2010
Novice
Button with VBA to attach a file in a protected document
 
Join Date: May 2019
Posts: 14
wolfgrrl is on a distinguished road
Default Button with VBA to attach a file in a protected document

I've got a form running perfectly with the exception of my "attach" button. I've got this macro attached to my button:

Code:
Private Sub CommandButton2_Click()
' Browse & Select File
With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Title = "Select the File that you want to insert"
        If .Show = True Then
            FiletoInsert = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
    
    ' Embed File Inline
    Application.Selection.InlineShapes.AddOLEObject _
        FileName:=FiletoInsert, _
        LinkToFile:=False, _
        DisplayAsIcon:=True, _
        IconLabel:=Right(FiletoInsert, Len(FiletoInsert) - InStrRev(FiletoInsert, "\"))
End Sub
When the document is unprotected. the button works and files attach as expected. However, when I turn protection on in the document, attach fails.

I've got my protection set up as shown in the attachment.



How can I get the attach macro to play nice with the protection?

Thank you!
Attached Images
File Type: png Annotation 2019-09-05 081126.png (11.7 KB, 9 views)
Reply With Quote
  #2  
Old 09-05-2019, 11:34 AM
gmaxey gmaxey is offline Button with VBA to attach a file in a protected document Windows 10 Button with VBA to attach a file in a protected document Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 962
gmaxey will become famous soon enoughgmaxey will become famous soon enough
Default

add code in your macro to unprotect and then reprotect the document.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 09-13-2019, 06:38 AM
wolfgrrl wolfgrrl is offline Button with VBA to attach a file in a protected document Windows 10 Button with VBA to attach a file in a protected document Office 2010
Novice
Button with VBA to attach a file in a protected document
 
Join Date: May 2019
Posts: 14
wolfgrrl is on a distinguished road
Default

@gmaxey

I've found your code here: VBA code in word 2007 for "protect" and "unprotect" - Microsoft Community

I need to adapt this for password-protected forms.

Can you please assist?

Thank you!
Reply With Quote
  #4  
Old 09-20-2019, 07:07 AM
wolfgrrl wolfgrrl is offline Button with VBA to attach a file in a protected document Windows 10 Button with VBA to attach a file in a protected document Office 2010
Novice
Button with VBA to attach a file in a protected document
 
Join Date: May 2019
Posts: 14
wolfgrrl is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
add code in your macro to unprotect and then reprotect the document.
Gary,

Are you able to assist?

Thank you!
Reply With Quote
  #5  
Old 09-20-2019, 07:15 AM
gmaxey gmaxey is offline Button with VBA to attach a file in a protected document Windows 10 Button with VBA to attach a file in a protected document Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 962
gmaxey will become famous soon enoughgmaxey will become famous soon enough
Default

Something like this should do. Change "Password" to your document password:

Code:
Option Explicit
Private Sub CommandButton2_Click()
  With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = False
    .Title = "Select the File that you want to insert"
    If .Show = True Then
      FiletoInsert = .SelectedItems(1)
    Else
      Exit Sub
    End If
  End With
  UnProt "Password"
  Application.Selection.InlineShapes.AddOLEObject _
        FileName:=FiletoInsert, _
        LinkToFile:=False, _
        DisplayAsIcon:=True, _
        IconLabel:=Right(FiletoInsert, Len(FiletoInsert) - InStrRev(FiletoInsert, "\"))
  Prot "Password"
End Sub
Sub Prot(strPW As String)
  'A basic Word macro coded by Greg Maxey
  ActiveDocument.Protect wdAllowOnlyFormFields, True, strPW
End Sub
Sub UnProt(strPW As String)
  'A basic Word macro coded by Greg Maxey
  If ActiveDocument.ProtectionType <> wdNoProtection Then
    ActiveDocument.Unprotect strPW
  End If
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 09-20-2019, 07:28 AM
wolfgrrl wolfgrrl is offline Button with VBA to attach a file in a protected document Windows 10 Button with VBA to attach a file in a protected document Office 2010
Novice
Button with VBA to attach a file in a protected document
 
Join Date: May 2019
Posts: 14
wolfgrrl is on a distinguished road
Default

Nevermind - I got it. Thank you!
Attached Images
File Type: png Annotation 2019-09-20 102706.png (32.0 KB, 2 views)
Reply With Quote
  #7  
Old 09-20-2019, 07:38 AM
gmaxey gmaxey is offline Button with VBA to attach a file in a protected document Windows 10 Button with VBA to attach a file in a protected document Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 962
gmaxey will become famous soon enoughgmaxey will become famous soon enough
Default

So you image doesn't show:

Code:
Sub Prot(strPW As String)   'A basic Word macro coded by Greg Maxey   ActiveDocument.Protect wdAllowOnlyFormFields, True, strPW End Sub Sub UnProt(strPW As String)   'A basic Word macro coded by Greg Maxey   If ActiveDocument.ProtectionType <> wdNoProtection Then     ActiveDocument.Unprotect strPW   End If End Sub

Where did you put them?  Put them in your userform module with the command button code.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA to create a button to attach the active word doc to an email as a PDF without using Outlook TAKMalcolm Word VBA 1 09-21-2017 01:52 AM
Button with VBA to attach a file in a protected document How do I attach a ribbon button to a particular template in Word 2010? HelenT Word VBA 1 10-07-2015 03:32 AM
File Not Found error when trying to attach Word document to email Kimber Word 0 03-06-2015 06:47 PM
Button with VBA to attach a file in a protected document Command Button will not work when document is protected brockjensen Word 1 11-02-2012 06:59 PM
Debug for macro run through button only when sheet protected leahca Excel Programming 0 11-24-2011 04:47 AM


All times are GMT -7. The time now is 02:48 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft