C# Snippets


InsertLandscape

Public Sub Page_LandscapeInsert(ByRef objDocument As Word.Document, _
ByVal sDocumentType As String, _
ByVal sDocumentTemplate_Code As String)

Dim objCurrentPageFit As Word.WdPageFit
Dim iCurrentViewPercentage As Integer

Dim objSection As Word.Section
Dim objSectionPrevious As Word.Section

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

objCurrentPageFit = gApplicationWord.ActiveWindow.ActivePane.View.Zoom.PageFit
iCurrentViewPercentage = gApplicationWord.ActiveWindow.ActivePane.View.Zoom.Percentage

gApplicationWord.ActiveWindow.ActivePane.View.Zoom.PageFit = Word.WdPageFit.wdPageFitFullPage

If modWordObjectModel.Section_CanBeChanged(objDocument, sDocumentTemplate_Code) = False Then
Exit Sub
Else
objSection = gApplicationWord.Selection.Sections(1)
gApplicationWord.Selection.MoveUp(Unit:=Word.WdUnits.wdLine, Count:=1)
objSectionPrevious = gApplicationWord.Selection.Sections(1)
gApplicationWord.Selection.MoveDown(Unit:=Word.WdUnits.wdLine, Count:=1)

If Not (objSectionPrevious.Index < objSection.Index) Then
gApplicationWord.Selection.InsertBreak(Type:=Word.WdBreakType.wdSectionBreakNextPage)
With gApplicationWord.Selection
.TypeParagraph()
.MoveUp(Unit:=Word.WdUnits.wdLine, Count:=1)
.Style = objDocument.Styles("B-Section Heading")
.TypeText(Text:="New Section")
.TypeParagraph()
.Style = objDocument.Styles("B-Heading 2. Full Width")
.TypeText(Text:="Style: B-Heading 2. Full Width")
.InsertBreak(Type:=Word.WdBreakType.wdSectionBreakNextPage)
.Delete()
.MoveUp(Unit:=Word.WdUnits.wdLine, Count:=1)

Call modWordObjectModel.Page_CheckOrientation(objDocument, sDocumentType, sDocumentTemplate_Code)

End With
Else
With gApplicationWord.Selection
.TypeParagraph()
.MoveUp(Unit:=Word.WdUnits.wdLine, Count:=1)
.Style = objDocument.Styles("B-Section Heading")
.TypeText(Text:="New Section")
.TypeParagraph()
'.MoveLeft Unit:=wdCharacter, Count:=1
'.TypeParagraph
.Style = objDocument.Styles("B-Heading 2. Full Width")
.TypeText(Text:="Style: B-Heading 2. Full Width")
.InsertBreak(Type:=Word.WdBreakType.wdSectionBreakNextPage)
.Delete()
.MoveUp(Unit:=Word.WdUnits.wdLine, Count:=1)

Call modWordObjectModel.Page_CheckOrientation(objDocument, sDocumentType, sDocumentTemplate_Code)
End With
End If
End If

Call modWordObjectModel.Page_LandscapeFormat(objDocument, sDocumentType, sDocumentTemplate_Code)

gApplicationWord.ActiveWindow.ActivePane.View.Zoom.PageFit = objCurrentPageFit
gApplicationWord.ActiveWindow.ActivePane.View.Zoom.Percentage = iCurrentViewPercentage

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"insert a landscape page at this point in the document." & _
System.Environment.NewLine & _
System.Environment.NewLine & _
"Please check the formatting in this section.")
End Try
End Sub

InsertPortrait

Public Sub Page_PortraitInsert(ByRef objDocument As Word.Document, _
ByVal sDocumentType As String, _
ByVal sDocumentTemplate_Code As String)

Dim objCurrentPageFit As Word.WdPageFit
Dim iCurrentViewPercentage As Integer
Dim objTargetSection As Word.Section

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

objCurrentPageFit = gApplicationWord.ActiveWindow.ActivePane.View.Zoom.PageFit
iCurrentViewPercentage = gApplicationWord.ActiveWindow.ActivePane.View.Zoom.Percentage

gApplicationWord.ActiveWindow.ActivePane.View.Zoom.PageFit = Word.WdPageFit.wdPageFitFullPage

If modWordObjectModel.Section_CanBeChanged(objDocument, sDocumentTemplate_Code) = False Then
Exit Sub
Else
With gApplicationWord.Selection
.Collapse(Word.WdCollapseDirection.wdCollapseStart)
.InsertBreak(Type:=Word.WdBreakType.wdSectionBreakNextPage)
.Style = objDocument.Styles("B-Section Heading")
.TypeText(Text:="Section Header")
.TypeParagraph()
.Style = objDocument.Styles("B-Heading 1")
.TypeText(Text:="Heading 1")

