yohanmcdonal
01-26-2018, 02:11 PM
I have been looking at fast tracking our file naming for our construction proposals. We have the naming format as a job number followed by a brief description of the project (ex. 01-0111-485 Test Project.docx)
The macro I am writing:
1. Goes down to the "Subject:" paragraph line #8
2. Grabs all the text on that paragraph line after "Subject:"
3. Converts that selection to a bookmark named "Description"
4. Goes down to the "SSi Project #:" paragraph line #9
5. Grabs all the text on that paragraph line after "SSi Project #:"
6. Converts that selection to a bookmark named "Job_Number"
7. Opens the FileSaveAs dialog box
8. Enters the bookmarks as the filename and lets me browse to the needed folder
Sub Proposal_SaveAs()ActiveDocument.Paragraphs(8).Range.Select 'selects project description
With Selection.Find
.Text = "Subject: "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
ActiveDocument.Bookmarks.Add _
Name:="Description", Range:=Selection.Range 'creates description bookmark from subject selection
ActiveDocument.Paragraphs(9).Range.Select 'selects job number
With Selection.Find
.Text = "SSi Project #: "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
ActiveDocument.Bookmarks.Add _
Name:="Job_Number", Range:=Selection.Range 'creates job number bookmark from job number selection
With Dialogs(wdDialogFileSaveAs) 'creates filename and opens save as dialog box
.Name = ActiveDocument.Bookmarks("Job_Number").Range.Text & " " & ActiveDocument.Bookmarks("Description").Range.Text
.Show
End With
End Sub
Everything works perfectly until I hit "Save". I keep getting "invalid filename" errors. I am pretty good with Excel VBA but new to Word VBA.
Can anyone help troubleshoot this?
The macro I am writing:
1. Goes down to the "Subject:" paragraph line #8
2. Grabs all the text on that paragraph line after "Subject:"
3. Converts that selection to a bookmark named "Description"
4. Goes down to the "SSi Project #:" paragraph line #9
5. Grabs all the text on that paragraph line after "SSi Project #:"
6. Converts that selection to a bookmark named "Job_Number"
7. Opens the FileSaveAs dialog box
8. Enters the bookmarks as the filename and lets me browse to the needed folder
Sub Proposal_SaveAs()ActiveDocument.Paragraphs(8).Range.Select 'selects project description
With Selection.Find
.Text = "Subject: "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
ActiveDocument.Bookmarks.Add _
Name:="Description", Range:=Selection.Range 'creates description bookmark from subject selection
ActiveDocument.Paragraphs(9).Range.Select 'selects job number
With Selection.Find
.Text = "SSi Project #: "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
ActiveDocument.Bookmarks.Add _
Name:="Job_Number", Range:=Selection.Range 'creates job number bookmark from job number selection
With Dialogs(wdDialogFileSaveAs) 'creates filename and opens save as dialog box
.Name = ActiveDocument.Bookmarks("Job_Number").Range.Text & " " & ActiveDocument.Bookmarks("Description").Range.Text
.Show
End With
End Sub
Everything works perfectly until I hit "Save". I keep getting "invalid filename" errors. I am pretty good with Excel VBA but new to Word VBA.
Can anyone help troubleshoot this?