Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports Microsoft.Vbe.Interop
- Public Class MacroInsert
- Private strFilenameToConvert As String
- Private procLine As Long
- Private oDoc As New Microsoft.Office.Interop.Word.Document
- Private vbProj As Microsoft.Vbe.Interop.VBProject
- Private vbc As Microsoft.Vbe.Interop.VBComponent
- Private vbcode As Microsoft.Vbe.Interop.CodeModule
- Private wordApp = New Microsoft.Office.Interop.Word.Application
- Private oModule As VBComponent
- Dim obj As Object
- Public exMacro As Boolean
- Dim newMacro As Boolean
- Public exMacroOld As Boolean
- Dim VBALineCount As Long
- Public Function AbrirWord(ByVal Local As String, ByVal Show As Boolean, ByVal displayAlert As Boolean, ByVal EnableMacro As Boolean)
- wordApp.Application.DisplayAlerts = displayAlert
- wordApp.Visible = Show
- If EnableMacro = False Then
- wordApp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable
- End If
- oDoc = wordApp.Documents.Open(Local)
- vbProj = oDoc.VBProject
- If (oDoc.VBProject.VBComponents.Count() < 1) Then
- oDoc.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule)
- newMacro = True
- End If
- vbc = oDoc.VBProject.VBComponents(vbext_ComponentType.vbext_ct_StdModule)
- vbcode = vbc.CodeModule
- ReadMacros()
- Return 0
- End Function
- Private Function ReadMacros()
- On Error GoTo NoOldMacro
- If vbcode.ProcCountLines("ReturnUserName", vbext_ProcKind.vbext_pk_Proc) > 0 Then
- exMacroOld = True
- End If
- On Error GoTo NoMacro
- If vbcode.ProcCountLines("AutoOpen", vbext_ProcKind.vbext_pk_Proc) > 0 Then
- exMacro = True
- End If
- NoMacro:
- exMacro = False
- NoOldMacro:
- exMacroOld = False
- Return 0
- End Function
- Public Function GravarInit(ByVal Local As String, ByVal Macro As String)
- wordApp.Application.DisplayAlerts = False
- wordApp.Visible = False
- wordApp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable
- oDoc = wordApp.Documents.Open(Local)
- vbProj = oDoc.VBProject
- If (oDoc.VBProject.VBComponents.Count() < 1) Then
- oDoc.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule)
- End If
- vbc = oDoc.VBProject.VBComponents(vbext_ComponentType.vbext_ct_StdModule)
- vbcode = vbc.CodeModule
- GravarMacro(Macro)
- Return 0
- End Function
- Public Function PreencherCampo(ByVal Valor As String, ByVal index As Integer)
- oDoc.FormFields(index).Result = Valor
- Return 0
- End Function
- Public Function SaveAs(ByVal local As String)
- wordApp.ActiveDocument.SaveAs(local)
- Return (0)
- End Function
- Public Function LockDocument()
- oDoc.Protect(Microsoft.Office.Interop.Word.WdProtectionType.wdAllowOnlyFormFields)
- Return 0
- End Function
- Public Function GetDocumentState() As Integer
- If oDoc.ProtectionType = Microsoft.Office.Interop.Word.WdProtectionType.wdNoProtection Then
- Return 1
- Else
- Return 2
- End If
- Return 0
- End Function
- Public Function ListAllFields()
- For i As Integer = 1 To oDoc.FormFields.Count()
- oDoc.FormFields(i).OwnHelp = True
- oDoc.FormFields(i).HelpText = i
- Next
- Return 0
- End Function
- Public Function protectDoc(ByVal pass As String)
- oDoc.Protect(Microsoft.Office.Interop.Word.WdProtectionType.wdAllowOnlyFormFields, True, pass)
- Return 0
- End Function
- Public Function UnprotectDoc(ByVal pass As String) As Boolean
- Dim password As Object
- password = pass
- oDoc.Save()
- On Error GoTo ErrnoProtec
- oDoc.Password = 0
- oDoc.Save()
- oDoc.Unprotect(password)
- oDoc.Save()
- Return True
- ErrnoProtec:
- Return False
- End Function
- Public Function ClearAllFields()
- For i As Integer = 1 To oDoc.FormFields.Count()
- oDoc.FormFields(i).TextInput.Clear()
- Next
- Return 0
- End Function
- Public Function GravarMacro(ByVal filePat As String)
- If exMacro Then
- vbcode.InsertLines(vbcode.ProcStartLine("AutoOpen", vbext_ProcKind.vbext_pk_Proc), "ProtDoc()")
- End If
- vbcode.AddFromFile(filePat)
- oDoc.RunAutoMacro(Microsoft.Office.Interop.Word.WdAutoMacros.wdAutoOpen)
- Return 0
- End Function
- Private Function countCodeLines()
- Dim obj As Object
- Dim VBALineCount As Long
- For Each obj In oDoc.VBProject.VBComponents
- VBALineCount = VBALineCount + obj.CodeModule.CountOfLines
- Next obj
- Return VBALineCount
- End Function
- Public Function Close()
- oDoc.Close()
- Return (0)
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement