Consulting

Results 1 to 10 of 10

Thread: Setting FIle Name when splitting document

  1. #1

    Setting FIle Name when splitting document

    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")

  2. #2
    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

  3. #3
    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

  4. #4
    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.

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    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.

  7. #7
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    What does the message box say?
    And what does the (Saveas) line of code look like?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  8. #8
    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.

  9. #9
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hmmm......

    I think the line ...[vba]MsgBox (MyFileName)[/vba]

    might have something to do with it
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Do you think?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •