Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-05-2015, 05:31 AM
mrkozmic mrkozmic is offline Can't copy image format from template Windows 8 Can't copy image format from template Office 2013
Novice
Can't copy image format from template
 
Join Date: Mar 2015
Posts: 6
mrkozmic is on a distinguished road
Default Can't copy image format from template


I'm trying to copy image format from an image in a template to an image in a document. The document is based on that template.
When I run the code as shown in the attachment I get error:
---------------------------
Run-time error '4248':

This command is not available because no document is open.
---------------------------

The error is prompted at the first reference to ActiveDocument
If I step through the code it works (!?) It is enough to set a breakpoint at the first reference to ActiveDocument and it works fine.

But when I run full speed I get the error.

(I have tried to replaced the breakpoint with a 10s delay without luck)
Attached Images
File Type: png Untitled.png (17.0 KB, 11 views)
Reply With Quote
  #2  
Old 03-05-2015, 06:23 AM
gmayor's Avatar
gmayor gmayor is offline Can't copy image format from template Windows 7 64bit Can't copy image format from template Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,105
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

You can't select an item in a closed document - even if that document is the template. You need to open the template in order to copy from it e.g. as follows. The code assumes the process you envisaged was otherwise correct.

Code:
Sub Macro1()
Dim oSource As Document
Dim oTarget As Document
Dim i As Long
Dim j As Long
    If Documents.Count = 0 Then
        MsgBox "No document open!"
        GoTo lbl_Exit
    End If
    'Define the document to be processed
    Set oTarget = ActiveDocument
    'Open the source document
    Set oSource = Documents.Open(ThisDocument.FullName)

    'Find the shape that fits the criteria
    For j = 1 To oSource.InlineShapes.Count
        If oSource.InlineShapes(j).AlternativeText = " FIGURE_STYLE" Then
            oSource.InlineShapes(j).Select
            Selection.CopyFormat
            Exit For
        End If
    Next
    'Close the template
    oSource.Close 0
    'Find the shape to process
    For i = 1 To oTarget.InlineShapes.Count
        If oTarget.InlineShapes(i).Type = wdInlineShapePicture Then
            oTarget.InlineShapes(i).Select
            Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
            Selection.PasteFormat
        End If
    Next i
lbl_Exit:
    Set oSource = Nothing
    Set oTarget = Nothing
    Exit Sub
End Sub
Use the #CODE button and paste your code into the message rather than insert an image of the code. It makes it much easier to test your code.
__________________
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
  #3  
Old 03-05-2015, 06:57 AM
mrkozmic mrkozmic is offline Can't copy image format from template Windows 8 Can't copy image format from template Office 2013
Novice
Can't copy image format from template
 
Join Date: Mar 2015
Posts: 6
mrkozmic is on a distinguished road
Default

Thanks gmayor!
Your solution works fine!

The template is not open as a regular document, but since it is attached to the document it seems as it is somehow "open"

I do copy other things from the template, eg. code below.I can also copy content controls and images. And as I wrote, It works if I step through the code. Maybe there-s a trick to do this without opening the doc?
HTML Code:
ThisDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Copy
        ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Paste
        'a linebreak is always added at the end, remove it
        ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Paragraphs(ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Paragraphs.Count).Range.Delete
Reply With Quote
  #4  
Old 03-05-2015, 07:22 AM
gmayor's Avatar
gmayor gmayor is offline Can't copy image format from template Windows 7 64bit Can't copy image format from template Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,105
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

The template is not open unless your open it.
You don't have to open it to extract the header from it, but you do to select something in it, so if you are going to use the selection object you must open it first, and as it was opened in the previous code, you can add the following before you close it

Code:
oTarget.Sections(1).Headers(wdHeaderFooterFirstPage).Range.FormattedText = _
oSource.Sections(1).Headers(wdHeaderFooterFirstPage).Range.FormattedText
If the document is created from the template, why doesn't it already have the information in the header and elsewhere?
__________________
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
  #5  
Old 03-05-2015, 08:21 AM
mrkozmic mrkozmic is offline Can't copy image format from template Windows 8 Can't copy image format from template Office 2013
Novice
Can't copy image format from template
 
Join Date: Mar 2015
Posts: 6
mrkozmic is on a distinguished road
Default

I do also select ContentControls (CC) from the template pages before copying to the document. It works.
I have actually edited the template while working with the document by refering to thisDocument (!?).
Quote:
Originally Posted by gmayor View Post
If the document is created from the template, why doesn't it already have the information in the header and elsewhere?
I do this when the template changes. There are complex elements contained within CCs. When I update some of these I also want to update these in the documents based on the template. I could create Building Blocks (BBs) out of the CCs, but that's few more operations every time a change is made, to update the BBs. Also, I want to replace old CCs in the document with new from the template having the same title, without looking up the correct BB for that CC.
Reply With Quote
  #6  
Old 03-06-2015, 05:35 AM
mrkozmic mrkozmic is offline Can't copy image format from template Windows 8 Can't copy image format from template Office 2013
Novice
Can't copy image format from template
 
Join Date: Mar 2015
Posts: 6
mrkozmic is on a distinguished road
Default

I might be wrong, but it looks like the template file is open. It's just not open as a regular file, not included in Documents.Count. There are several indications to that:
1. I can edit the template by referring to thisDocument.
2. I can copy all other things like if the template was open.
3. My code to copy the format works, but only if I set a breakpoint. I think I now why. When I select an image in thisDocument I make it the ActiveDocument and it gets corrupted with respect to the rest of the code. Now, if I set a breakpoint, the document gets focus back, ActiveDocument is corrected and the rest of the code works.

Does that make any sense?

My question is now:
How do I change focus from the template back to the document in VBA. ActiveDocument? Activate does obviously not work.

And finally:
4. When I open a document based on the template and try to delete the template, I get a message that the file is open i MS Word.
Reply With Quote
  #7  
Old 03-06-2015, 06:15 AM
mrkozmic mrkozmic is offline Can't copy image format from template Windows 8 Can't copy image format from template Office 2013
Novice
Can't copy image format from template
 
Join Date: Mar 2015
Posts: 6
mrkozmic is on a distinguished road
Post

I solved the problem with the following code:

HTML Code:
Dim startingPosition As Range
Set startingPosition = Selection.Range

    ...select something in the template  

'move focus back to the document  
startingPosition.Select
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't copy image format from template 1.image in a table 2.right click 3.menu click format 4.a format column appears OldFatDog Drawing and Graphics 1 06-13-2014 11:19 PM
OneNote - Copy and paste image to the table MartinK OneNote 0 09-03-2013 05:08 AM
Can't copy image format from template Copy text pastes image kfranken8 Word 2 07-12-2012 09:33 PM
Can't copy image format from template Vector image format jespestana Drawing and Graphics 4 11-26-2010 03:51 AM
Image Paste Format curtisdehaven Word 0 09-16-2009 04:12 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:49 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