运行时错误,同时打开一个经典模板

我有一个邮件工具来创buildOutlook模板。 模板在其中一个工作表中存储为OLEObjects。

要使用这些模板,我要在Temp文件夹中创build它们的一个副本。 之后,工具直接引用它,并用CreateItemFromTemplate打开。 这只适用于我的电脑。 我公司的其他人出现错误。

重新创buildOLE对象的代码:

Sub RecreateObject(ObjectName As String, TemplateName As String) 'creates a copy of the template stored in config in the users temp folder so that we can reference it from hard drive Dim objShell As Object Dim objFolder As Variant Dim objFolderItem As Variant Dim oleObj As OLEObject Set objShell = CreateObject("shell.application") Set objFolder = objShell.Namespace(Environ("USERPROFILE") & "\Documents" & Application.PathSeparator) Set objFolderItem = objFolder.Self Set oleObj = wsConfig.OLEObjects(ObjectName) 'On Error GoTo Error1: oleObj.Copy If Dir(CStr(Environ("USERPROFILE") & "\Documents\" & TemplateName & ".oft"), vbDirectory) = vbNullString Then objFolderItem.InvokeVerb ("Paste") Else Kill Environ("USERPROFILE") & "\Documents\" & TemplateName & ".oft" oleObj.Copy objFolderItem.InvokeVerb ("Paste") End If EndThisSub: Set objShell = Nothing Set objFolder = Nothing Set objFolderItem = Nothing Set oleObj = Nothing Exit Sub Error1: MsgBox "Please re-open this file - template recreation failed." GoTo EndThisSub: End Sub 

打开模板的代码:

 Sub OpenTemplate(TemplateName As String, InsHeight As Long, InsWidth As Long, InsTop As Long, InsLeft As Long) Dim response Dim varEditedTempBody As Variant, varEditedTempSubject As Variant 'On Error GoTo Error1: Set objOutlook = CreateObject("Outlook.Application") 'On Error GoTo Error2: If objMail Is Nothing Then 'checks if any mails opened, if not fires procedure If curProcess = AddingTemplate Then Set objMail = objOutlook.CreateItem(0) Set objInspector = objMail.GetInspector objMail.Display objMail.Body = "" 'clearing the automatic signature End If If curProcess = EditingTemplate Then Set objMail = objOutlook.CreateItemFromTemplate(Environ("USERPROFILE") & "\Documents\" & frmTemplates.Controls(TemplateName).Value & ".oft") 'clearing the automatic signature by copying in the template after displaying varEditedTempBody = objMail.HTMLBody varEditedTempSubject = objMail.Subject Set objMail = objOutlook.CreateItemFromTemplate(Environ("USERPROFILE") & "\Documents\" & frmTemplates.Controls(TemplateName).Value & ".oft") With objMail .Display .HTMLBody = varEditedTempBody .Subject = varEditedTempSubject End With Set objInspector = objMail.GetInspector End If With objInspector .WindowState = 2 .Height = InsHeight .Width = InsWidth .Top = InsTop .Left = InsLeft End With Else response = MsgBox("A mail template is already opened. Would you like to proceed and close it without save?", vbYesNo) If response = vbYes Then 'if user agrees to closing procedure fires Call CloseTemplate If curProcess = AddingTemplate Then Set objMail = objOutlook.CreateItem(0) Set objInspector = objMail.GetInspector objMail.Display objMail.Body = "" 'clearing the automatic signature End If If curProcess = EditingTemplate Then Set objMail = objOutlook.CreateItemFromTemplate(Environ("USERPROFILE") & "\Documents" & Application.PathSeparator & frmTemplates.Controls(TemplateName).Value & ".oft") varEditedTempBody = objMail.HTMLBody varEditedTempSubject = objMail.Subject Set objMail = objOutlook.CreateItemFromTemplate(Environ("USERPROFILE") & "\Documents" & Application.PathSeparator & frmTemplates.Controls(TemplateName).Value & ".oft") With objMail .Display .HTMLBody = varEditedTempBody .Subject = varEditedTempSubject End With Set objInspector = objMail.GetInspector End If With objInspector .WindowState = 2 .Height = InsHeight .Width = InsWidth .Top = InsTop .Left = InsLeft End With Else objMail.Display Exit Sub End If End If ExitThisSub: Exit Sub Error1: MsgBox "Cannot open the Outlook application. Please note that mailer uses Outlook by default and without it it's not possible to use the program." GoTo ExitThisSub: Error2: MsgBox "The template cannot be opened from hard drive. Please contact ...." GoTo ExitThisSub: End Sub 

我得到这一行的错误:

  Set objMail = objOutlook.CreateItemFromTemplate(Environ("USERPROFILE") & "\Documents\" & frmTemplates.Controls(TemplateName).Value & ".oft") 

说:运行时错误'-2147286960(80030050)'无法打开文件/path/。 该文件可能不存在,您可能没有权限打开它…

我读了这个,并build议objOutlook的一个实例可能以某种方式locking文件。 所以在玩模板或者重新创build之后,我没有把它设置到任何地方,但是它仍然返回了这个错误。

您的文件或目录是ReadOnly。 更改目录的属性,这就是全部。