-
Dec 4th, 2019, 01:40 PM
#1
Thread Starter
New Member
Reading from Text file
Hey Guys!
Maybe someone can help me with this ..
Just a chunk of a bigger script
If (bWordExtras) Then
ReportProgress " Writing System Information"
WriteHeader 1,"System Information"
oWord.Selection.Style = wdStyleBodyText
oWord.Selection.TypeText "text from file C:\file.txt" & vbCrLf
End If
Is there an easy way to include text from file.txt in that part of the script, I guess it needs to read the file and then print this back ?
-
Dec 4th, 2019, 02:00 PM
#2
Re: Reading from Text file
You'd need to read the file first (or concurrently).
----
EDIT: Take a look at the "Open" and "Line Input" statements. That's probably how I'd first attack the problem.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Dec 4th, 2019, 02:14 PM
#3
Thread Starter
New Member
Re: Reading from Text file
Originally Posted by Elroy
You'd need to read the file first (or concurrently).
----
EDIT: Take a look at the "Open" and "Line Input" statements. That's probably how I'd first attack the problem.
Thank you! very helpful !
And yes ... I can read it at the top of the script and keep it in variable .. not an issue
-
Dec 5th, 2019, 01:39 PM
#4
Thread Starter
New Member
Re using variables ?
Hi Guys,
So I have a larger open source script ... and I wanted to read information from a file and and then re use the data somewhere down stream in the script.
Script ( Fetches text from txt file )
Code:
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\randomdata.txt")
Do Until objFile.AtEndOfStream
strData = ""
strSearchString = objFile.ReadLine
intStart = InStr(strSearchString, "<data01>")
If intStart <> 0 Then
intStart = intStart + 13
strText = Mid(strSearchString, intStart, 500)
For i = 1 to Len(strText)
If Mid(strText, i, 1) = "MoreData:" Then
Exit For
Else
strData = strData & Mid(strText, i, 1)
End If
Next
End If
Loop
I added this at the top of the original script and I tried to re use variable strData but it will not print anything
Code:
If (bWordExtras) Then
ReportProgress " Recording"
WriteHeader 1,"Name 01"
oWord.Selection.Style = wdStyleBodyText
oWord.Selection.TypeText "Test" & strData & vbCrLf
End If
Any suggestion would be appreciated ! thank you in advance.
Last edited by Shaggy Hiker; Dec 5th, 2019 at 03:29 PM.
Reason: Added CODE tags.
-
Dec 5th, 2019, 02:38 PM
#5
Re: Re using variables ?
If you added the block at the bottom to the top of the script shown above it then strData will not have been assigned a value yet. Of course I am not sure that is what you meant nor am I sure what your are using. You say script which would imply VBScript but you have posted in a VB section [there is a different section for VBScript] It is also possible that you are talking about Office VBA which has its own location for questions as well. While all three shared many things in common VBScript, VBA and VB are 3 different things.
So let us know which environment you are actually using and show us the actual code in the position it occurs so we can see what you are really doing.
-
Dec 5th, 2019, 02:43 PM
#6
Thread Starter
New Member
Re: Re using variables ?
Thank you, Sorry it is VBScript .vbs ....
I will repost this
But to clarify the big block ( open file, search file ) is at the top and the below is the little block that writes to word... so strData should be populated ?
Thank you
-
Dec 5th, 2019, 03:12 PM
#7
Re: Re using variables ?
If the code occurs in the positions posted then yes it should be populated unless for whatever reason it is not being populated by the code above it or there is code that changes/clears the var somewhere between the population of the var and the bolded line near the bottom.
Basically once a var has been assigned a value that value remains in the var until it is either cleared/changed via code or goes out of scope.
-
Dec 5th, 2019, 03:13 PM
#8
Thread Starter
New Member
Re: Re using variables ?
this might be stupid question what should I be looking for ? to see if some other parts of code might be clearing it ?, Nothing should be changing it.
-
Dec 5th, 2019, 03:15 PM
#9
Re: Issues with variable passing ?
Because you didn't enclose your code in code tags it is very difficult to follow the flow of the various loops and if conditions properly.
You also haven't made it clear where the first snippet of code resides and where the second snippet of code resides.
Given those issues, I (and many others here) wouldn't normally bother to try to help because my time would likely be spent asking you for clarification, you providing clarification that possibly needs more clarification, me asking for that further clarification, etc. Which turns into a time sink that I'm not interested in getting in to on this site.
But since you are new, I'll give you the benefit of the doubt that you'll pick up on how to post a question in a way that helps us help you the next time you have a question.
All that being said, about the only thing I can suggest given your post above is to try changing this:
Code:
Do Until objFile.AtEndOfStream
strData = ""
to this:
Code:
strData = ""
Do Until objFile.AtEndOfStream
I can see no reason why you would be clearing out strData each time through the loop if you want it to keep getting appended to throughout the course of that initial block of code you posted.
Good luck.
-
Dec 5th, 2019, 03:18 PM
#10
Re: Re using variables ?
Those first 3 lines in the bottom piece appear to be calling sub routines or functions, it is possible that there is something in one of those that is changing the value.
You should check to see if there is a value in the var right after the loop statement.
Been ages since I did anything with vbscript not sure if it has the msgbox statement or not. If it does then I would suggest using msgbox to display the value of that var at various points after the loop to see if there is a value and when that value is lost.
-
Dec 5th, 2019, 06:29 PM
#11
Hyperactive Member
Re: Re using variables ?
What does your input file look like? If the matching line isn't the last one in the file, then that loop will clear out the strData variable anyhow. Also your check for MoreData will never work since that Mid function is only asking for a single character and you would need 9 for a match.
-
Dec 5th, 2019, 07:18 PM
#12
Re: Re using variables ?
Good catch on the variable being cleared in the loop. When I saw the code there was no indents and I missed that the line that cleared it was inside the loop.
-
Dec 6th, 2019, 08:48 AM
#13
Re: Issues with variable passing ?
How many threads do you need on the same thing? And in both threads you were warned about posting code wihout the [code][/code] blocks...
[
-tg
Last edited by Shaggy Hiker; Dec 6th, 2019 at 01:08 PM.
Reason: Removed no longer valid link
-
Dec 6th, 2019, 01:09 PM
#14
Re: Reading from Text file
I merged the three together so that there is one discussion on the topic. I now understand why there were three threads, too, or at least I think I do. The first thread was older, and incomplete. I can understand re-asking the question in the more complete form. Then I assume you lost the thread and started a third. Now they are all in one place.
Adding CODE tags makes formatting much nicer.
My usual boring signature: Nothing
-
Dec 9th, 2019, 01:54 PM
#15
Thread Starter
New Member
Re: Reading from Text file
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|