Writing to current Word document

K

kanwal

Hello,

Can anyone tell me how can i write a variable to a currently open word
document ?

Also, how can i know the filename of the currently open document ?

kanwal
 
P

Perry

? activedocument.Name
Document3

or

? documents(1).FullName
D:\data\PPC\another.doc

Krgrds,
Perry
 
J

Jonathan West

kanwal said:
thanks alot , i am using now Name....but it shows extension also? anyway
to
get rid of it (other then instr/left)

I would recommend that if you are going to need to strip the extension
frequently, then write a small function that does just that, rather than
using the string manipulation functions every time. I have a variety of such
functions in a library, for stripping the extension, for returning only the
extension, for getting the path, for removing the path part etc. All tested
and I know they work, so much less opportunity for errors to creep in. I
would recommend you do the same.

By the way, Perry didn't answer the other part of your question - how to
write the contents of a variable into the current document. This depends on
*where* in the document you want it written, but if you want it inserted at
the current selection point, then it is as simple as

Selection.Typetext myString

where myString is a string variable containing the text you want to insert.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jonathan West

perry said:
? activeworkbook.name
Book2.xls

? split(activeworkbook.name, ".")(0)
Book2


No. I would not recommend that approach. If the filename has a period in it,
you will split at the first period in the filename, not at the period
separating the filename from the extension. Do this instead.

Function StripExtension(sFile as String)
StripExtension = Left$(sFile , InstrRev(sFile , ".") - 1)
End Function

? StripExtension(ActiveDocument.Name)


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
P

Perry

Hi Kanwal,

Here's another "what if" scenario.
Function is setup to process any Filename proposal, passed to the
function... ok?

What if target file doesn't exist ?

Try to rewrite the function, circumventing this ...

Good luck,
Perry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top