VBA MS Word Question

Thecodont

Member
Joined
Mar 27, 2007
Messages
6
Programming Experience
Beginner
I am not sure if this is out of scope for this fourm but I am a newbee having trouble with a function. I am trying to paste text from the clipboard into a new MS word doc, then save the file as a new file using the first line of text as the file name. I keep getting a error saying that my name is not valid. Here is the code I wrote..any thoughts?

VB.NET:
Dim title As String
Dim newdoc As Word.Document
Word.Documents.Add
Selection.PasteAndFormat (wdFormatPlainText)
'select first line for name
    Dim lines As Range
    Set lines = ActiveDocument.Range( _
        Start:=ActiveDocument.Paragraphs(1).Range.Start, _
        End:=ActiveDocument.Paragraphs(1).Range.End)
    lines.Select
    lines.Copy
    title = lines
  
    ActiveDocument.SaveAs FileName:="D:\Documents and Settings\Doug.OFFICE\Desktop\" & title & ".doc"
 
I think the lines range may include the carriage return at the end of the line, which would be objectionable in a file name.

Dropping one character from the end of the range may work:
VB.NET:
End:=ActiveDocument.Paragraphs(1).Range.End-1
Also, the following may be a little simpler for assigning the file name:
VB.NET:
title = lines.Text
In lieu of:
VB.NET:
lines.Select
lines.Copy
title = lines
 
Thanks

Thanks for the help! That solved the problem. :) . Now I an finding that if the first liine in the document to be cut and pasted is blank then i also get an error. I suspect there is a grab first line command in VBA so will take a look
 
Back
Top