PDA

View Full Version : [SOLVED:] Macro to position eahc shape in a new page and carry the previous text



pk247
10-16-2016, 01:03 PM
Hi Everyone,

I've been hitting my head against a brick wall with this one and was hoping someone could help me please?

The company I work for has developed a new process for generating our Word documents. It's through using TFS and once we're ready to publish to Word the document is created and then we manually tidy it up. I've manage to automate a lot of this but one thing that urks me is taking each image within the document and placing it in a new page and then resizing to a readable size. Each image has a line of text above it that needs to be carried down into the new page - and this is where my issue is. The line of text is "Normal" style but other than that I don't know how to automate the moving of the image AND the line of taxt above it into a new page for each.

I've been tweaking the following code but can't get it to work. Could someone maybe point out what I'm doing wrong please?


Sub imgnewpageTEST()



Dim x As Long
Dim oRng As Range

For x = 1 To ActiveDocument.InlineShapes.Count
Set oRng = ActiveDocument.InlineShapes(x).Range
With oRng

.MoveUp Unit:=wdLine, Count:=1, Extend:=wdMove
.MoveStart Unit:=wdLine, Count:=1
.Collapse direction:=wdCollapseStart
.InsertBreak Type:=wdPageBreak


End With
Next
End Sub



Thanks! I appreciate all the posts and help you guys have provided in the past!

Paul, IRELAND

gmaxey
10-16-2016, 03:43 PM
Maybe:


Sub imgnewpageTEST()
Dim x As Long
Dim oRng As Range
For x = 1 To ActiveDocument.InlineShapes.Count
Set oRng = ActiveDocument.InlineShapes(x).Range
With oRng
.MoveStart wdParagraph, -1
.Collapse wdCollapseStart
.InsertBreak wdPageBreak
End With
Next
End Sub

pk247
10-18-2016, 02:56 PM
Hi Greg,

Thanks for your help!. Unfortunately I am getting a runtime error 4605 "This method or property is not available because the current selection is outside of a block-level XML element".

When I debug .InsertBreak wdPageBreak is highlighted.

I tried your code in a blank Word document and it worked prefectly. I've googled 4605 but alas I think I'm out of depth.

I can't send you the file because it has client specific information. Perhaps you know a way around block-level XML element? I'm thinking I need to run some code beforehand to break these elements?

Thanks!

Paul, IRELAND

gmaxey
10-18-2016, 05:57 PM
Paul,

I would have see at least part of one of the files that causes the error. Maybe you could sanitize one and attach or send me website feedback and you and then send it to me direct via my reply.

pk247
10-19-2016, 03:40 PM
Thanks Greg. I will try to do that but it'll take me some time and will be tricky enough. Nevertheless it will be worth it in the long run!. I will mark this post as solved because technically you did help me with what I needed. Who knew XML elements would throw me a curve ball!

I appreciate your help!

Paul, IRELAND

gmaxey
10-19-2016, 06:09 PM
Paul, take your time. I will not be around much next week.