elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Arreglado, de nuevo, el registro del warzone (wargame) de EHN


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  Duda [VBA] Reemplazar una fecha por otra en la cabecera
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Duda [VBA] Reemplazar una fecha por otra en la cabecera  (Leído 1,980 veces)
solaire

Desconectado Desconectado

Mensajes: 9


Ver Perfil
Duda [VBA] Reemplazar una fecha por otra en la cabecera
« en: 10 Febrero 2015, 12:40 pm »

Hola de nuevo,
Hace poco pedí ayuda para un vbs  aquí y me lo solucionasteis muy rapido y bien, ahora otra vez ando con un problema parecido aunque esta vez es vba.
He estado mirando todo lo que he podido pero no he encontrado nada que me aclarase, si cosas parecidas pero no algo que me ayudara a acabarlo
Estoy intentando ver si puedo hacer un vba que reemplace la fecha de una cabecera por otra en varios documento .doc
El tema esta en que remplazara la fecha de la cabecera ( con este formato: 22/02/2014 ) por otra del mismo formato. La entrada de la fecha por la que se va a remplazar me da igual que este en el mismo script o que la pida al ejecutar.
Hasta ahora tengo hecho algo asi:
Sub FindAndReplaceFirstStoryOfEachType()
    Set objRegEx = CreateObject("vbscript.regexp")
    objRegEx.Global = True
    objRegEx.IgnoreCase = True
    objRegEx.MultiLine = True
    Set wdDoc = wdApp.Documents.Open("C:\Nueva carpeta\*.doc")
With wdDoc.Content.Find
      .Text = "([1-31]{1,3})/([1-12]{1,3})/([2010-2016]{10,20})"
      .Replacement.Text = "<fecha actual>"
      .Wrap = wdFindContinue
      .Execute Replace:=wdReplaceAll
End With
End Sub

He probado varias cosas y no doy con el formato adecuado en .Text para que me lo remplace y no consiguo que se aplique a la cabecera.
Espero que me podias ayudar tan bien como la ultima vez
Saludos



« Última modificación: 10 Febrero 2015, 12:41 pm por solaire » En línea

solaire

Desconectado Desconectado

Mensajes: 9


Ver Perfil
Re: Duda [VBA] Reemplazar una fecha por otra en la cabecera
« Respuesta #1 en: 11 Febrero 2015, 11:33 am »

Hasta ahora solo he conseguida modificar la fecha y cambiarlo en el formato correcto
Código:
Sub FindAndReplaceFirstStoryOfEachType()
Set objRegEx = CreateObject("vbscript.regexp")
objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.MultiLine = True
objRegEx.Pattern = "[0-9]{2}/[0-9]{2}/[0-9]{4}"

Dim rngStory As Range
For Each rngStory In ActiveDocument.StoryRanges
If Not IsEmpty(rngStory) Then
With rngStory.Find
    If objRegEx.test(rngStory) = True Then

     .Text = CStr(rngStory)
     .Replacement.Text = Format(now(), "mm/dd/yyyy")
     .Wrap = wdFindContinue
     .Execute Replace:=wdReplaceAll
    End If
End With

End If
Next rngStory
End Sub
Tambien he sacado un codigo para modificar la cabecera:
Código:
ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
El problema esta en que no me deja moverme por la cabecera, es decir, solo me modifica la primera celda de la cabecera.
Ayuda por favor, no hay manera de que funcione


« Última modificación: 11 Febrero 2015, 11:35 am por solaire » En línea

solaire

Desconectado Desconectado

Mensajes: 9


Ver Perfil
Re: Duda [VBA] Reemplazar una fecha por otra en la cabecera
« Respuesta #2 en: 12 Febrero 2015, 10:52 am »

Al final tras investigar y preguntar por otros sitios lo he conseguido
Para el que le interese hay le va el codigo :
Este para abrir en varios words, lo guardas en una plantilla
Código:
Sub openf()
Dim FSO As Object
Dim fPath As String
Dim myFolder, myFile
Dim wdApp As Object
Dim wdDoc As Variant

fPath = "C:\" 'change to your directory
Set wdApp = GetObject(, "Word.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set myFolder = FSO.GetFolder(fPath).Files
For Each myFile In myFolder
If LCase(myFile) Like "*.docx" Then 'change to you file type
      Set wdDoc = wdApp.Documents.Open(CStr(myFile))
      wdApp.Visible = True
      FindAndReplaceFirstStoryOfEachType
      wdDoc.Save
      wdDoc.Close
      Set wdDoc = Nothing
End If
Next myFile

End Sub
Y luego este que modifica la cabecera:
Código:
Sub FindAndReplaceFirstStoryOfEachType()

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
    ActivePane.View.Type = wdOutlineView Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.MoveRight Unit:=wdCharacter, Count:=14
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=3
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=Format(Now(), "dd/mm/yyyy")
Selection.MoveLeft Unit:=wdCharacter, Count:=4
Selection.TypeBackspace
Selection.TypeText Text:="/"
End Sub
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines