C# Snippets


CrossReference

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

'http://support.microsoft.com/kb/175998
'http://support.microsoft.com/default.aspx?scid=kb;en-us;209668
With gApplicationWord.Dialogs(Word.WdWordDialog.wdDialogInsertCrossReference)
'.ReferenceType = "Figure"
'.Referencekind = Word.WdReferenceKind.wdNumberFullContext

System.Windows.Forms.SendKeys.Send("ff {TAB} {DOWN} {TAB}")
.Show()
End With

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

DocEvents_Close

Private Sub DocEvents_Close() Handles DocEvents2.Close

If mbFireEvents = True Then
RaiseEvent DocEventsClose_Event()
End If
End Sub

DocEvents_New

Private Sub DocEvents_New() Handles DocEvents.New

If mbFireEvents = True Then
RaiseEvent DocEventsNew_Event()
End If
End Sub

DocEvents_Open

Private Sub DocEvents_Open() Handles DocEvents.Open

If mbFireEvents = True Then
RaiseEvent DocEventsOpen_Event()
End If
End Sub

DocumentBeforeClose

Public Sub AppEvents_DocumentBeforeClose(ByVal Doc As Word.Document, _
ByRef Cancel As Boolean) _
Handles AppEvents.DocumentBeforeClose

If mbFireEvents = True Then
RaiseEvent AppEventsDocumentBeforeClose_Event(Doc, Cancel)
End If
End Sub

DocumentBeforePrint

Public Sub AppEvents_DocumentBeforePrint(ByVal Doc As Word.Document, _
ByRef Cancel As Boolean) _
Handles AppEvents.DocumentBeforePrint

If mbFireEvents = True Then
RaiseEvent AppEventsDocumentBeforePrint_Event(Doc, Cancel)
End If
End Sub

DocumentBeforeSave

Public Sub AppEvents_DocumentBeforeSave(ByVal Doc As Word.Document, _
ByRef SaveAsUI As Boolean, _
ByRef Cancel As Boolean) _
Handles AppEvents.DocumentBeforeSave

If mbFireEvents = True Then
RaiseEvent AppEventsDocumentBeforeSave_Event(Doc, SaveAsUI, Cancel)
End If
End Sub

DocumentChange

Public Sub AppEvents_DocumentChange() Handles AppEvents.DocumentChange

If mbFireEvents = True Then
RaiseEvent AppEventsDocumentChange_Event()
End If
End Sub

DocumentOpen

Public Sub AppEvents_DocumentOpen(ByVal Doc As Word.Document) _
Handles AppEvents.DocumentOpen

If mbFireEvents = True Then
RaiseEvent AppEventsDocumentOpen_Event(Doc)
End If
End Sub

Finalize

Protected Overrides Sub Finalize()
MyBase.Finalize()

gApplicationWord = Nothing
gErrorWord = Nothing
gCmdBarWord = Nothing

End Sub

FormFeldsReplaceFieldsWithParameters

Public Shared Function FormFeldsReplaceFieldsWithParameters() As String(,)
'this is to get around a known MSFT bug KB 286841

Try
If clsError.ErrorFlag() = True Then Exit Function

Dim objFormField As Word.FormField
Dim arFormFieldsArray As String(,)
Dim iTextFormCount As System.Int32

' Redim array to hold contents of text field.
ReDim Preserve arFormFieldsArray(2, gApplicationWord.ActiveDocument.FormFields.Count - 1)

For Each objFormField In gApplicationWord.ActiveDocument.FormFields
Select Case objFormField.Type
Case Word.WdFieldType.wdFieldFormTextInput
arFormFieldsArray(0, iTextFormCount) = "Text"
arFormFieldsArray(1, iTextFormCount) = objFormField.Result
arFormFieldsArray(2, iTextFormCount) = objFormField.Name

' Select the form field and replace it with placeholder text.
objFormField.Select()
gApplicationWord.Selection.TypeText("")

iTextFormCount = iTextFormCount + 1

Case Word.WdFieldType.wdFieldFormCheckBox
arFormFieldsArray(0, iTextFormCount) = "Checkbox"
arFormFieldsArray(1, iTextFormCount) = CType(objFormField.CheckBox.Value, String)
arFormFieldsArray(2, iTextFormCount) = objFormField.Name

