#1
|
|||
|
|||
OrganizerCopy method for ActiveDocument
When running the following code with the document I want to replace the style in open, I get a "File in Use" (Test.docx is locked for editing... ) window.
It seems to make a different how the file is opened though, and I am hoping one of you can explain why that is. If I open the file using File >Open within Word and then run the macro, it works. However, that is not a typical workflow for me—I usually open a file by clicking on it. Code:
Sub ReplaceStyles() Dim DocSource Dim myFile DocSource = "C:\Users\myname\Desktop\EXAMPLE.docx" Set myFile = ActiveDocument 'myFile = "full path to document.docx" Application.OrganizerCopy Source:=DocSource, Destination:=myFile, Object:=wdOrganizerObjectStyles, _ Name:="Caption-Photo" End Sub Word 2010, Windows 7 |
#2
|
||||
|
||||
How you open the myFile document is irrelevant UNLESS you have created a new document and the file has not yet been given a name and filepath location. But you do have some other issues with your code that would also be failure points.
OrganizerCopy expects the Source and the Destination to both be of type string. You supplied one string and one document so that would be a likely point of failure. Secondly, myFile may not be saved yet and therefore its path doesn't yet exist Thirdly, myFile may not be writeable. You can open a document but it could be opened in Read Only mode. The error message sounds like this is likely to be the problem. Fourthly, DocSource may not exist or be readable. Finally, does the style called 'Caption-Photo' exist in the DocSource file? It's not a built-in style name so there is always the danger that it doesn't exist. If you choose to do this manually, style by style that is your prerogative but it can be done with a single line if you used an attached template - Just saying...
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Quote:
If I open myFile via File > Open from Word, the macro executes properly when I run it and the style copies correctly. If I open myFile by double-clicking on it, the macro will not execute. If the file is opened from the network, the "File in Use" window pops up. Tested today: If the file is opened from my desktop, there is no error message, but no style is copied. Quote:
Quote:
|
#4
|
||||
|
||||
If you are learning vba, there is a good article that you should read which happens to include the OrganizerCopy method in it. See https://wordmvp.com/FAQs/MacrosVBA/MaintainableCode.htm
The macro provided at the bottom of that page can be simplified to suit your purposes. It is not exactly the same as your code and wouldn't import fresh style definitions if the stylename already exists in the target doc. By removing the StyleIsMissing parts you can copy the style every time regardless of whether the style already exists or not. Try this version. It doesn't test whether the ActiveDocument is read only or has style restrictions but if it fails then it is going to be much easier to pinpoint the problem. You can set the source file path to whatever suits you. Code:
Sub ImportMyStyles() Dim StyleArray() As String, i As Long, sSource As String, sTarget As String sSource = NormalTemplate.FullName '"C:\Users\myname\Desktop\EXAMPLE.docx" sTarget = ActiveDocument.FullName ActiveDocument.UpdateStylesOnOpen = False If ActiveDocument.Path = "" Or Dir(sSource) = "" Then MsgBox "One of these doesn't exist:" & vbCr & sTarget & vbCr & sSource, vbOKOnly, "Problem Encountered" Exit Sub End If StyleArray = Split("Caption-Photo|Heading 1|Heading 2", "|") 'list of styles to be copied here separated by a vertical bar On Error Resume Next 'don't throw an error if the style doesn't exist in the source doc For i = LBound(StyleArray) To UBound(StyleArray) Application.OrganizerCopy Source:=sSource, Destination:=sTarget, Name:=StyleArray(i), Object:=wdOrganizerObjectStyles Next i End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
||||
|
||||
Try the following which is trapped against the usual errors.
Code:
Option Explicit Sub CopyStyle() Dim oSource As Document Dim oTarget As Document Dim strFullName As String Dim fso As Object strFullName = Environ("USERPROFILE") & "\Desktop\EXAMPLE.docx" On Error Resume Next If LCase(ActiveDocument.FullName) = LCase(strFullName) Then MsgBox "The active document is the style source document!" GoTo lbl_Exit End If Set fso = CreateObject("Scripting.FileSystemObject") Set oTarget = ActiveDocument oTarget.Save If oTarget.Path = "" Then MsgBox "The document must be saved!" GoTo lbl_Exit End If If fso.FileExists(strFullName) Then Set oSource = Documents.Open(FileName:=strFullName, _ Visible:=False, _ AddToRecentFiles:=False) Application.OrganizerCopy Source:= _ oSource.FullName, Destination:= _ oTarget.FullName, _ Name:="Caption-Photo", _ Object:=wdOrganizerObjectStyles oSource.Close Else MsgBox strFullName & " is not available" End If lbl_Exit: Set oTarget = Nothing Set oSource = Nothing Set fso = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
Tags |
active document, styles |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Loop through listboxes in Application.OrganizerCopy | Marrick13 | Word VBA | 50 | 04-16-2024 01:21 PM |
ActiveDocument.ContentControls(ID) not working | Tejas.T | Word VBA | 3 | 03-09-2015 06:50 PM |
Loop through listboxes in Application.OrganizerCopy-revisited | Marrick13 | Word VBA | 0 | 12-06-2014 06:07 AM |
wordapp.ActiveDocument.SaveAs Not Working | KSReynolds | Mail Merge | 1 | 07-18-2014 04:03 PM |
Using With ActiveDocument.Tables() | SuzeG | Word VBA | 1 | 01-08-2014 02:00 PM |