Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

read the input of a .txt file into a variable in my vba macro script

Hi @All,

i want to read the input of a .txt file into a variable in my vba macro script.

In line 2 the fso Object is empty ? I don't know why ? So the rest of my script can' work, because i need the fso Object

  1. fso = CreateObject("Scripting.FileSystemObject")
  2. 'msgbox ("FSO: " & fso)
  3. 'dateipfad = "path_and_name.txt"
  4. 'fsoFile = fso.opentextfile(dateipfad, fsoForReading)
  5. 'inhalt = Split(fsoFile.ReadAll, vbCr)
  6. 'inhalt = fsoFile.ReadAll
  7. 'msgBox(inhalt)
  8. 'fsoFile.Close
  9. 'fsoFile = Nothing
1 Solution

Accepted Solutions
Not applicable
Author

Ok thanks, i have the solution.

So I didn't knew that there are a difference between VBA and VBScript.

Like Mr. Zeeman explains, the QlikView Macro only supports VBScript.

So in the past i had VBA Commands in my code, and so it doesn't function.

Here my solution

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForReading)

Do Until objFile.AtEndOfStream

    strSearchString = objFile.ReadLine

Loop

objFile.Close

View solution in original post

10 Replies
Not applicable
Author

Try

SET fso = CreateObject("Scripting.FileSystemObject")

Object assignment needs the SET operator

Not applicable
Author

I have set the Set operator before, but it also doesn't work.

I'm testing it at the moment wit the set operator, but it also doesnt' work.

tresesco
MVP
MVP

Creating object is like creating a placeholder. Unless it holds something magbox would not probably be able to show that. Try without those message boxes.

Not applicable
Author

Alternatively, you can read the file in the Qlikview LOAD script in stead of in a macro.

If you do need the value in a macro you can access the script variable like this

set MyVar = ActiveDocument.Variables("vName")

jerrysvensson
Partner - Specialist II
Partner - Specialist II

Have you tried changing the security settings in the macro dialog to Allow system Access?
Or are you trying to achieve this outside QV?

Not applicable
Author


yes the script runs with Allowing system Access

Not applicable
Author

I have tried without msgBox, but it also doesn't work

Not applicable
Author

Do you have maybe an example or tutorial, where i can read a txt file into a variable in vba macro of qlikview. Because i have the experience, that the vba macro is not the same as the "normal" vba language.

Not applicable
Author

First in your LOAD script read the file and assign the value to a variable (I assume the file has just 1 value)

LOAD @1

FROM

C:\Temp\myvar.txt

(txt, codepage is 1252, no labels, delimiter is '\t', msq);

Let vVar = Peek('@1', -1);

Next, in your VBScript macro you can read the Qlikview variable:

Function GetVar

  set MyVar = ActiveDocument.Variables("vVar")

  'add your stuff here

End Function

Remember to always see if you really need a macro, you'd be amazed what you can accomplish with the - more robust and manageable - Qlikview load script.

By the way

the vba macro is not the same as the "normal" vba language.

Qlikview does not use VBA, it is VBScript. A small but essential difference!