Option Explicit
Private Sub Form_Load()
Dim Word As Object
Dim Documento As Object
Dim Path As String
Dim Archivo As String
Dim Existe As String
Screen.MousePointer = vbHourglass
Path = App.Path & "\Informes\"
Archivo = "Plantilla.dot"
Existe = Dir(Path & Archivo)
If Trim(Existe) <> "" Then
Set Word = CreateObject("Word.Application")
Set Documento = Word.Documents.Add(Path & Archivo, , , False)
Documento.Activate
With Documento.Application
If IrAMarcador("[MARCADOR]", Documento) Then .Selection.TypeText Text:="EL TEXTO QUE QUIERO"
End With
Word.Visible = True
Word.Windows(Word.ActiveWindow).Caption = "Prueba Marcadores"
Documento.Activate
Else
MsgBox "No se encontró la plantilla de WORD", vbCritical, "Word"
End If
Screen.MousePointer = vbDefault
Set Documento = Nothing
Set Word = Nothing
End Sub
Public Function ExisteMarcador(Marcador As String, Documento As Object) As Boolean
Dim TextoDoc As Object
With Documento
Set TextoDoc = Documento.Content
TextoDoc.Find.Execute FindText:=Marcador, Forward:=True
ExisteMarcador = (TextoDoc.Find.Found = True)
End With
End Function
Public Function IrAMarcador(Marcador As String, ByVal Documento As Object) As Boolean
If ExisteMarcador(Marcador, Documento) Then
With Documento.Application
.Selection.Find.ClearFormatting
With .Selection.Find
.Text = Marcador
.Replacement.Text = ""
.Forward = True
.Wrap = 1
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
.Selection.Find.Execute
.Selection.TypeText Text:=""
IrAMarcador = True
End With
Else
IrAMarcador = False
End If
End Function