簡體   English   中英

打開常規模板時出現運行時錯誤

[英]Runtime error while opening an oft template

我有一個郵件工具來創建Outlook模板。 模板存儲為工作表之一中的OLEObjects。

要使用模板,我正在Temp文件夾中創建它們的副本。 之后,該工具直接引用它並使用CreateItemFromTemplate打開。 這僅在我的PC上有效。 我公司中的其他人出錯。

重新創建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實例可能以某種方式鎖定文件。 因此,在使用模板或重新創建模板后,我在所有位置均未將其設置為任何值,但仍返回此錯誤。

您的文件或目錄為ReadOnly。 更改目錄的屬性,僅此而已。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM