PDA

View Full Version : Solved: how do do i get my string to appear on the word document



k13r4n
12-01-2008, 12:00 PM
hi everyone

i have some experience using VB Script in web development but im completely new to VB for applications, and ive got stuck on what is probably a really daft thing.

I have a simple loop that grabs some info out of a table in excel and arranges it into a text string to use as a script for AutoCAD. That all works fine, the problem is i need the string to goto word so that it can be saved as a plain text file.

Ive managed to work out how to open a new word document by reading through the forums here but i cant work out how to put the string onto the page! :banghead:

Most examples talk about opening templates and using bookmarks but i dont want to do any of that i just want a string of text dumped on the page.

ive been searching the forums and the net for 2 days now and cant find out how, if anyone can help or point me to an artical i can read that would be great.

here is a snippit
Ive opened the new document
Set ObjWord = CreateObject("Word.application")
ObjWord.Visible = True
ObjWord.documents.Add

and i have my string

"rectang " & xStart & "," & yStart & " @" & pLength & "," & pWidth


Whats the bit of code i need to put infront of that string to make it display on the word document? I can get it to output to a cell in excel just fine but i cant get it onto the new word document :banghead:

Cheers

Kieran

Bob Phillips
12-01-2008, 12:09 PM
You can write to a text file in Excel, no need for Word.



Dim FileNumber

FileNumber = FreeFile
Open "TEST" For Output As #FileNumber
Write #FileNumber, "This is a sample."
Close #FileNumber

Bob Phillips
12-01-2008, 12:16 PM
Or better,



Dim FileNumber
FileNumber = FreeFile
Open "TESTxxxxx.txt" For Output As #FileNumber
Print #FileNumber, "This is a sample."
Print #FileNumber, "This is another sample."
Close #FileNumber

k13r4n
12-01-2008, 12:31 PM
Or better,



Dim FileNumber
FileNumber = FreeFile
Open "TESTxxxxx.txt" For Output As #FileNumber
Print #FileNumber, "This is a sample."
Print #FileNumber, "This is another sample."
Close #FileNumber


Thanks xld
Could you just explain FileNumber = FreeFile bit? and why is there a # infront of FileNumber?

cheers

Bob Phillips
12-01-2008, 12:51 PM
That is just the syntax. FreeFile is a VBA function that returns the next file number slot available. This is passed to a vraibale which is used when open ing and printing the text file. It is probably making life simple for the compiler.

k13r4n
12-01-2008, 01:14 PM
Thanks xld, got it all sorted
:thumb