SaveAs problems...

D

Domestos

Hi all,

I assume what i need to do is very simple, but i have not got a clue how to
do it. I have tried various things with no luck...

What I need to do is this...

I have a 120 page document, I need to copy each page in turn and paste it
into a new document. This I have managed to do...

I then need to save that new document with a dynamic filename. The filename
will be taken from the first word of the document. I have tried the
following but keep getting an error (expected function or variable)

Please help, and many thanks in advance...

eg.

Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend ' selects
first word in document
Selection.Copy ' copies selection
myDocname = Selection.Paste ' trying to paste my selection into a
variable
myDocname = myDocname & ".doc" ' appends '.doc' to variable
ActiveDocument.SaveAs FileName:=myDocname, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
ActiveWindow.Close


Domestos
 
J

Jezebel

Not sure what you think is happening with the selection object here, but a
simpler method would be:

Dim pFileName as string

pFileName = ActiveDocument.Words(1) & ".doc"
: (some work to do here)
ActiveDocument.SaveAs FileName:=pFileName

Before saving you'll need to check a) That you've ended up with a valid
filename (Word's concept of "Word" is a little odd and includes punctuation
marks. Documents don't normally begin with a punctuation mark, but you never
know...)

b) Check for invalid characters

and c) Check for uniqueness. With 120 documents I would expect a proportion
of them to have 'The' as the first word.
 
P

Peter Hewett

Hi Domestos

Try the following code:

Public Sub SaveAsFirstWordOfDoc(ByVal strPath As String)
Dim strFileName As String

strFileName = Trim$(ActiveDocument.Words(1))
strFileName = strPath & strFileName & ".doc"
ActiveDocument.SaveAs strFileName, wdFormatDocument, False, "", _
False, "", False, False, False, False, False
ActiveDocument.Close wdDoNotSaveChanges
End Sub

It saves the active document using the the first word of your document as a
name. All you need to do is pass in the path to where you want the document
saved.

But beware if the first paragraph of your document is blank then so is your
file name. Also, it does not check for duplicate file names.

I presume there's some logic to you methodology that prevents this problem
and generates meaningful document names.

HTH + Cheers - Peter
 
J

Jean-Guy Marcil

Hi Domestos,

To add to what Jezebel said:

To select the first word in a document:
and to take care of some problems (not all of them... it is not as simple as
it looks!, as Jezebel pointed out...)
'_______________________________________
Dim FirstWord As String
Dim WordIndex As Long

WordIndex = 1

'For example here are two scenarios
'Spaces at beginning and non-breaking spaces
'If document starts with spaces, then the first word
'is a bunch of spaces
'You do not want that as a file name!
'in that case, pick up the second word
If Trim(ActiveDocument.Words(1)) = "" Then
WordIndex = 2
End If

'If there is a non-breaking space (Chr(160) after
'the first word, it will end up as part of your
'file name, you do not want that either
'so get rid of it in case...
FirstWord = Trim(Replace(ActiveDocument.Words(WordIndex), _
Chr(160), ""))
'_______________________________________
Trim is necessary otherwise FirstWord will include a trailing space as a
word in Word always includes the trailing space (the only problem is if the
first word is followed by a trailing non-breaking space, that will not be
eliminated by the trim function, thus the replace function (Which will not
Work with Word 97... but you are not stating what Word version you are
using.)

Also, what if the first "word" is a comma?
Selection.Copy ' copies selection Yes, from the document
myDocname = Selection.Paste ' trying to paste my selection into a
variable
No, it pastes the previously copied stuff at the insertion point back in the
document. Try selecting a code word in the VBE window and hitting F1, you
will learn a lot hat way.

What you want is something like:
ActiveDocument.SaveAs FileName:=FirstWord & ".doc

I hope you have enough to get going!
HTH

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jezebel

chr(160) is not the only character that can cause problems in a filename.
Also: /\?*&@`!'.:,() Chr(34) chr(11). There are also some high-range unicode
characters (can't remember which ones) that map to these characters.
 
J

Jean-Guy Marcil

That's right,

That's why I wrote "For exemple..." near the beginning of my posted code.
;-)

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
I

Ian

Jezebel said:
chr(160) is not the only character that can cause problems in a filename.
Also: /\?*&@`!'.:,() Chr(34) chr(11).

As it happens, I have just completed a procedure that looks for
characters that can cause problems when programmatically building
Windows filenames.

However, my list of bad characters does *not* include any of the
characters you mention, except "/", "\", "?", "*", ":", Chr(34) and
Chr(11). What problems do the remaining characters in your list cause?
The Windows 98 system here, for example, does not object to saving a
document with the name &@`!'.,().doc

But, my list *does* include ">", "<", "|", and vbTab as invalid filename
characters.
 
D

Domestos

Thanks 2 u all,

This method works a treat. The first word in the document will always be a
single word that corrosponds to the content without any putuation or funny
characters so i dont need the formattings stuff...

Thanks
Again...
Domestos
 

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