VBA Word 2003

V

VBA

Here is a macro which splits document, and there are a file permission error.
So how can I solve this error.


Sub SplitDoc()

Dim rng As Range
Dim c As Long
Dim i As Integer
Dim pavadinimas As String
Dim Show As Boolean

Show = ActiveDocument.ActiveWindow.View.ShowHiddenText
If Not Show Then ActiveDocument.ActiveWindow.View.ShowHiddenText = True
pavadinimas = ActiveDocument.Paragraphs.First.Range.Text

Set rng = ActiveDocument.Range
rng.Collapse wdCollapseStart
Do
c = rng.MoveEndUntil(Chr$(12), wdForward)
If c = 0 Then
rng.End = ActiveDocument.Range.End
Else
If rng.Paragraphs.First.Range.Characters.First = Chr$(12) Then
pavadinimas = Mid$(rng.Paragraphs.First.Range.Text, 2)
Else
pavadinimas = rng.Paragraphs.First.Range.Text
End If

rng.Start = rng.MoveStartUntil(Chr$(13), wdForward)
rng.Copy
Documents.Add.Range.Paste
i = i + 1
ActiveDocument.SaveAs fileName:=pavadinimas
ActiveDocument.Close
rng.MoveEnd wdCharacter, 1
rng.Collapse wdCollapseEnd
End If
Loop Until c = 0
ActiveDocument.ActiveWindow.View.ShowHiddenText = Show
End Sub
 
D

Doug Robbins - Word MVP

I would rewrite the code as follows:

Sub SplitDoc()

Dim rng As Range
Dim c As Long
Dim i As Integer
Dim pavadinimas As String
Dim Show As Boolean
Dim Source as Document, Target as Document

Set Source = ActiveDocument

Show = Source.ActiveWindow.View.ShowHiddenText
If Not Show Then Source.ActiveWindow.View.ShowHiddenText = True
pavadinimas = Source.Paragraphs.First.Range.Text

Set rng = Source.Range
rng.Collapse wdCollapseStart
Do
c = rng.MoveEndUntil(Chr$(12), wdForward)
If c = 0 Then
rng.End = Source.Range.End
Else
If rng.Paragraphs.First.Range.Characters.First = Chr$(12) Then
pavadinimas = Mid$(rng.Paragraphs.First.Range.Text, 2)
Else
pavadinimas = rng.Paragraphs.First.Range.Text
End If

rng.Start = rng.MoveStartUntil(Chr$(13), wdForward)
rng.Copy
Set Target = Documents.Add
Target.Range.Paste
i = i + 1
Target.SaveAs fileName:=pavadinimas
Target.Close
rng.MoveEnd wdCharacter, 1
rng.Collapse wdCollapseEnd
End If
Loop Until c = 0
Source.ActiveWindow.View.ShowHiddenText = Show
End Sub

Your problem could be the system losing track of the ActiveDocument, which
the above modification will prevent.

If not, what line of code is highlighted if you click on Debug.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
V

VBA

When I click on Debug the line is highlighted Targer.SaveAs
fileName:=pavadinimas


Arnoldas Rope
 
D

Doug Robbins - Word MVP

And what does pavadinimas contain at that point?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
V

VBA

Pavadinimas contains first line after page break, I want that don't copy to
pavadinimas enter symbol.so could you tell me what i need to change in macro?
 
D

Doug Robbins - Word MVP

Declare pavadinimas as a Range, then use

Set pavadinimas = Source.Paragraphs.First.Range
pavadiminas.End = pavadinimas.End - 1


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top