The overwritting may be coming from the picturebox resizing? The code I posted before to adjust the formatting
of the image had formatting to make the text wrap around the image. You may have to go back to that version.
Printable View
The overwritting may be coming from the picturebox resizing? The code I posted before to adjust the formatting
of the image had formatting to make the text wrap around the image. You may have to go back to that version.
that's what wer're using, though. i don't know what the problem is.
VB Code:
oDoc.Shapes.AddPicture FileName:=.FileName, LinkToFile:=True, SaveWithDocument:=True ', _ 'Left:=-999998, Top:=0, Width:=189, Height:=125.25, Anchor:=0 'Positioning: oDoc.Shapes(1).Select 'True oDoc.Shapes(1).Left = 0 '-999998 oDoc.Shapes(1).Top = 200 oDoc.Shapes(1).Width = 189 oDoc.Shapes(1).Height = 125.25 'Formatting: Selection.ShapeRange.WrapFormat.Type = wdWrapSquare Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph Selection.ShapeRange.LockAspectRatio = True oDoc.SaveAs FileName:=RealPath & "Flyer.doc", FileFormat:=wdFormatDocument oDoc.Close Set oDoc = Nothing oApp.Quit Set oApp = Nothing End With Unload oCDBForm Set oCDBForm = Nothing
Oh, I thought you said you changed it to a picturebox or something.
Are you setting the oDoc = to the active document or the index name of it?
VB Code:
Set oDoc = ActiveDocument 'Or Set oDoc = oApp.Documents("Flyer")
No, i also have the picture in the vb program. the filename of the doc is hardcoded.
Set oDoc = oApp.Documents.Open("***")
with app.path
tough one? i always seem to pick them. my great suggestion.
I thought you were going to upload a test doc?
I don't know what happened to it. I uploaded a new one this afternoon.
Oh. It was in #38. I removed it just now.
Can you post your vb code? It works ok in the vba but cant test it for multiple runs.
I will look at the vb code tomorrow.
Later.
VB Code:
Dim ff As Integer Dim ImageLoc As String Dim X As Long Dim Y As Long Dim oCDBForm As New frmCommonDialog X = ImageFlyer.Left + ImageFlyer.Width Y = ImageFlyer.Top oCDBForm.Move X, Y With oCDBForm.CommonDialog1 .DialogTitle = "Select Picture to use" .InitDir = RealPath .Filter = "Picture (*.jpg)|*.jpg|All " & _ "Files (*.*)|*.*" .FLAGS = _ cdlOFNFileMustExist + _ cdlOFNHideReadOnly + _ cdlOFNLongNames + _ cdlOFNExplorer .CancelError = False ' Change to trap cancel True .ShowOpen If .FileName = "" Then Exit Sub ImageFlyer.Picture = LoadPicture(.FileName) ff = FreeFile Open RealPath & "ImageLocation.txt" For Output As #ff Print #ff, .FileName Close #ff Dim oApp As Word.Application Dim oDoc As Word.Document Set oApp = New Word.Application oApp.Visible = False Set oDoc = oApp.Documents.Open(RealPath & "Flyer.doc") oDoc.Shapes(1).Delete oDoc.Shapes.AddPicture FileName:=.FileName, LinkToFile:=True, SaveWithDocument:=True ', _ 'Left:=-999998, Top:=0, Width:=189, Height:=125.25, Anchor:=0 'Positioning: oDoc.Shapes(1).Select 'True oDoc.Shapes(1).Left = 0 '-999998 oDoc.Shapes(1).Top = 200 oDoc.Shapes(1).Width = 189 oDoc.Shapes(1).Height = 125.25 'Formatting: Selection.ShapeRange.WrapFormat.Type = wdWrapSquare Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph Selection.ShapeRange.LockAspectRatio = True oDoc.SaveAs FileName:=RealPath & "Flyer.doc", FileFormat:=wdFormatDocument oDoc.Close Set oDoc = Nothing oApp.Quit Set oApp = Nothing End With Unload oCDBForm Set oCDBForm = Nothing
that's it. works fine the first time, but not the next.
hope you can find something. plus if you can help the over-writing problem that would be great!
There is no reason to create the app each time the button is clicked. Let move some of the word code to the
form load. This way we can recycle the object and hopefuly prevent the 429 issue.
(Crossing fingers waiting for response of Success) :DVB Code:
Option Explicit Private oApp As Word.Application Private Sub Form_Load() Set oApp = New Word.Application oApp.Visible = False End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) oApp.Quit False Set oApp = Nothing End SUb Private Sub Command1_Click Dim ff As Integer Dim ImageLoc As String Dim X As Long Dim Y As Long Dim oCDBForm As New frmCommonDialog X = ImageFlyer.Left + ImageFlyer.Width Y = ImageFlyer.Top oCDBForm.Move X, Y With oCDBForm.CommonDialog1 .DialogTitle = "Select Picture to use" .InitDir = RealPath .Filter = "Picture (*.jpg)|*.jpg|All " & _ "Files (*.*)|*.*" .FLAGS = _ cdlOFNFileMustExist + _ cdlOFNHideReadOnly + _ cdlOFNLongNames + _ cdlOFNExplorer .CancelError = False ' Change to trap cancel True .ShowOpen If .FileName = "" Then Exit Sub ImageFlyer.Picture = LoadPicture(.FileName) ff = FreeFile Open RealPath & "ImageLocation.txt" For Output As #ff Print #ff, .FileName Close #ff Dim oDoc As Word.Document oApp.Visible = False Set oDoc = oApp.Documents.Open(RealPath & "Flyer.doc") oDoc.Shapes(1).Delete oDoc.Shapes.AddPicture FileName:=.FileName, LinkToFile:=True, SaveWithDocument:=True ', _ 'Left:=-999998, Top:=0, Width:=189, Height:=125.25, Anchor:=0 'Positioning: oDoc.Shapes(1).Select 'True oDoc.Shapes(1).Left = 0 '-999998 oDoc.Shapes(1).Top = 200 oDoc.Shapes(1).Width = 189 oDoc.Shapes(1).Height = 125.25 'Formatting: Selection.ShapeRange.WrapFormat.Type = wdWrapSquare Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph Selection.ShapeRange.LockAspectRatio = True oDoc.SaveAs FileName:=RealPath & "Flyer.doc", FileFormat:=wdFormatDocument oDoc.Close False Set oDoc = Nothing End With Unload oCDBForm Set oCDBForm = Nothing End Sub
Allright. It worked for me, but I have to send it on to get the mark of approval.
Take a look at the .pdf that I just made. It is moving the picture, and then writing the Price underneath it. Is there something that we can do to prevent it?
The Selection.ShapeRange.WrapFormat.Type = wdWrapSquare should be handling this for you.
The only thing I can think of is to change the order so it formats the picture and then after that it sizes it.
Worse. I switch things around and now I get error 91 object variable or with block not set
at the WordWrapSquare line.
VB Code:
oDoc.Shapes.AddPicture FileName:=.FileName, LinkToFile:=True, SaveWithDocument:=True ', _ 'Left:=-999998, Top:=0, Width:=189, Height:=125.25, Anchor:=0 'Formatting: Selection.ShapeRange.WrapFormat.Type = wdWrapSquare Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph Selection.ShapeRange.LockAspectRatio = True 'Positioning: oDoc.Shapes(1).Select 'True oDoc.Shapes(1).Left = 0 '-999998 oDoc.Shapes(1).Top = 200 oDoc.Shapes(1).Width = 189 oDoc.Shapes(1).Height = 125.25
You need to have a selection object before you can use the selection object.
Add oDoc.Shapes(1).Select 'True before your formatting.
Still same error: oDoc.Shapes(1).Select added
Is it possible that it is using the wrong object? I know that the image is getting resized, but is the range wrong? also, we are referring to both odoc and selection, and i'm not sure of the difference.
I'd like to be able to help, but I can't find any reference to go by.
:confused:VB Code:
oDoc.Shapes.AddPicture FileName:=.FileName, LinkToFile:=True, SaveWithDocument:=True ', _ 'Left:=-999998, Top:=0, Width:=189, Height:=125.25, Anchor:=0 'Formatting: oDoc.Shapes(1).Select 'True Selection.ShapeRange.WrapFormat.Type = wdWrapSquare Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph Selection.ShapeRange.LockAspectRatio = True 'Positioning: oDoc.Shapes(1).Select 'True oDoc.Shapes(1).Left = 0 '-999998 oDoc.Shapes(1).Top = 200 oDoc.Shapes(1).Width = 189 oDoc.Shapes(1).Height = 125.25
What can I do? Something isn't right, but I don't know what it is?
Do you still have the document? Can you try it?
:: ka ching :: circled around again, wealth distributed.
Oh, so thats what you meant ;) Thanks.
I ran through three iterations with this code and no problems. I attached the
three docs it generated. It must be something in the commented code or
throughout your project. :confused:
Let me know when you DL the attachments so I can take them off.VB Code:
Option Explicit Private oApp As Word.Application Private Sub Form_Load() Set oApp = New Word.Application oApp.Visible = False End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) oApp.Quit False Set oApp = Nothing End Sub Private Sub Command1_Click() Dim ff As Integer Dim ImageLoc As String Dim X As Long Dim Y As Long ' Dim oCDBForm As New frmCommonDialog ' X = ImageFlyer.Left + ImageFlyer.Width ' Y = ImageFlyer.Top ' oCDBForm.Move X, Y ' With oCDBForm.CommonDialog1 ' .DialogTitle = "Select Picture to use" ' .InitDir = RealPath ' .Filter = "Picture (*.jpg)|*.jpg|All Files (*.*)|*.*" ' .Flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly Or cdlOFNLongNames Or cdlOFNExplorer ' .CancelError = False ' Change to trap cancel True ' .ShowOpen ' If .FileName = "" Then Exit Sub ' ImageFlyer.Picture = LoadPicture(.FileName) ' ff = FreeFile ' Open RealPath & "ImageLocation.txt" For Output As #ff ' Print #ff, .FileName ' Close #ff Dim oDoc As Word.Document oApp.Visible = False Set oDoc = oApp.Documents.Open("D:\Development\Flyer.doc") oDoc.Shapes(1).Delete oDoc.Shapes.AddPicture "D:\Development\About1.gif", LinkToFile:=True, SaveWithDocument:=True 'Positioning: oDoc.Shapes(1).Select 'True oDoc.Shapes(1).Left = 0 '-999998 oDoc.Shapes(1).Top = 200 oDoc.Shapes(1).Width = 189 oDoc.Shapes(1).Height = 125.25 'Formatting: Selection.ShapeRange.WrapFormat.Type = wdWrapSquare Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph Selection.ShapeRange.LockAspectRatio = True oDoc.SaveAs "D:\Development\Flyer3.doc", wdFormatDocument oDoc.Close False Set oDoc = Nothing ' End With ' Unload oCDBForm ' Set oCDBForm = Nothing End Sub
Thanks. I have downloaded them. That has got to be one for the books.
I am going to look at them now. That fixed things.
As it turned out, it was probably correct, but I was merging with the wrong letter, (the .dot file still, which I changed to replace the picture when I was still adjusting the sizes)
I had to run things again just now to make sure. The document looked fine on its own, but when it merged is when it got messed up. Now I finally think that things are working right.
I switched the size/format back to the way it was this morning.
Thanks again. I hope this is it for this project.
:thumb: I think this thread is a new record in the VBA forum :D
Its all just a matter of debugging and resolving the unknowns. :thumb:
Well, calling the object was definitely a problem that was pointed out to me after I thought things were working correcty.
Then, I had spent too much time changing things, so that I changed to a doc instead of dot, but forgot the merge was using the dot, and wondered why my changes weren't being seen.
Second set of eyes do it every time. That's what makes the forum great!
So then its official. Thread "Resolved" !!! :D
And now his thread is 3 pages long! Glad to hear that its working now and hope it gets approval by the client :thumb:
Ps, now you know why I dont like MMs ;)
Hello,
I am complete blank with word macro's etc., but I am a software engineer and have built several internet applications on ASP and VBscript. Now I have to make a Word template (.dot file) with a standard image. The client has to change this image when in it is an .doc file.
I read this post and i copied the sourcecode into the Visual Basic Editor of Word (office 2000), but it didn't seem to work.
My question is simple but huge:
How can I make an template (.dot) where the client can change the image. (the size of the image doesn't matter). Please can you explain me the method?