PDA

View Full Version : Setting FIle Name when splitting document



markvenis
11-11-2005, 03:11 AM
I have a large word mail merge file that contains 3000 or so letters (all 1 page), I have code to split and save the document but need to change the line of code that sets the file name to be the first line of text in the letter.

This line references an employee number I need to set it as the file name so that the documents can be filed/ scanned.

The code at the moment is

strNewFileName = Replace(docMultiple.FullName, ".doc", "_" & Right$("000" & iCurrentPage, 4) & ".doc")

markvenis
11-11-2005, 04:35 AM
I have amended the code to (doc single is the document)

strNewFileName = (docSingle.Sentences(1).Text & ".doc")

Which works up to the point where it tries to save and it gives me a run time error 5487, word cannot complete the save due to a file permission error .doc

Jay Freedman
11-12-2005, 12:47 PM
Hi markvenis,

This looks like a problem with the data coming from the document, not necessarily a problem with the code of the macro. Probably the text contained in docSingle.Sentences(1).Text includes one or more characters that aren't valid in a file name.

Single-step the code in the VBA editor by pressing F8 and watch the statements as they're highlighted. Immediately after it executes the statement you quoted, hover the mouse pointer over the variable strNewFileName to see what's in it. (Or right-click the variable and add it to the Watch window.) What's the value of the string? Does it contain any nonprintable characters shown as squares? Any punctuation that isn't valid in a file name?

Regards,
Jay

markvenis
11-24-2005, 03:22 AM
Thanks, have tried this but still a bit stumped. Having tried both approaches it appears that the line is not returning a value.

Each document has a numeric string of between 4 to 8 digits (employee numbers) on the first line, the only value on the first line of the document, that I would like to set as the filename.

There are no other values (spaces, punctuation etc) in this line.

TonyJollans
11-24-2005, 04:14 AM
If the first line contains a number and nothing else, the first sentence will contain the paragraph mark which could cause a problem. Can you use the first word, rather than the first sentence?

markvenis
11-24-2005, 06:22 AM
Many thanks - the macro is working now only remaining problem is getting rid of the message box that comes up when saving each document (has an OK button to click).

Have got in the macro -

Application.ScreenUpdating = False
Application.DisplayAlerts = False

In Excel would usually expect something like Application.EnableEvents = False, options such as wdAlerts do appear but don't seem to make much difference.

Am using Word 2003.

TonyJollans
11-24-2005, 09:06 AM
What does the message box say?
And what does the (Saveas) line of code look like?

markvenis
11-24-2005, 09:11 AM
The code to save is

MyFileName = Doc.Path & "\" & ActiveDocument.Words(1).Text & ".doc"
MsgBox (MyFileName)
ActiveDocument.SaveAs FileName:=MyFileName
ActiveDocument.Close False (as finishes splitting the rest of the mail merge).

The message box is a Microsoft Word pop up it displays the path and file name with an 'OK' button.

TonyJollans
11-24-2005, 09:45 AM
Hmmm......

I think the line ...MsgBox (MyFileName)

might have something to do with it :)

fumei
11-25-2005, 06:14 AM
Do you think?