' Select the form field and replace it with placeholder text.
'objFormField.Select()
'gApplicationWord.Selection.TypeText("")

iTextFormCount = iTextFormCount + 1

Case Else
End Select
Next objFormField

' Redim array to hold the exact number of text form fields
ReDim Preserve arFormFieldsArray(2, iTextFormCount - 1)

FormFeldsReplaceFieldsWithParameters = arFormFieldsArray

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_WORD = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("FormFeldsReplaceFieldsWithParameters", msCLASSNAME, _
"replace all the form fields with equivalent parameters.", _
mobjCOMException, mobjException)
End If
End Try
End Function

FormFeldsReplaceParametersWithFields

Public Shared Sub FormFeldsReplaceParametersWithFields(ByVal arFormFieldsArray(,) As String)

Try
If clsError.ErrorFlag() = True Then Exit Sub

Dim objFormField As Word.FormField
Dim ifieldcount As System.Int32

gApplicationWord.Selection.HomeKey(Unit:=Word.WdUnits.wdStory) 'Go to top of document.

gApplicationWord.Selection.Find.ClearFormatting()
With gApplicationWord.Selection.Find
.Forward = True
.Wrap = Word.WdFindWrap.wdFindContinue

For ifieldcount = 0 To arFormFieldsArray.GetUpperBound(1)
Do While .Execute(FindText:="") = True

' Replace the placeholder with the form field.
objFormField = gApplicationWord.Selection.FormFields.Add(Range:=gApplicationWord.Selection.Range, _
Type:=Word.WdFieldType.wdFieldFormTextInput)

' Restore form field contents and bookmark name.
objFormField.Result = arFormFieldsArray(1, ifieldcount)
objFormField.Name = arFormFieldsArray(2, ifieldcount)
Loop
gApplicationWord.Selection.HomeKey(Unit:=Word.WdUnits.wdStory) 'Go to top of document for next find.

'Do While .Execute(FindText:="") = True

' ' Replace the placeholder with the form field.
' objFormField = gApplicationWord.Selection.FormFields.Add(Range:=gApplicationWord.Selection.Range, _
' Type:=Word.WdFieldType.wdFieldFormCheckBox)

' ' Restore form field contents and bookmark name.
' objFormField.CheckBox.Value = CType(arFormFieldsArray(1, ifieldcount), Boolean)
' objFormField.Name = arFormFieldsArray(2, ifieldcount)
'Loop
'gApplicationWord.Selection.HomeKey(Unit:=Word.WdUnits.wdStory) 'Go to top of document for next find.

