Click here to Skip to main content
14,869,580 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have master word document which contains sample payslips pagewise and i want split each page and save it as separate word document based on employee code mentioned in the particular word document page.

I have written VBA code for this but this is not working , kindly help me in this


I am getting error message and not getting proper output.
Run-time error 4605 this method or property is not available because no text is selected

What I have tried:

Private Sub CommandButton1_Click()
Application.Documents.Open ("C:\Users\shankrayya.g\Desktop\Test\Payslip.docx")
Application.ScreenUpdating = False
Application.Browser.Target = wdBrowsePage
For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")
ActiveDocument.Bookmarks("\page").Range.Copy
Documents.Add
Selection.Paste
Selection.TypeBackspace

Dim SSS As String
SSS = ActiveDocument.Range.Paragraphs(20).Range.Text
MsgBox SSS
SSS = Replace(SSS, Chr(13), "")
SSS = "C:\Users\shankrayya.g\Desktop\Test\Output\file" & SSS & ".doc"
MsgBox SSS
ActiveDocument.SaveAs FileName:=SSS
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges

End Sub
Posted
Updated 3-Sep-19 8:10am
Comments
Richard MacCutchan 3-Sep-19 9:23am
   
The message is quite clear. You are trying to manipulate some text but you have not selected any. Use the macro debugger to find out what is happening.
Shankrayya R 3-Sep-19 9:26am
   
Ok. Thanks for your input let me check that .. But I am not able to get the Proper Output.. Can you please suggest on this.

1 solution

Quote:
Run-time error 4605 this method or property is not available because no text is selected

If you read carefully the error message, it tells you why there is an error. Chances are that it also tells you the line where error occurs, chances are that it also propose to open the debugger.

Advice: As you are debugging your code, keep screen update to see what is going on.
VB
Private Sub CommandButton1_Click()
    Stop ' to switch to debugger mode
    Application.Documents.Open ("C:\Users\shankrayya.g\Desktop\Test\Payslip.docx")
    ' Make next line a comment while you are debugging code
    ' Application.ScreenUpdating = False
    Application.Browser.Target = wdBrowsePage
    For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")
        ActiveDocument.Bookmarks("\page").Range.Copy
        Documents.Add
        Selection.Paste
        Selection.TypeBackspace

        Dim SSS As String
        SSS = ActiveDocument.Range.Paragraphs(20).Range.Text
        MsgBox SSS
        SSS = Replace(SSS, Chr(13), "")
        SSS = "C:\Users\shankrayya.g\Desktop\Test\Output\file" & SSS & ".doc"
        MsgBox SSS
        ActiveDocument.SaveAs FileName:=SSS
    Next i
    ActiveDocument.Close savechanges:=wdDoNotSaveChanges

End Sub

Chances are also that ActiveDocument switch as you Add a new document.
-----
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
Indentation style - Wikipedia[^]
In excel, I use an addin "Smart Indenter v3.5", lets guess it works the same with word.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900