Re: VBA Word 2003 by Doug
Doug
Mon Jun 16 01:33:33 PDT 2008
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
"VBA" <VBA @discussions.microsoft.com> wrote in message
news:E93BF4FB-C233-4F30-BAF6-124C46465D4A@microsoft.com...
> 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
>
>
>
>