简体   繁体   中英

How to open an embedded object in spreadsheet as read only through excel VBA

I have few objects embedded in a hidden sheet in workbook which runs the VBA code. These objects (word, excel, pdf etc.) are just the templates and I need to open a copy of these or open these as read only upon a click on a command button, so that the template content remains same.

I have searched over the internet but did not find a way to open these embedded objects as read only. I am running this code but save as operation is not successful.

Private Sub M114_Click()
Dim WDObj As Object
Dim WDApp As Object

Set WDApp = GetObject(, "Word.Application")
Set WDObj = Sheets("Tools").OLEObjects("MO")

WDObj.Activate

WDApp.ActiveDocument.SaveAs ("MO_copy.doc")
Set WDObj = Nothing
Set WDApp = Nothing
End Sub

You need to protect the document to prevent changes before you embed it in Excel.


With the example of a Word Document:

You can use Word's protection functionality to protect it, and could optionally give different rights to different users.

  1. Go to the Review tab on the ribbon.
  2. In the Protect group, click Restrict Editing
  3. In section 2 of the Restrict Editing options on the side of the screen, check the box that says " Allow only this type of editing " and make sure the dropdown is set to **No Changes (Read Only)** .
  4. Set other protection options as desired.
  5. In section 3 click Yes, start enforcing .
  6. Save & close the document and then embed in your Excel file.

Alternatively, if your embedded object is linked to the source file, you can set the file to Read Only in the Windows file properties. This won't work if the object isn't linked to the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM