从文件复制Txt并输入到脚本中

时间:2018-11-19 12:36:42

标签: vba outlook

所以我们现在有了以下脚本。调试突出显示OpenTextfile,以粗体突出显示 在将其放入更新脚本的第二部分之前,我们可以以某种方式确认文本已被读取

函数TextFile_PullData()     目的:将所有数据从文本文件发送到字符串变量     昏暗的TextFile作为整数     昏暗的FilePath作为字符串     昏暗的FileContent作为字符串     昏暗的strUser作为字符串

' get the current user name
strUser = CreateObject("WScript.Network").UserName
'or use strUser = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%USERNAME%")

'File Path of Text File
FilePath = "C:\Users\" & strUser & "\Temp\VFile.txt"

'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile

'Open the text file
**Open FilePath For Input As TextFile**

'Store file content inside a variable
FileContent = Input(LOF(TextFile), TextFile)

'Close Text File
Close TextFile

'Report Out Text File Contents
MsgBox FileContent

'have the function return the data to the calling code
TextFile_PullData = FileContent

结束功能

Sub UpdateSubject()     昏暗的SaveCode作为字符串     昏暗的关键字作为字符串     昏暗的objItem作为MailItem

KeyWord = "TSD"

SaveCode = TextFile_PullData
Set objItem = GetCurrentItem()
objItem.Subject = "[" + KeyWord + "=" + SaveCode + "] " + objItem.Subject

结束子

函数GetCurrentItem()作为对象     昏暗的objApp作为Outlook.Application

Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
    Case "Explorer"
        Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
    Case "Inspector"
        Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select

Set objApp = Nothing

结束功能

2 个答案:

答案 0 :(得分:0)

这不是VBScript,因为您正在定义变量As <something>。在VBScript中,所有变量都是变量类型。

无论如何,您的Sub UpdateSubject可能很好地阅读了文本文件的内容,但是除了在消息框中显示之外,没有任何其他作用。 为了使其成为返回数据,而不是仅在仅包含该子对象的局部变量中读取它,请将其设置为Function

Function TextFile_PullData()
    'PURPOSE: Send All Data From Text File To A String Variable
    Dim TextFile As Integer
    Dim FilePath As String
    Dim FileContent As String
    Dim strUser As string

    ' get the current user name
    strUser = CreateObject("WScript.Network").UserName
    'or use strUser = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%USERNAME%")

    'File Path of Text File
    FilePath = "C:\Users\" & strUser & "\Temp\VFile.txt"

    'Determine the next file number available for use by the FileOpen function
    TextFile = FreeFile

    'Open the text file
    Open FilePath For Input As TextFile

    'Store file content inside a variable
    FileContent = Input(LOF(TextFile), TextFile)

    'Close Text File
    Close TextFile

    'Report Out Text File Contents
    MsgBox FileContent

    'have the function return the data to the calling code
    TextFile_PullData = FileContent
End Function

接下来在UpdateSubject子例程中使用该信息

Sub UpdateSubject()
    Dim SaveCode As String
    Dim KeyWord As String
    Dim objItem As MailItem
    Dim FileContent As String

    ' here, you use the function to pull the content of the text file and store it in
    ' a local variable called 'FileContent' to use in your inputbox.
    FileContent = TextFile_PullData


    SaveCode = InputBox("Please enter filecode in the format nnn/nnn", "VisualFiles Auto Save", FileContent)

    Set objItem = GetCurrentItem()
    KeyWord = "TSD"

    objItem.Subject = "[" + KeyWord + "=" + SaveCode + "] " + objItem.Subject

    'or skip the inputox alltogether and set the subject directly:
    'objItem.Subject = "[" + KeyWord + "=" + FileContent + "] " + objItem.Subject
End Sub

答案 1 :(得分:0)

````   Const ForReading = 1, ForWriting = 2
````   Dim fso, f
````   Set fso = CreateObject("Scripting.FileSystemObject")
````   Set f = fso.OpenTextFile(Environ("USERPROFILE") & "\temp\email.txt")
````   ReadAllTextFile = f.ReadAll

````End Function

````Public Sub UpdateSubject()

````   Dim SaveCode As String
````   Dim KeyWord As String
````   Dim objItem As MailItem

````    KeyWord = "ABD"

````   SaveCode = InputBox("Please enter filecode in the format nnn/nnn", "VisualFiles Auto Save", ReadAllTextFile)
````   MsgBox SaveCode
````   Set objItem = GetCurrentItem()
````   objItem.Subject = "[" + KeyWord + "=" + SaveCode + "] " + objItem.Subject

````End Sub
````Function GetCurrentItem() As Object
````    Dim objApp As Outlook.Application

````    Set objApp = Application
````    On Error Resume Next
````    Select Case TypeName(objApp.ActiveWindow)
````        Case "Explorer"
````            Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
````        Case "Inspector"
````            Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
````    End Select
````    Set objApp = Nothing
````End Function


This does what i want on a local pc. Adds a text file puts it in a text box for confirmation, once confirmed adds the text to the subject of the email.

Any idea why this would not work on a terminal server? all local installs of Outlook work fine it can find the email.txt file, but on a terminal server it does not. If i message out the location like Below it gives me the correct location

````MsgBox (Environ("USERPROFILE") & "\temp\email.txt")

Any suggestions