简体   繁体   中英

Opening Workbook from Outlook without read-only mode

I'm trying to open an excel workbook and run a macro from Outlook. Unfortunately everytime I open the workbook it seems to default to read-only mode, meaning that I cannot run the macro I want to.

Here is my code:

Sub runPat(client As String)

'Runs a PAT using tool with client as input

Dim xlApp As Excel.Application
Set xlApp = New Excel.Application

Dim sourceWB As Workbook
Dim usr As Worksheet
Dim strFile As String

Set xlApp = CreateObject("Excel.Application")

With xlApp
    .Visible = True
    .EnableEvents = False
End With

'Source file, will need to be changed if PAT is ever moved
strFile = "C:\Users\albio\Dropbox\Document Creation Program\Portfolio Analysis Tool\Portfolio Analysis tool v1.0.xlsm"  'PAT File path

'Open workbook
Set sourceWB = Workbooks.Open(strFile, , False)

'Run this PAT
sourceWB.Application.Run "buttons.launchButton"

End Sub

I've tried various solutions, such as setting IgnoreReadOnlyRecommended=True and using setAttr to vbNormal before opening the file path. For some reason, it always opens in read-only mode and I can't see why?

[EDIT] The workbook normally opens without read-only mode on. It is only when the workbook is opened within my Outlook VbaProject that read-only is enabled.

If you are opening the workbook directly from excel and it opens in read-only mode that happens because it is not a trusted document. Usually if you click on enable macros it would add it as a trusted document next time onwards. If that doesn't happen that may be because trusted documents have been disabled.

You can enable trusted documents by navigating to the following location in excel

file > Options > Trust Center > Trust Center Settings > Trusted Documents

Here you need to uncheck the "Disable Trusted Documents" and maybe even check the "Allow documents on a network to be trusted"


If you are trying to open the excel file through a VBA code then you need to change the automation security before opening the workbook. For example

application.AutomationSecurity = msoAutomationSecurityLow
Workbooks.Open abc.xlsm
Application.AutomationSecurity = msoAutomationSecurityByUI 'Changes it back to the default value

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