Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-14-2015, 02:07 AM
raymondroe raymondroe is offline Batch instruction works or ".doc" in folder, but not ".docx" Windows XP Batch instruction works or ".doc" in folder, but not ".docx" Office 2007
Novice
Batch instruction works or ".doc" in folder, but not ".docx"
 
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
  #2  
Old 02-14-2015, 02:48 AM
macropod's Avatar
macropod macropod is offline Batch instruction works or ".doc" in folder, but not ".docx" Windows 7 64bit Batch instruction works or ".doc" in folder, but not ".docx" Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try changing:
If Right(strName, 4) = ".doc" Or Right(strName, 4) = ".dot" Then
to:
If Split(strName, ".")(UBound(Split(strName, "."))) Like "do[ct]*" Then

PS: When posting code, please use the code tags, inserted via the # button on the posting menu
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-14-2015, 03:40 AM
raymondroe raymondroe is offline Batch instruction works or ".doc" in folder, but not ".docx" Windows XP Batch instruction works or ".doc" in folder, but not ".docx" Office 2007
Novice
Batch instruction works or ".doc" in folder, but not ".docx"
 
Join Date: Feb 2015
Location: West Sussex, UK
Posts: 3
raymondroe is on a distinguished road
Default

Wow, I am grateful - and think I can follow what you've done. I'll digest it today!

A bug turns up immediately following your improvement. The editor highlights:

Code:
Set wdDoc = Documents.Open(FileName:=strPath & "\" & strName, _ 
ReadOnly:=False, Format:=wdOpenFormatAuto)
I found the # button

Should 'format' (in the second line) be
ReadOnly:=False, Format:=wdDoc)?

Thanks again.

Ray
Reply With Quote
  #4  
Old 02-14-2015, 04:14 AM
macropod's Avatar
macropod macropod is offline Batch instruction works or ".doc" in folder, but not ".docx" Windows 7 64bit Batch instruction works or ".doc" in folder, but not ".docx" Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I'm not getting any errors at that point. If you have password protected documents, though, that may cause issues.

PS: You may be interested in: https://www.msofficeforums.com/word/...html#post16826. For what you're doing, you could simply replace:
'Do something
in that code with:
.Range.Paragraphs.First.Range.Delete
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 02-15-2015, 01:19 AM
raymondroe raymondroe is offline Batch instruction works or ".doc" in folder, but not ".docx" Windows XP Batch instruction works or ".doc" in folder, but not ".docx" Office 2007
Novice
Batch instruction works or ".doc" in folder, but not ".docx"
 
Join Date: Feb 2015
Location: West Sussex, UK
Posts: 3
raymondroe is on a distinguished road
Default

Thanks again, Paul. One way or another you've got my issue solved. I am grateful and look forward to my future in this forum.
Reply With Quote
Reply

Tags
.doc, .docx, batch

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
remove repeated words with " macro " or " wild cards " in texts with parentheses and commas jocke321 Word VBA 2 12-10-2014 11:27 AM
When composing an e-mail how do I add "Page Layout" and "View" tabs to the ribbon CensorTilSin Outlook 1 12-11-2013 12:05 PM
How to edit the "Format" and the "show level" of an EXISTING table of content? Jamal NUMAN Word 2 08-14-2011 10:46 AM
Batch instruction works or ".doc" in folder, but not ".docx" How to choose a "List" for certain "Heading" from "Modify" tool? Jamal NUMAN Word 2 07-03-2011 03:11 AM
"Microsoft Excel Application" missing in the "Component Services" on win08 sword.fish Excel 0 02-26-2010 02:09 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:46 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft