This post is referring to a Word problem, but I'm sure the same applies to Excel.
My code is used to create individual .doc file that match a specific criteria (i.e. : a region in the table within my word document is equal to "HIGH RISK") from a .doc merge mail file that contains all my records in a single file.
Here is the code :
The thing is I noticed that some records are not saved as single files because the name is too long. First, I would like to know what are the filename length restriction in windows. I think my file is not created because the filename + the path to it is too long. The longest docname record that gets created has 33 characters.
Also, is it ok to write something like this:
Finaly I would like to delete the file "temp.doc" that gets created by SaveTempCopy macro. How do I write that?
Your help is really appreciated.
My code is used to create individual .doc file that match a specific criteria (i.e. : a region in the table within my word document is equal to "HIGH RISK") from a .doc merge mail file that contains all my records in a single file.
Here is the code :
Code:
Sub high_risk()
On Error GoTo errhandler
SaveTempCopy
Dim Letters As Integer, Counter As Integer
Letters = ActiveDocument.Sections.Count
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
docname = "HIGH RISK - " & Left(ActiveDocument.Tables(1).Cell(Row:=2, Column:=1).Range.Text, Len(ActiveDocument.Tables(1).Cell(Row:=2, Column:=1).Range.Text) - 2)
risk = Left(ActiveDocument.Tables(1).Cell(Row:=2, Column:=2).Range.Text, Len(ActiveDocument.Tables(1).Cell(Row:=2, Column:=2).Range.Text) - 2)
ActiveDocument.Sections.First.Range.Cut
If risk = "HIGH RISK" Then
Documents.Add Template:="C:\test\scorecard_template.dotx"
Selection.Paste
ActiveDocument.Sections(2).PageSetup.SectionStart = wdSectionContinuous
ActiveDocument.SaveAs FileName:="C:\test\" & docname, FileFormat:=wdFormatDocument
ActiveWindow.Close
End If
Counter = Counter + 1
Wend
ActiveDocument.Close SaveChanges:=False
errhandler: Exit Sub
End Sub
Sub SaveTempCopy()
ActiveDocument.Save
Application.Documents.Add ActiveDocument.FullName
ActiveDocument.SaveAs FileName:="temp"
End Sub
The thing is I noticed that some records are not saved as single files because the name is too long. First, I would like to know what are the filename length restriction in windows. I think my file is not created because the filename + the path to it is too long. The longest docname record that gets created has 33 characters.
Also, is it ok to write something like this:
Code:
If Len(docname)> 30 Then docname = left(docname, 30)
Finaly I would like to delete the file "temp.doc" that gets created by SaveTempCopy macro. How do I write that?
Your help is really appreciated.
Last edited: