|
-
Jun 8th, 2005, 02:33 PM
#1
Opening Word Document and changing text & insert picture
I did a search but did not find much help on this.
I need to know how to modify text in the document, and also how to insert a picture (that is NOT in a file, it's a picturebox in my program)
For example I have to find "FirstName" and replace with actual first name let's say "John Doe" (keeping the same font and formatting).
Also, I need to insert a picture in the document in the top/right corder, and have no clue how.
This is the code I found by searching, and did not find anything to do the rest of what I need...
VB Code:
Option Explicit
Private objWord As Word.Application
Private objDocument As Word.Document
Private Sub Form_Load()
Set objWord = New Word.Application
Set objDocument = objWord.Documents.Open("C:\Template.doc")
End Sub
Any help apreciated, thanks
-
Jun 8th, 2005, 03:54 PM
#2
Re: Opening Word Document and changing text & insert picture
Nevermind, I found out to search and replace text here:
http://msdn.microsoft.com/library/de...eplacetext.asp
So now, how to insert a picture in the document ??
-
Jun 8th, 2005, 05:10 PM
#3
Re: Opening Word Document and changing text & insert picture
OK, I found out how to insert a picture also
VB Code:
objWord.Documents.Item(1).Shapes.AddPicture "C:\Test.JPG"
Now I need to know how to align it to the right ?
Also it would be nice if I would not have to save the picture then load it in the document. It would be nice if I could just add the picturebox in the VB to Word...
Is anyone helping me here ? or I have to figure out everything on my own ?
-
Jun 8th, 2005, 05:19 PM
#4
Re: Opening Word Document and changing text & insert picture
Try:
VB Code:
objWord.Documents.Item(1).Shapes.AddPicture "C:\Test.JPG"
objWord.Documents.Item(1).Shapes(1).Select ' Not shure what number it would be so it may need to be changed
Selection.ShapeRange.Align msoAlignRights, False
As for a direct VB Picturebox too MS Word picture copy as far as I am aware it is not possible.
Cheers,
RyanJ
-
Jun 8th, 2005, 05:26 PM
#5
Re: Opening Word Document and changing text & insert picture
If you search the VBA Forum I have a thread in there where I wrote code for something almost exactly like this.
Search VBF first for all your code needs
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jun 8th, 2005, 05:27 PM
#6
Re: Opening Word Document and changing text & insert picture
 Originally Posted by RobDog888
If you search the VBA Forum I have a thread in there where I wrote code for something almost exactly like this.
Search VBF first for all your code needs 
Especially when its VBA related because we all know that Rob is the VBA Master 
Cheers,
RyanJ
-
Jun 8th, 2005, 05:29 PM
#7
Re: Opening Word Document and changing text & insert picture
Ok, I'm getting now. 
Here is the thread -
http://www.vbforums.com/showthread.php?t=326292
Actually it should be - VB/Office Guru™
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jun 8th, 2005, 06:20 PM
#8
Re: Opening Word Document and changing text & insert picture
I get a permission denied error...
VB Code:
Option Explicit
Private objWord As Word.Application
Private objDocument As Word.Document
Private Sub Form_Load()
Dim SHP As Word.Shape
Set objWord = New Word.Application
Set objDocument = objWord.Documents.Open("C:\Test.doc")
With objWord.Selection.Find
.ClearFormatting
.Text = "FirstName"
With .Replacement
.ClearFormatting
.Text = "John Doe"
End With
.Execute Replace:=Word.WdReplace.wdReplaceAll
End With
Set SHP = objWord.Documents.Item(1).Shapes.AddPicture("C:\Test.JPG")
SHP.WrapFormat.Type = wdWrapSquare
SHP.Select
[color=orange]Selection.ShapeRange.Align msoAlignRights, False[/color]
objDocument.SaveAs "C:\Test_out.doc"
objDocument.Close False
Set objDocument = Nothing
objWord.Quit False
Set objWord = Nothing
End Sub
-
Jun 8th, 2005, 06:42 PM
#9
Re: Opening Word Document and changing text & insert picture
Actually, this worked
VB Code:
Selection.ShapeRange.Align msoAlignRights, True
But it placed the picture a littl to much to the right, I need it before the right margin, cuz i'll have to print the document.
-
Jun 8th, 2005, 08:43 PM
#10
Re: Opening Word Document and changing text & insert picture
Did you see the post with the formatting code for the added image? #22
VB Code:
'Positioning:
objDocument.Shapes(1).Select 'True
objDocument.Shapes(1).Left = 0 '-999998
objDocument.Shapes(1).Top = 200
objDocument.Shapes(1).Width = 189
objDocument.Shapes(1).Height = 125.25
'Formatting:
Selection.ShapeRange.WrapFormat.Type = wdWrapSquare
Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
Selection.ShapeRange.LockAspectRatio = msoTrue
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jun 8th, 2005, 09:29 PM
#11
Re: Opening Word Document and changing text & insert picture
This did not make any diference whatsoever:
VB Code:
'Formatting:
Selection.ShapeRange.WrapFormat.Type = wdWrapSquare
Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
Selection.ShapeRange.LockAspectRatio = msoTrue
When I insert the picture it is on left/top, and I need it on top/right.
So the only thing that made a difference is the "objDocument.Shapes(1).Left = 355".
The shape object returns 78 as the width, so it means that the right margin (where I want it aligned) is 433.
The document object returns 966 as the width of the document, so that does not make any sence... because first I tried something like: "SHP.Left = objWord.Width - SHP.Width" that that was off the sceen I think, because I did not see the picture anywhere
So... right now i'm using this code:
VB Code:
Set SHP = objWord.Documents.Item(1).Shapes.AddPicture("C:\Test.JPG")
SHP.Select
Selection.ShapeRange.LockAspectRatio = msoTrue
SHP.Left = 355
But that positions the picture where I want it, but I would rather if it was aligned to the right
Anyways... I guess it's kind of resolved...
-
Jun 9th, 2005, 12:12 AM
#12
Re: Opening Word Document and changing text & insert picture
Are you creating it on a new document? I had opened a document, where the image was a bookmark. It stayed where it was placed only after setting the Top, Left, Width, and Height.
-
Jun 9th, 2005, 12:14 AM
#13
Re: Opening Word Document and changing text & insert picture
I posted that code already in #10. It should be positioning correctly if all the props are set.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Aug 31st, 2005, 03:32 AM
#14
Lively Member
Re: Opening Word Document and changing text & insert picture
This method works fine when placeing an image from a network share but is there a way of putting images into a document from a SQL database?
I've got a template word document with bookmarks in and I've worked out how to get text inserted in the relevant place, but the image won't go.
-
Aug 31st, 2005, 09:55 AM
#15
Re: Opening Word Document and changing text & insert picture
Sure you can. If your not familiar with databases and ADO you should check out beacons tutorial in the sticky at the top of the db forum.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 14th, 2006, 02:50 AM
#16
New Member
Re: Opening Word Document and changing text & insert picture
Another question about insert pictures into a Word-document. What if I have a picture and want to insert it on every new page that is created dynamicly. I want to insert a picture and a smaller picture over the other picture on every new page.
I´m using
DOC.Selection.InsertBreak wdSectionBreakNextPage
in a loop for making a new page. And then on the new page I will insert the pictures again.
If I´m using
DOC.Shapes.AddPicture theFile, False, True, 50, 50, 200, 300, Selection.Range
All the pictures will end up on the last page. And if I´m using:
DOC.Application.Selection.InlineShapes.AddPicture theFile
I can´t get one picture over the other.
Hope you understand...
-
Feb 14th, 2006, 03:07 AM
#17
Re: Opening Word Document and changing text & insert picture
Use a drawing canvas and then add your images to it. That will allow you to place the ordering of the layers of the images.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 14th, 2006, 03:11 AM
#18
New Member
Re: Opening Word Document and changing text & insert picture
Ok... I´m stupid. But can you give me an example in code. THAT WOULD BE VERY NICE!!! You are fast man!
-
Feb 14th, 2006, 03:26 AM
#19
Re: Opening Word Document and changing text & insert picture
VB Code:
Dim shp As Shape
Set shp = ThisDocument.Shapes.AddCanvas(100, 100, 200, 200)
shp.CanvasItems.AddPicture "C:\Documents and Settings\VB-Guru\My Documents\My Pictures\birthday.gif", True, True, 0, 0, 200, 200
shp.CanvasItems.AddPicture "C:\Documents and Settings\VB-Guru\My Documents\My Pictures\MVP.bmp", True, True, 50, 50
shp.CanvasItems.Item(2).ZOrder msoBringToFront
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 14th, 2006, 03:56 AM
#20
New Member
Re: Opening Word Document and changing text & insert picture
shp.CanvasItems Doesn´t work. I can´t get to COnvasItems. It seems that shp doesn´t have it. ????
-
Feb 14th, 2006, 04:24 AM
#21
New Member
Re: Opening Word Document and changing text & insert picture
I forgott to say that I´m using VB6. Is there a solution for VB6?
-
Feb 14th, 2006, 04:56 AM
#22
New Member
Re: Opening Word Document and changing text & insert picture
I made a change and the pictures was applied to the document. One over another. But one problem is not solved. ALL of the pictures is ending up on the last page....
Code snippet:
For Each theFile In f.Files
With DOC.Application.Selection.PageSetup
.TopMargin = 0
.BottomMargin = 0
.LeftMargin = 0
.RightMargin = 0
.Gutter = 0
.HeaderDistance = 0
.FooterDistance = 0
End With
DOC.Shapes.AddCanvas 100, 100, 200, 200
DOC.Shapes.Item(1).CanvasItems.AddPicture theFile, True, True, 0, 0, 200, 200
DOC.Shapes.Item(1).CanvasItems.AddPicturetheFile, True, True, 50, 50
DOC.Shapes.Item(1).CanvasItems.Item(2).ZOrder 0
DOC.Application.Selection.InsertBreak wdSectionBreakNextPage
Next theFile
I want the to pictures on every page that is created. Not only on the last page.
-
Feb 14th, 2006, 09:56 AM
#23
Re: Opening Word Document and changing text & insert picture
Not sure how to get it on a particular page as identifing page positions are very limited in code.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 15th, 2006, 05:13 AM
#24
New Member
Re: Opening Word Document and changing text & insert picture
The solution that I made was that I created a wordtemplate first with 2 pictures on it. Then I inserted the template as file in a new document. This was made in my loop, so for every new template file, this template was inserted into a new page in the "new" document. So... it ended up in not the best solution but it worked.
-
Feb 15th, 2006, 12:15 PM
#25
Re: Opening Word Document and changing text & insert picture
Cool, glad its solved now. 
Dont forget to mark your thread as Resolved.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|