#1
|
|||
|
|||
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) |
#2
|
||||
|
||||
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
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
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 |
#4
|
||||
|
||||
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
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
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 (!?). 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. |
#6
|
|||
|
|||
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. |
#7
|
|||
|
|||
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 |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |
Copy text pastes image | kfranken8 | Word | 2 | 07-12-2012 09:33 PM |
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 |