打开经常模板时出现运行时错误

Posted

技术标签:

【中文标题】打开经常模板时出现运行时错误【英文标题】:Runtime error while opening an oft template 【发布时间】:2016-11-26 06:33:15 【问题描述】:

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

为了使用模板,我在 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)”无法打开文件 /path/ 。该文件可能不存在,您可能无权打开它...

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

【问题讨论】:

如果你给出直接路径 `eg C:\Users\Om3r\Documents` 会发生什么? 嗨。很抱歉回答晚了 - 基本上这是同样的错误 - 另外,如果我尝试从 Windows 打开 .oft 文件,我会收到类似的错误,指出该文件可能不存在,您可能没有打开它的权限。好像根本就是访问文件的情况。 frmTemplates 没有在任何地方声明? 另外,当您将 .oft 作为 OLE 对象添加到工作簿时,请记住 .oft 文件的名称。然后当你 oleObj.Copy & objFolderItem.InvokeVerb ("Paste") 时,它会得到它的原始名称(导入时的名称)。 【参考方案1】:

您的文件或目录是只读的。更改目录的属性,仅此而已。

【讨论】:

以上是关于打开经常模板时出现运行时错误的主要内容,如果未能解决你的问题,请参考以下文章

使用查询变量时出现 Gatsby 运行时错误

PageOffice打开word时出现Office运行时错误,部分系统文件可能丢失或已损坏.(错误代码:0x80040154)

VS2010 打开时出现未知错误

从终端运行时出现运行时错误,但不是从 Eclipse

PageOffice打开word时出现Office运行时错误,部分系统文件可能丢失或已损坏.(错误代码:0x80040154)

PageOffice打开word时出现Office运行时错误,部分系统文件可能丢失或已损坏.(错误代码:0x80040154)