PDA

View Full Version : Word Macro to attach file to a form



tostrand
08-09-2017, 03:32 AM
I have a form I have been working with. The form it self need to be protected. I don't know much about VBA programming so there lies my issue.

I have this button with this code to it.


Private Sub Bilaga_Click()
' Browse & Select File
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = True
.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, "\"))


What I need it to do is to add the attached file to a textbox or some other means in the protected document.
I know the above code attache the file to the form but only works when unprotected.

Any help is appreciated

gmayor
08-09-2017, 04:24 AM
There are some things that cannot be achieved when the form is locked. You could unlock the form, perform the action then lock it again.
Alternatively you could convert your form to use Content Controls which don't require the form be locked.



If Not ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If

'Do Stuff

ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""

You