Setting File Name for Save As

markvenis

Board Regular
Joined
Sep 17, 2003
Messages
60
I am trying to save mail merge file and using the code below, the only problem I have is when I try and return the first line (a 6 digit number) of the document as the Filename.

I get a run time error due to a file permission error.

------------

Sub AllSectionsToSubDoc()

Dim x As Long
Dim Sections As Long
Dim Doc As Document

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Set Doc = ActiveDocument
Sections = Doc.Sections.Count
For x = Sections - 1 To 1 Step -1
Doc.Sections(x).Range.Copy
Documents.Add
ActiveDocument.Range.Paste
ActiveDocument.SaveAs (Doc.Path & "\" & Doc.Paragraphs(1).Range.Text & ".doc")
ActiveDocument.Close False
Next x

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Sometimes VBA doesn't like doing too much in one line.
I would first break it down and check that the filename being produced is correct with something like :=
Code:
    Dim MyFileName As String
    MyFileName = Doc.Path & "\" & Doc.Paragraphs(1).Range.Text & ".doc"
    MsgBox (MyFileName)
    ActiveDocument.SaveAs FileName:=MyFileName

If the filename is correct and there is no other reason for the error (not met this one) I would suspect a corrupted module. Clean the code by copy/pasting to Notepad, replace with a new module and copy/paste code back again.
 
Upvote 0
Cheers, have tried it and it works if I set the filename as a different value i.e. x which returns the document number in the string but won't work when I try to return the value I want!

Each document has an 8 digit employee number on the first line, the only value on the first line. I am trying to return this as the filename.

I have tried returning the first paragrpah and first sentence but doesn't like it and not too sure how I could go about returning the first 8 digits in the document - sure it is a schoolboy solution just can't seem to get it!
 
Upvote 0

Forum statistics

Threads
1,214,573
Messages
6,120,318
Members
448,956
Latest member
Adamsxl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top