Literally Reading Word Files

rowix
rowix Member Posts: 27
Im having a problem with the following.

In VBA i can read a word document literally by using the word 11.0 automation and the folowing Code
    Dim wapp As Word.Application
    
    Set wapp = CreateObject("Word.Application")
    
    Word.Documents.Open (txtPath)
    
    txtNumberOfWords = (Word.ActiveDocument.Words.Count) - 1
    
    txtPath = Word.ActiveDocument.Words(1)

    Word.ActiveDocument.Close
This will result in the first word of the word document.

When i try to create this for navision it looks like this:
IF NOT ISCLEAR(WoApp) THEN
  CLEAR(WoApp);

CREATE(WoApp);

WoApp.Documents.Open(Path);

Spaces := WoApp.ActiveDocument.Words.Count;
Spaces -= 1; //Delete the begin Space

Var1 := WoApp.ActiveDocument.Words.Item(1);

WoApp.Documents.Close;

MESSAGE('%1',FORMAT(txt1));
In this code Path is the string to filename and Woapp is the word automation.

But this wil result in a crash ](*,) or a not usefull value :-k

When i change var1 from variant to text it results in a compile error text := automation

If anyone has any idea how i can get this to work i will be very :lol:

Answers

  • Dakkon
    Dakkon Member Posts: 193
    My advice is to write yourself a wrapper dll, I would recommend in VB. If you just need a word count then add a method for that, such as:

    GetWordCount(path as string) as int

    or in the case of the first word ..something like:
    GetFirstWord(path as string) as string

    Navision doesn't handle certain values to well, such as null. Also Navision doesn't allow you to proactively handle errors well either. I always recommend making your wrapper for such purposes and enclose the functionality into methods in your wrapper. Hope this helps.
    Thad Ryker
    I traded my sanity for a railgun :mrgreen:
  • Lin
    Lin Member Posts: 40
    Try changing you code to be like this ...
    IF NOT ISCLEAR(woApp) THEN
      CLEAR(woApp);
    
    CREATE(woApp);
    
    ParamFALSE := FALSE;
    ParamTRUE  := TRUE;
    ParamFile  := '..your file path..';
    
    woDoc := woApp.Documents.Open2000(ParamFile,ParamFALSE,ParamTRUE);
    
    Spaces := woApp.ActiveDocument.Words.Count;
    Spaces -= 1; //Delete the begin Space 
    
    Var1 := woDoc.Words.Item(1).Text;
    
    woApp.Documents.Close;
    woApp.Quit;
    
  • rowix
    rowix Member Posts: 27
    Lin wrote:
    Try changing you code to be like this ...
    IF NOT ISCLEAR(woApp) THEN
      CLEAR(woApp);
    
    CREATE(woApp);
    
    ParamFALSE := FALSE;
    ParamTRUE  := TRUE;
    ParamFile  := '..your file path..';
    
    woDoc := woApp.Documents.Open2000(ParamFile,ParamFALSE,ParamTRUE);
    
    Spaces := woApp.ActiveDocument.Words.Count;
    Spaces -= 1; //Delete the begin Space 
    
    Var1 := woDoc.Words.Item(1).Text;
    
    woApp.Documents.Close;
    woApp.Quit;
    

    lin Thanks for the help the .text; does the trick in my code but what kind of variable is woDoc I thought it was “automation.words” but that will not work.

    Kind Regards
    Peter