1

enter image description hereThis code i used to create binary file:

dim eFile as FolderItem
dim output as BinaryStream
eFile= GetSaveFolderItem("application/vnd.ms-word", "mydocument.doc")
output.write  "User Name: "+" User Name field"+chr(13)
output.write  "Website: "+" Website field"+chr(13)
output.close

I want to insert table format in that word document.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • If anybody have any idea about this, how to create or have some material or reference link. Please provide here. Thanks in adv.. – Amol Deshpande May 15 '13 at 12:41
  • On Windows I have used below code to create table in word document. dim wrdApplication As WordApplication dim docDocument As WordDocument dim tblWordTable As WordTable – Amol Deshpande May 15 '13 at 13:06
  • Please specify the version of Word you like to support. I found, for instance, that scripting Word 2004 is different from Word 2008, sadly. – Thomas Tempelmann May 15 '13 at 22:54

2 Answers2

3

Here's more details on how to do this for Mac OS X:

Get a little acquainted with the app "AppleScript Editor".

Enter the following script into a new script window:

tell application "Microsoft Word"
    set d to active document
    get content of text object of d
end tell

If you have a text document open in Word, and then you run this script, it should print the plain text of it in the Result pane below the script code.

In the same way you can alter the text in the Word document:

tell application "Microsoft Word"
    set d to active document
    set content of text object of d to "new text"
end tell

That's the basics to read and write text in Word via Applescript. Do learn how to add text to existing text or to change fonts etc, you need to read the "Dictionary" of Word, which you can open in the AppleScript Editor, see the File menu.

For more help with AppleScript, visit http://www.macscripter.net

Now, to use this with REALbasic, create this script:

on run {newContent}
    tell application "Microsoft Word"
        set d to active document
        set content of text object of d to newContent
    end tell
end run

Save it as a script file (file extension .scpt), e.g. as "SetContentInActiveWordDocument.scpt"

Then drag this script file into your REALbasic project, it'll appear as an item named "SetContentInActiveWordDocument" in italics.

You can now call this SetContentInActiveWordDocument like a function, passing a String to it.

E.g, write:

eFile.Launch() // this should open an existing word file if it exists in "eFile"
SetContentInActiveWordDocument("the new text") // replaces the text in the opened word file

To create a table in a word document, things get more complicated, though. You can pass only strings and numbers to a script from RB this way, so if you have an array of values you want to place into a table, you'll have to convert the array values into a string with unique separators, pass this string to the script, in which the string then gets split into the individual fields again.

It also helps immensely if you purchase "Script Debugger" - that not only lets you single step through scripts but it also provides an "Explorer" that lets you see all the values of a running application, making it much easier to figure out what you want to access. I used Script Debugger to see how to get to the content just by using its Explorer. Without this, you'd have to figure it out from the Scripting Dictionary, which is usually quite difficult if you're not experienced in it.

(later)

I attempted to add or even just read a table with Word 2008. I can't get anything to work that references tables in the "active document", despite finding several examples on the 'net that suggest that this should work, like this:

tell application "Microsoft Word"
    get table 1 of active document
end tell

I can't tell if this is a general problem with Word 2008 or with my particular installation. And I have no other versions to test with, sorry. This leaves you to google yourself for examples like this and see if you can make sense of it. In any case, first develop and test your code in Script Editor before trying to make it work with REALbasic.

Also, google for "Word 2004 AppleScript Reference", which is a fairly comprehensive guide on using Word with AppleScript.

Thomas Tempelmann
  • 11,045
  • 8
  • 74
  • 149
  • Thanks Thomas for your information, But I need more information and i'm using openOffice on MAC. Do you have any idea, how to create table format in doc file on MAC OS. Please share your information. Thanks – Amol Deshpande May 22 '13 at 09:29
  • Sorry, there's no way to do this with a .doc format file without having MS Word installed because that format is way too complex in order to write that generically. Instead, look into writing to an "open" document format, such as RTF. That can be read and written by many "Word"-like programs. Lastly, please learn to spell "Mac" correctly. It's not an acronym, it's an abbreviation, and thus is _not_ written in all caps. – Thomas Tempelmann May 22 '13 at 09:54
  • amol said that he wasn't doing this "manually", but using OpenOffice. I unfortunately don't know to what extent OpenOffice supports OS X automation. It's certainly possible to write an OO.org BASIC script that does it. Probably all one needs is a way to tell OO.org at runtime to run that script. – Kuba hasn't forgotten Monica Aug 29 '14 at 22:45
1

Real Studio can talk natively to MS Word and Excel but not on the Mac side (or at least in modern versions of Real Studio and MS Office).

To do the same thing on the Mac via Real Studio you will have to learn how to do it via AppleScript which does work through Real Studio. I am not an Applescript developer so I can't help you there.

BKeeney Software
  • 1,299
  • 6
  • 8