2016-07-22 92 views
1

我有一個郵件工具來創建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)'無法打開文件/路徑/。該文件可能不存在,您可能沒有權限打開它...

我讀了這個,並建議objOutlook實例可能以某種方式鎖定該文件。所以在玩模板或重新創建它們之後,我沒有將它設置到任何地方,但它仍然返回了這個錯誤。

+0

如果你給直接路徑'例如C:\ Users \ Om3r \ Documents \',會發生什麼? – 0m3r

+0

嗨。對不起,遲到的答案 - 基本上它是相同的錯誤 - 另外,如果我試圖從Windows打開.oft文件我得到一個類似的錯誤,說該文件可能不存在,您可能沒有權限打開它。這似乎是訪問該文件的情況。 – szczepan077

回答

0

你的文件或目錄是ReadOnly。更改目錄的屬性,這就是全部。