objTargetSection = gApplicationWord.Selection.Sections(1)

.TypeParagraph()
.TypeParagraph()
.InsertBreak(Type:=Word.WdBreakType.wdSectionBreakNextPage)

Call modWordObjectModel.Page_CheckOrientation(objDocument, sDocumentType, sDocumentTemplate_Code)
End With
End If

Call modWordObjectModel.Page_PortraitFormat(objDocument, sDocumentType, sDocumentTemplate_Code, objTargetSection)

gApplicationWord.ActiveWindow.ActivePane.View.Zoom.PageFit = objCurrentPageFit
gApplicationWord.ActiveWindow.ActivePane.View.Zoom.Percentage = iCurrentViewPercentage

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"insert a portrait page at this point in the document." & _
System.Environment.NewLine & _
System.Environment.NewLine & _
"Please check the formatting in this section.")
End Try
End Sub

IntentionallyBlankInsert

Public Sub Page_IntentionallyBlankInsert(ByRef objDocument As Word.Document, _
ByVal sDocumentTemplate_Code As String)

Dim objCurrentPageFit As Word.WdPageFit
Dim iCurrentViewPercentage As Integer

Dim objSection As Word.Section
Dim objSectionPrevious As Word.Section

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

objCurrentPageFit = gApplicationWord.ActiveWindow.ActivePane.View.Zoom.PageFit
iCurrentViewPercentage = gApplicationWord.ActiveWindow.ActivePane.View.Zoom.Percentage

gApplicationWord.ActiveWindow.ActivePane.View.Zoom.PageFit = Word.WdPageFit.wdPageFitFullPage

If (modWordObjectModel.Section_CanBeChanged(objDocument, sDocumentTemplate_Code) = False) Then
Exit Sub
Else

objSection = gApplicationWord.Selection.Sections(1)

If objSection.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape Then
Call modMessages.Message_General_OperationNotAllowedInLandscape()
Exit Sub
End If

gApplicationWord.Selection.MoveUp(Unit:=Word.WdUnits.wdLine, Count:=1)
objSectionPrevious = gApplicationWord.Selection.Sections(1)
gApplicationWord.Selection.MoveDown(Unit:=Word.WdUnits.wdLine, Count:=1)

If Not (objSectionPrevious.Index < objSection.Index) Then

With gApplicationWord.Selection
.TypeParagraph()
.MoveUp(Unit:=Word.WdUnits.wdLine, Count:=1)
.Style = objDocument.Styles("B-TextPageLeftyBlank")
.TypeText(Text:="This page is intentionally left blank")

'.InsertBreak Type:=wdSectionBreakNextPage
'.Delete
'.MoveUp Unit:=wdLine, Count:=1
'.ParagraphFormat.Alignment = wdAlignParagraphCenter
'.PageSetup.VerticalAlignment = wdAlignVerticalCenter
'Call CheckOrientation

End With
Else
With gApplicationWord.Selection
.TypeParagraph()
.MoveUp(Unit:=Word.WdUnits.wdLine, Count:=1)
.Style = objDocument.Styles("B-TextPageLeftyBlank")
.TypeText(Text:="This page is intentionally left blank")
End With
End If


End If

gApplicationWord.ActiveWindow.ActivePane.View.Zoom.PageFit = objCurrentPageFit
gApplicationWord.ActiveWindow.ActivePane.View.Zoom.Percentage = iCurrentViewPercentage

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"insert an intentionally blank page." & _
System.Environment.NewLine & _
System.Environment.NewLine & _
"Please check the formatting in this section.")
End Try
End Sub

LandscapeFormat

Public Sub Page_LandscapeFormat(ByRef objDocument As Word.Document, _
ByVal sDocumentType As String, _
ByVal sDocumentTemplate_Code As String, _
Optional ByVal oTargetSection As Word.Section = Nothing, _
Optional ByVal SkipTableStretch As Boolean = False)

Dim oMargins As docSectionMargins
Dim oNextSection As Word.Section

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If oTargetSection Is Nothing Then
oTargetSection = gApplicationWord.Selection.Sections(1)
Else
oTargetSection = oTargetSection
End If

If modWordObjectModel.Section_CanBeChanged(objDocument, sDocumentTemplate_Code) = False Then
Exit Sub
Else
With oTargetSection
With .PageSetup
oMargins.TopMargin = .TopMargin
oMargins.BottomMargin = .BottomMargin
oMargins.LeftMargin = .LeftMargin
oMargins.RightMargin = .RightMargin
oMargins.Footer = .FooterDistance
oMargins.Header = .HeaderDistance
End With

If objDocument.Sections.Count > oTargetSection.Index Then
oNextSection = objDocument.Sections(oTargetSection.Index + 1)

With oNextSection
.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).LinkToPrevious = False
'.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages).LinkToPrevious = False
'.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).LinkToPrevious = False

End With
End If

.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).LinkToPrevious = False
'.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages).LinkToPrevious = False
'.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).LinkToPrevious = False

.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape

'Application.DoEvents()

With .PageSetup
.TopMargin = oMargins.TopMargin
.BottomMargin = oMargins.BottomMargin
.LeftMargin = oMargins.LeftMargin
.RightMargin = oMargins.RightMargin
.FooterDistance = oMargins.Footer
.HeaderDistance = oMargins.Header
End With

'Application.DoEvents()

With .Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.ParagraphFormat
.TabStops.ClearAll()
.TabStops.Add(Position:=getTabstop(oTargetSection), _
Alignment:=Word.WdTabAlignment.wdAlignTabRight, _
Leader:=Word.WdTabLeader.wdTabLeaderSpaces)
End With

'Application.DoEvents()
End With

If SkipTableStretch = False Then
Call modWordTables.Table_Stretcher(oTargetSection)
End If
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"convert the selected section to landscape." & _
System.Environment.NewLine & _
System.Environment.NewLine & _
"Please check the formatting in this section.")
End Try
End Sub

OrientationCheck

Public Sub Page_OrientationCheck(ByRef objDocument As Word.Document, _
ByVal sDocumentType As String, _
ByVal sDocumentTemplate_Code As String, _
Optional ByVal oTargetSection As Word.Section = Nothing, _
Optional ByVal SkipTableStretch As Boolean = False)

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If gApplicationWord.Selection.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape Then
Call modWordObjectModel.Page_LandscapeFormat(objDocument, _
sDocumentType, _
sDocumentTemplate_Code, _
oTargetSection, _
SkipTableStretch)
Else
Call modWordObjectModel.Page_PortraitFormat(objDocument, _
sDocumentType, _
sDocumentTemplate_Code, _
oTargetSection, _
SkipTableStretch)
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex)
End Try
End Sub

PortraitFormat

Public Sub Page_PortraitFormat(ByRef objDocument As Word.Document, _
ByVal sDocumentType As String, _
ByVal sDocumentTemplate_Code As String, _
Optional ByVal oTargetSection As Word.Section = Nothing, _
Optional ByVal SkipTableStretch As Boolean = False)

Dim oMargins As docSectionMargins
Dim oNextSection As Word.Section

Try
Call Tracer_Add2("SUBROUTINE", System.Reflection.MethodBase.GetCurrentMethod.Name & " start")
If My.Settings.ERROR_OCCURRED = True Then Exit Sub

If oTargetSection Is Nothing Then
oTargetSection = gApplicationWord.Selection.Sections(1)
Else
oTargetSection = oTargetSection
End If

If modWordObjectModel.Section_CanBeChanged(objDocument, sDocumentTemplate_Code) = False Then
Exit Sub
Else

With oTargetSection
With .PageSetup
oMargins.TopMargin = .TopMargin
oMargins.BottomMargin = .BottomMargin
oMargins.LeftMargin = .LeftMargin
oMargins.RightMargin = .RightMargin
oMargins.Footer = .FooterDistance
oMargins.Header = .HeaderDistance
End With

oNextSection = objDocument.Sections(oTargetSection.Index + 1)

With oNextSection
.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).LinkToPrevious = False
'.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages).LinkToPrevious = False
'.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).LinkToPrevious = False

End With

.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).LinkToPrevious = False
'.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages).LinkToPrevious = False
'.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).LinkToPrevious = False

.PageSetup.Orientation = Word.WdOrientation.wdOrientPortrait

'Application.DoEvents()

With .PageSetup
.TopMargin = oMargins.TopMargin
.BottomMargin = oMargins.BottomMargin
.LeftMargin = oMargins.LeftMargin
.RightMargin = oMargins.RightMargin
.HeaderDistance = oMargins.Header
.FooterDistance = oMargins.Footer
End With

'Application.DoEvents()

With .Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.ParagraphFormat
.TabStops.ClearAll()
.TabStops.Add(Position:=getTabstop(oTargetSection), _
Alignment:=Word.WdTabAlignment.wdAlignTabRight, _
Leader:=Word.WdTabLeader.wdTabLeaderSpaces)
End With

'Application.DoEvents()

End With

If SkipTableStretch = False Then
Call modWordTables.Table_Stretcher(oTargetSection)
End If
End If

Catch ex As System.Exception
Call modMessages.Exception(System.Reflection.MethodBase.GetCurrentMethod, Nothing, ex, _
"convert the selected section to portrait." & _
System.Environment.NewLine & _
System.Environment.NewLine & _
"Please check the formatting in this section.")
End Try
End Sub

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top