打开oft模板时出现运行时错误

时间:2016-07-22 11:12:13

标签: excel vba outlook oft

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

要使用模板,我将在Temp文件夹中创建它们的副本。之后,该工具直接引用它并使用CreateItemFromTemplate打开。这仅适用于我的电脑。我公司的其他人收到错误。

重新创建OLE对象的代码:

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)'无法打开文件/路径/。该文件可能不存在,您可能没有权限打开它...

我读到了这个,并且建议是objOutlook的实例可能以某种方式锁定文件。因此,在使用模板或重新创建模板后,我已将其设置为无处,但它仍然返回此错误。

1 个答案:

答案 0 :(得分:0)

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