Results 1 to 25 of 25

Thread: Opening Word Document and changing text & insert picture

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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:
    1. Option Explicit
    2.  
    3. Private objWord As Word.Application
    4. Private objDocument As Word.Document
    5.  
    6. Private Sub Form_Load()
    7.     Set objWord = New Word.Application
    8.    
    9.     Set objDocument = objWord.Documents.Open("C:\Template.doc")
    10. End Sub
    Any help apreciated, thanks

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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 ??

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Opening Word Document and changing text & insert picture

    OK, I found out how to insert a picture also
    VB Code:
    1. 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 ?

  4. #4
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Opening Word Document and changing text & insert picture

    Try:

    VB Code:
    1. objWord.Documents.Item(1).Shapes.AddPicture "C:\Test.JPG"
    2. objWord.Documents.Item(1).Shapes(1).Select ' Not shure what number it would be so it may need to be changed
    3. 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
    My Blog.

    Ryan Jones.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  6. #6
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Opening Word Document and changing text & insert picture

    Quote 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
    My Blog.

    Ryan Jones.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Opening Word Document and changing text & insert picture

    I get a permission denied error...
    VB Code:
    1. Option Explicit
    2.  
    3. Private objWord As Word.Application
    4. Private objDocument As Word.Document
    5.  
    6. Private Sub Form_Load()
    7.     Dim SHP As Word.Shape
    8.     Set objWord = New Word.Application
    9.    
    10.     Set objDocument = objWord.Documents.Open("C:\Test.doc")
    11.    
    12.     With objWord.Selection.Find
    13.         .ClearFormatting
    14.         .Text = "FirstName"
    15.        
    16.         With .Replacement
    17.             .ClearFormatting
    18.             .Text = "John Doe"
    19.         End With
    20.        
    21.         .Execute Replace:=Word.WdReplace.wdReplaceAll
    22.     End With
    23.    
    24.     Set SHP = objWord.Documents.Item(1).Shapes.AddPicture("C:\Test.JPG")
    25.     SHP.WrapFormat.Type = wdWrapSquare
    26.     SHP.Select
    27.    
    28.     [color=orange]Selection.ShapeRange.Align msoAlignRights, False[/color]
    29.    
    30.     objDocument.SaveAs "C:\Test_out.doc"
    31.     objDocument.Close False
    32.     Set objDocument = Nothing
    33.    
    34.     objWord.Quit False
    35.     Set objWord = Nothing
    36. End Sub

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Opening Word Document and changing text & insert picture

    Actually, this worked
    VB Code:
    1. 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.

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. 'Positioning:
    2.     objDocument.Shapes(1).Select 'True
    3.     objDocument.Shapes(1).Left = 0 '-999998
    4.     objDocument.Shapes(1).Top = 200
    5.     objDocument.Shapes(1).Width = 189
    6.     objDocument.Shapes(1).Height = 125.25
    7.     'Formatting:
    8.     Selection.ShapeRange.WrapFormat.Type = wdWrapSquare
    9.     Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
    10.     Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
    11.     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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Opening Word Document and changing text & insert picture

    This did not make any diference whatsoever:
    VB Code:
    1. 'Formatting:
    2.     Selection.ShapeRange.WrapFormat.Type = wdWrapSquare
    3.     Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
    4.     Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
    5.     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:
    1. Set SHP = objWord.Documents.Item(1).Shapes.AddPicture("C:\Test.JPG")
    2.     SHP.Select
    3.     Selection.ShapeRange.LockAspectRatio = msoTrue
    4.    
    5.     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...

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  14. #14
    Lively Member
    Join Date
    Jul 2005
    Posts
    67

    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.

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  16. #16
    New Member
    Join Date
    Feb 2006
    Posts
    6

    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...

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  18. #18
    New Member
    Join Date
    Feb 2006
    Posts
    6

    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!

  19. #19
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Opening Word Document and changing text & insert picture

    VB Code:
    1. Dim shp As Shape
    2. Set shp = ThisDocument.Shapes.AddCanvas(100, 100, 200, 200)
    3. shp.CanvasItems.AddPicture "C:\Documents and Settings\VB-Guru\My Documents\My Pictures\birthday.gif", True, True, 0, 0, 200, 200
    4. shp.CanvasItems.AddPicture "C:\Documents and Settings\VB-Guru\My Documents\My Pictures\MVP.bmp", True, True, 50, 50
    5. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  20. #20
    New Member
    Join Date
    Feb 2006
    Posts
    6

    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. ????

  21. #21
    New Member
    Join Date
    Feb 2006
    Posts
    6

    Re: Opening Word Document and changing text & insert picture

    I forgott to say that I´m using VB6. Is there a solution for VB6?

  22. #22
    New Member
    Join Date
    Feb 2006
    Posts
    6

    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.

  23. #23
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  24. #24
    New Member
    Join Date
    Feb 2006
    Posts
    6

    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.

  25. #25
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width