Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-24-2018, 07:52 PM
AnnaNmty AnnaNmty is offline OrganizerCopy method for ActiveDocument Windows 7 64bit OrganizerCopy method for ActiveDocument Office 2010 64bit
Novice
OrganizerCopy method for ActiveDocument
 
Join Date: Jul 2018
Posts: 2
AnnaNmty is on a distinguished road
Default 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
The eventual code will update/add many styles. I wish to do this via a macro rather than attaching a template.

Word 2010, Windows 7
Reply With Quote
  #2  
Old 07-25-2018, 12:36 AM
Guessed's Avatar
Guessed Guessed is offline OrganizerCopy method for ActiveDocument Windows 10 OrganizerCopy method for ActiveDocument Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 07-25-2018, 11:28 AM
AnnaNmty AnnaNmty is offline OrganizerCopy method for ActiveDocument Windows 7 64bit OrganizerCopy method for ActiveDocument Office 2010 64bit
Novice
OrganizerCopy method for ActiveDocument
 
Join Date: Jul 2018
Posts: 2
AnnaNmty is on a distinguished road
Default

Quote:
How you open the myFile document is irrelevant UNLESS you have created a new document...
But somehow it is relevant. I was testing with a file called Test2.docx (myFile). I created this document, along with the file styles are to be copied from. Neither is Read Only, has any restrictions, or could be open by other users. All styles that will be copied are available in the source document, Example.docx.

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:
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.
I did see that when finding the method originally in the MSDN, but admit to being a novice needing to learn more. This initial stab at writing the code worked with one method of opening though, and I am trying to understand why.

Quote:
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...
There is a corporate restriction on attaching a template. In the end, my goal is to add/modify about 5 - 10 styles, depending upon project requirements. The DocSource would change in the future, but the macro would only be run on a currently open document.
Reply With Quote
  #4  
Old 07-25-2018, 04:10 PM
Guessed's Avatar
Guessed Guessed is offline OrganizerCopy method for ActiveDocument Windows 10 OrganizerCopy method for ActiveDocument Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 07-25-2018, 08:31 PM
gmayor's Avatar
gmayor gmayor is offline OrganizerCopy method for ActiveDocument Windows 10 OrganizerCopy method for ActiveDocument Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
Reply

Tags
active document, styles

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
OrganizerCopy method for ActiveDocument Loop through listboxes in Application.OrganizerCopy Marrick13 Word VBA 46 05-26-2023 03:11 AM
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
OrganizerCopy method for ActiveDocument Using With ActiveDocument.Tables() SuzeG Word VBA 1 01-08-2014 02:00 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:42 AM.


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