Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word VB Form Document Attachment - Unlock / Open file / Re-lock

Status
Not open for further replies.

remeng

Technical User
Jul 27, 2006
518
US
Hello All;

I am working on attaching documents to a word form using VB to unlock the form, attach a document, and then relock the form. The issue is when I attempt to open the document from the form.

Current Code to attached the document (working):

Code:
Sub Add_table_file_attach()

'Unlock the document

If ActiveDocument.ProtectionType <> wdNoProtection Then
    
        ActiveDocument.Unprotect Password:="test"
        
    End If
    
    
' Add 1 cell in a table

    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
        1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
        
    With Selection.Tables(1)
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .ApplyStyleRowBands = True
        .ApplyStyleColumnBands = False
    End With

' Attach document in table

    Selection.InlineShapes.AddOLEObject ClassType:="Package", FileName:= _
        "", LinkToFile:=False, _
        DisplayAsIcon:=False
        
        
If ActiveDocument.ProtectionType <> wdNoProtection Then
    
        myDoc.Password = "test"
        
    End If

'Re-Locks the Document

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, Password:="test"


End Sub

add_doc_for_form_j2brvl.png


So the above code works to allow a file to be added from a button command when called. What I think I need to do for the open file function is to name the attached file as a public variable so that the open function can then open the specific file. Unfortunately, I do not know what code to use to assign the file name as a variable.

Any ideas?

Thanks,

Mike
 
Update:

So I spent a lot of time researching different ways to protect / unprotect a section where I can add the files and allow them to be opened.

Code:
ActiveDocument.Sections(2).ProtectedForForms = False

The question is, In the section, can you limit the type of edits that can be made?

Thanks,

Mike
 
Resolved:

Here is the code:

Code:
Private Sub ref_doc_Click()

'Focuses on the Reference Document unlocked section 6

Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=6, Name:=""



'add reference docunment into reference document section

    Selection.InlineShapes.AddOLEObject ClassType:="Package", FileName:= _
        "", LinkToFile:=False, _
        DisplayAsIcon:=False

Call TOC

End Sub
 
All:

So I had to add a section to the document and something is now allowing the section to be edited both by adding text, deleting text, or deleting the added file after the ref_doc code is run. Previously, after the file is added, it could not be removed and text on the form outside the fields could not be added, changed, or deleted. Any help would be appreciated.

What should be updated?

Thanks,

Mike

Current Code:

Code:
Private Sub ref_doc_Click()

'Focuses on the Reference Document unlocked section 7 - Reference Documents - Attachment (if section number changes, update Count:= # to reflect the change.

Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=7, Name:=""



'add reference docunment into reference document section

    Selection.InlineShapes.AddOLEObject ClassType:="Package", FileName:= _
        "", LinkToFile:=False, _
        DisplayAsIcon:=False

Call TOC

End Sub

TOC Code

Code:
Private Sub TOC()

' Macro updates the Table of Contents

Application.ScreenUpdating = False

Call pass_unlock

activedocument.TablesOfContents(1).Update


Call pass_lock

Application.ScreenUpdating = True


End Sub

Unlock Code:

Code:
Private Sub pass_unlock()

'Unlocks the form.  DO NOT CHANGE THE PASSWORD

If activedocument.ProtectionType <> wdNoProtection Then

    activedocument.Unprotect Password:="logspec"
    
End If


End Sub

Lock Document code:

Code:
Private Sub pass_lock()

'Locks the form.  DO NOT CHANGE THE PASSWORD

activedocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True, Password:="logspec"

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top