Word 2010
I hope I can describe this well enough for you to follow.
The Word application I am enhancing is used to create reports. The user can tell Word to "protect" the existing document which it appears it does by by adding a Section Break(Next Page) to the end of the document, then protecting the document, preserving all the existing content and puts the cursor on Page 2 for instance. Then anything that is added goes on Page 2. If the cursor is put in page 1 it jumps to Page 2. This is all being done automatically, the user does not manually insert the Section Break(Next Page). After the section is protected the section now says "end of Protected Section". This is being done by the add-in I did not write.
If I add another section(CreateSection) to page 2 of the "protected document" it works fine. I can put whatever I want in the newly added section. If I now choose to unprotect(UnProtectFORM)) the document and delete the section(GetAndRemoveSectionByInstanceNumber) I added with the code listed below and protect document(ProtectFORM), the Section Break(Next Page) that was on Page 1 seems to convert to Section Break(Continuous) so now my cursor can be on Page 1 which is undesirable.
I have put in parentheses all the code names I used. They are listed below.
------------------
Code:
Sub CreateSection()
' Called by InsertSelectedSynopticReportAtCursor
Dim strImageTag
System.Cursor = wdCursorWait
' Step #0, test
' Selection.InsertBreak Type:=wdSectionBreakContinuous
strImageTag = "<ImageTable: efrm" & ReportInstance & ">[EmbeddedReport]efrm goes here[/EmbeddedReport]"
' Step #1
Selection.InsertBreak Type:=wdSectionBreakContinuous
' Step #2
Selection.Font.Hidden = True
Selection.TypeText (strImageTag)
Selection.Font.Hidden = False
' Step #3
Selection.InsertBreak Type:=wdSectionBreakContinuous
End Sub
Code:
Sub UnProtecteFRM()
Application.ScreenUpdating = False
ActiveWindow.View.ShowHiddenText = True
System.Cursor = wdCursorWait
Set aDoc = ActiveDocument
If aDoc.ProtectionType <> wdNoProtection Then
' MsgBox ("UnProtecteFRM -Get Password")
aDoc.Unprotect Password:="PwrMcW3pzf6"
End If
With Selection.Find
.ClearFormatting
.Text = "ImageTable: efrm"
.Replacement.ClearFormatting
.Replacement.Text = "ImageTable efrm"
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
ActiveWindow.View.ShowHiddenText = False
Application.ScreenUpdating = True
End Sub
Code:
Sub GetAndRemoveSectionByInstanceNumber()
' done with Macropod's help on vba express forum
' http://www.vbaexpress.com/forum/show...ins-my-keyword
' 12/01/2013, dla
' 12/03/2013, dla
' there is an assumption that the document is unprotected.
' we need to see if this works if the document is protected.
Dim strSearchString As String
intSectionCount = -1
' these 2 lines are not necessary(?)
Application.ScreenUpdating = False
ActiveWindow.View.ShowHiddenText = True
strSearchString = "Inst#: " & ReportInstance
System.Cursor = wdCursorWait
' start at top of document
Selection.HomeKey Unit:=wdStory
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = strSearchString
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
If .Find.Found = True Then
.End = .Duplicate.Sections(1).Range.End
.Start = .Duplicate.Sections(1).Range.Start - 1
.Duplicate.Text = vbNullString
End If
End With
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ActiveWindow.View.ShowHiddenText = False
Application.ScreenUpdating = True
Application.ScreenRefresh
End Sub
Code:
Sub UnProtectFORM()
Application.ScreenUpdating = False
ActiveWindow.View.ShowHiddenText = True
System.Cursor = wdCursorWait
Set aDoc = ActiveDocument
If aDoc.ProtectionType <> wdNoProtection Then
aDoc.Unprotect Password:="PwrMcW3pzf6"
End If
With Selection.Find
.ClearFormatting
.Text = "ImageTable: efrm"
.Replacement.ClearFormatting
.Replacement.Text = "ImageTable efrm"
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
ActiveWindow.View.ShowHiddenText = False
Application.ScreenUpdating = True
End Sub
Code:
Sub ProtectFORM()
Application.ScreenUpdating = False
ActiveWindow.View.ShowHiddenText = True
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Text = "ImageTable efrm"
.Replacement.ClearFormatting
.Replacement.Text = "ImageTable: efrm"
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
'If OriginalProtection <> wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="PwrMcW3pzf6"
' MsgBox ("ProtecteFRM")
' End If
ActiveWindow.View.ShowHiddenText = False
Application.ScreenUpdating = True
End Sub