none
Word VBA: getting Run-time error '5096' when trying to programmatically SaveAs RRS feed

  • Question

  • I emulated Doug Robbins' "splitter macro" but am having an issue trying to programmatically SaveAs. 

    In my mailmerge document I used merge fields to display a phone number and a document number in the first paragraph of each page, e.g., 9815048969547-190514-02788.

    I then use the statement: temp = ActiveDocument.paragraphs(1) to assign the phone+document number to a variable.  (I've tried making temp either a string or a variant but still get the 5096 error when trying to SaveAs.)  I then use temp in the following fashion:

    DocName = "\\servername\Users\Christian.Bahnsen\Desktop\ToDo Folder\word\splitTest\" & temp

    DocName displays fine in a MsgBox whether I dimensioned temp as a string or variant, but the script throws Run-time error '5096' in this statement:

    Target.SaveAs FileName:=DocName

    The following statement works fine when doing a save as:

    DocName = "\\servername\Users\Christian.Bahnsen\Desktop\ToDo Folder\word\splitTest\Page" & Counter

    I can't figure out why one fails and the other works.

    Here's the full code block:

    Sub splitDocumentToSeparatePages() ' splitter Macro ' Macro created 16-08-98 by Doug Robbins to save each page of a document 'https://answers.microsoft.com/en-us/msoffice/forum/all/converting-multiple-pages-in-a-single-word/7f72eb6a-1963-4052-b7dd-09045c184c79 'https://social.msdn.microsoft.com/Forums/office/en-US/1f2d95e7-2f8d-4928-a94c-eff448df01ee/vba-word-how-can-save-each-page-in-a-document-into-a-sepaterate-document?forum=worddev ' Dim Counter As Long Dim Pages As Long Dim Source As Document Dim Target As Document Dim temp As Variant Dim DocName As Variant Set Source = ActiveDocument Selection.HomeKey Unit:=wdStory Pages = Source.BuiltInDocumentProperties(wdPropertyPages) Counter = 0 While Counter < Pages Counter = Counter + 1 temp = ActiveDocument.paragraphs(1) DocName = "\\servername\Users\Christian.Bahnsen\Desktop\ToDo Folder\word\splitTest\" & temp 'the following statement works fine when uncommented 'DocName = "\\servername\Users\Christian.Bahnsen\Desktop\ToDo Folder\word\splitTest\Page" & Counter

    'the filename displays fine whether temp was a string or variant MsgBox DocName Source.Bookmarks("\Page").Range.Cut Set Target = Documents.Add Target.Range.Paste 'the following line throws the error Target.SaveAs FileName:=DocName Target.Close Wend End Sub


    Christian Bahnsen

    Monday, March 23, 2020 3:41 PM

Answers

  • 1. ActiveDocument.Paragraphs(1) has type "Paragraph". (you can find out using

    ?typename(ActiveDcoument.Paragraphs(1)) 

    in the Immediate window of the VB Editor

    2. What you really need is to start with

    ActiveDocument.Paragraphs(1).Range.Text
    
    3. But even that may cause problems because the final character of that string will be an ASCII 13 (CR), so you will probably need

    Left(ActiveDocument.Paragraphs(1).Range.Text,Len(ActiveDocument.Paragraphs(1).Range.Text)-1)
    

    In this case, because you do not appear to have any other characters that would cause problems in your paragraph's text, I think that is all you will need. Otherwise, you will need to strip out characters that are not allowed in file/path names.

    Peter Jamieson

    Wednesday, March 25, 2020 10:35 AM

All replies

  • 1. ActiveDocument.Paragraphs(1) has type "Paragraph". (you can find out using

    ?typename(ActiveDcoument.Paragraphs(1)) 

    in the Immediate window of the VB Editor

    2. What you really need is to start with

    ActiveDocument.Paragraphs(1).Range.Text
    
    3. But even that may cause problems because the final character of that string will be an ASCII 13 (CR), so you will probably need

    Left(ActiveDocument.Paragraphs(1).Range.Text,Len(ActiveDocument.Paragraphs(1).Range.Text)-1)
    

    In this case, because you do not appear to have any other characters that would cause problems in your paragraph's text, I think that is all you will need. Otherwise, you will need to strip out characters that are not allowed in file/path names.

    Peter Jamieson

    Wednesday, March 25, 2020 10:35 AM
  • Well done, sir!  Your code from #3 works like a charm.

    Thank you.

    Christian

    Thursday, March 26, 2020 6:13 PM