View Single Post
 
Old 02-14-2015, 02:07 AM
raymondroe raymondroe is offline Windows XP Office 2007
Novice
 
Join Date: Feb 2015
Location: West Sussex, UK
Posts: 3
raymondroe is on a distinguished road
Default Batch instruction works or ".doc" in folder, but not ".docx"

Hello
I’ve just arrived, but have been developing my interest and use (often by imitation) in VBA. I’ve got an issue, it seems, in the language change between old and new Word and that's driving me mad! All I am doing is executing the code to remove the first line of all Word documents in a folder. It works fine with ".doc" but will not run on ".docx". Can see where to improve it? Your thoughts would be appreciated.
Best wishes
Ray
Code:
Option Explicit
Dim scrFso As Object
Dim scrFolder As Object
Dim scrFile As Object
Dim scrFiles As Object
 
Sub AllFilesInFolder()
Dim strStartPath As String
strStartPath = "C:\Documents\****** "
Application.ScreenUpdating = False
OpenAllFiles strStartPath
Application.ScreenUpdating = True
End Sub
 
Sub OpenAllFiles(strPath As String)
' AllFilesInFolder - runs through a folder oPath, opening each file in that folder, calling the macro and then closing each file in that folder
Dim strName As String
Dim wdDoc As Document
If scrFso Is Nothing Then Set scrFso = CreateObject("scripting.filesystemobject")
Set scrFolder = scrFso.getfolder(strPath)
For Each scrFile In scrFolder.Files
  strName = scrFile.Name
  Application.StatusBar = strPath & "\" & strName
  'open the file fName if it is a word document or template
  If Right(strName, 4) = ".doc" Or Right(strName, 4) = ".dot" Then
    Set wdDoc = Documents.Open(FileName:=strPath & "\" & strName, _
    ReadOnly:=False, Format:=wdOpenFormatAuto)
    'Call the macro to perform first line deletion
    DoWork wdDoc
    wdDoc.Close wdSaveChanges
  End If
Next
Application.StatusBar = False
End Sub
 
Sub DoWork(wdDoc As Document)
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub

Last edited by macropod; 02-14-2015 at 02:51 AM. Reason: Added code tags & formatting
Reply With Quote