Next ifieldcount
End With

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_WORD = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("FormFeldsReplaceParametersWithFields", msCLASSNAME, _
"replace all the parameters with their original form fields.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Handle_Get

Public Function Handle_Get() As System.IntPtr
Dim objhandle As System.IntPtr
Dim objActiveDocument As Word.Document
Dim swindowname As String = ""
Dim bfilereadonly As Boolean = False

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

If (gApplicationWord.Documents.Count > 0) Then
objActiveDocument = Document_GetActive()

swindowname &= objActiveDocument.Name
If (objActiveDocument.ReadOnly = True) Then
swindowname &= " (Read-Only)"
End If
swindowname &= " - " & gApplicationWord.Caption

objhandle = clsWin32.FindWindow("OpusApp", swindowname)
Else
swindowname &= gApplicationWord.Caption
objhandle = clsWin32.FindWindow("OpusApp", swindowname)
End If

Return objhandle

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

InsertBreak

Public Sub Dialogs_InsertBreak()

Dim objDocument As Word.Document

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

objDocument = Document_GetActive()

If modWordObjectModel.Selection_IsProtected(objDocument) = False Then
gApplicationWord.Dialogs(Word.WdWordDialog.wdDialogInsertBreak).Show()
Else
Call modMessages.Breaks_UnableToInsertDocumentProtected()
End If

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

New

Public Sub New(ByVal sSolutionName As String, _
ByVal sDialogPrefix As String, _
ByVal sOfficeSolution As String, _
ByVal objWord As Word.Application, _
ByVal sVersion As String, _
ByVal objCmdBar As clsCmdBar)

Try
gApplicationWord = objWord

gsSOLUTION_NAME_WORD = sSolutionName
gsDIALOG_PREFIX_WORD = sDialogPrefix

gErrorWord = New clsError(gsSOLUTION_NAME_WORD, gsDIALOG_PREFIX_WORD, _
sOfficeSolution, objWord, sVersion)

gCmdBarWord = objCmdBar

gbDEBUG_WORD = clsError.DebugFlag("Debug Word")


Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_WORD = True Or _
((IsNothing(mobjCOMException) = False Or IsNothing(mobjException) = False)) Then

Call clsError.Handle("New", "clsWord", _
"initialise the 'clsWord' object.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

NewDocument

Public Sub AppEvents_NewDocument(ByVal Doc As Word.Document) _
Handles AppEvents2.NewDocument

If mbFireEvents = True Then
RaiseEvent AppEventsNewDocument_Event(Doc)
End If
End Sub

PasteSpecial

Public Function Dialogs_PasteSpecial() As Boolean
Try
gApplicationWord.Dialogs(Word.WdWordDialog.wdDialogEditPasteSpecial).Show()
Return True

Catch ex As Exception
Return False
End Try
End Function

Quit

Public Sub AppEvents_Quit() Handles AppEvents2.Quit

If mbFireEvents = True Then
RaiseEvent AppEventsQuit_Event()
End If
End Sub

SaveFileDialog

Public Function Dialogs_SaveFileDialog() As String

Dim FileName As String = String.Empty
Dim objSaveFileDialog As New System.Windows.Forms.SaveFileDialog

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

objSaveFileDialog.Filter = "Word Document (*.doc)|*.doc"
' Note: SFD owner is set to the Word Application so it is displayed on the same monitor
'If Sfd.ShowDialog(Win32Window) = System.Windows.Forms.DialogResult.OK Then
' FileName = Sfd.FileName
'End If

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

Startup

Public Sub AppEvents_Startup() Handles AppEvents.Startup

If mbFireEvents = True Then
RaiseEvent AppEventsStartup_Event()
End If
End Sub

WindowActivate

Public Sub AppEvents_WindowActivate(ByVal Doc As Word.Document, _
ByVal Wn As Word.Window) _
Handles AppEvents.WindowActivate

If mbFireEvents = True Then
RaiseEvent AppEventsWindowActivate_Event(Doc, Wn)
End If
End Sub

WindowBeforeDoubleClick

Public Sub AppEvents_WindowBeforeDoubleClick(ByVal Sel As Word.Selection, _
ByRef Cancel As Boolean) _
Handles AppEvents.WindowBeforeDoubleClick

If mbFireEvents = True Then
RaiseEvent AppEventsWindowBeforeDoubleClick_Event(Sel, Cancel)
End If
End Sub

WindowBeforeRightClick

Public Sub AppEvents_WindowBeforeRightClick(ByVal Sel As Word.Selection, _
ByRef Cancel As Boolean) _
Handles AppEvents.WindowBeforeRightClick

If mbFireEvents = True Then
RaiseEvent AppEventsWindowBeforeRightClick_Event(Sel, Cancel)
End If
End Sub

WindowDeactivate

Public Sub AppEvents_WindowDeactivate(ByVal Doc As Word.Document, _
ByVal Wn As Word.Window) _
Handles AppEvents.WindowDeactivate

If mbFireEvents = True Then
RaiseEvent AppEventsWindowDeactivate_Event(Doc, Wn)
End If
End Sub

WindowSelectionChange

Public Sub AppEvents_WindowSelectionChange(ByVal Sel As Word.Selection) _
Handles AppEvents.WindowSelectionChange

If mbFireEvents = True Then
RaiseEvent AppEventsWindowSelectionChange_Event(Sel)
End If
End Sub

WindowSize

Public Sub AppEvents_WindowSize(ByVal Doc As Word.Document, _
ByVal Wn As Word.Window) _
Handles AppEvents.WindowSize

If mbFireEvents = True Then
RaiseEvent AppEventsWindowSize_Event(Doc, Wn)
End If
End Sub

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