2016-07-22 34 views
1

私はOutlookテンプレートを作成するメーリングツールを持っています。テンプレートはワークシートの1つにOLEObjectとして保存されます。oftテンプレートを開くときにランタイムエラーが発生する

テンプレートを使用するには、テンプレートのコピーを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のインスタンスが何らかの理由でファイルをロックする可能性があります。ですから、私はテンプレートを使って遊んだり、再作成したりしても、どこにも設定しませんでしたが、まだこのエラーを返しました。

+0

直接パス(例:C:\ Users \ Om3r \ Documents \)を指定するとどうなりますか? – 0m3r

+0

こんにちは。遅い答えに申し訳ありません - 基本的に同じエラーです - また、Windowsから.oftファイルを開こうとすると、ファイルが存在しない可能性があるという似たエラーが表示されます。ファイルへのアクセスはまったくないようです。 – szczepan077

答えて

0

あなたのファイルまたはディレクトリはReadOnlyです。ディレクトリのプロパティを変更すると、それだけです。

関連する問題