Results 1 to 22 of 22

Thread: {Resolved}Problem with Word-Bookmark with VB.NET

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    {Resolved}Problem with Word-Bookmark with VB.NET

    Hi,

    This is the code I execute in a loop. For example ... this will be run 3-4 times to loop in a dataset.


    VB Code:
    1. objWord = CreateObject("Word.Application")
    2. objWord.ActivePrinter = "My Printer Name"
    3. objWord.Visible = False
    4.  
    5. Select Case strDocument
    6.     Case ANNEXEAF
    7.         With objWord
    8.             .Documents.Open("C:\Projets\\MyDocument.dot")
    9.  
    10.             .ActiveDocument.Bookmarks.Item("siPOCKET").Select() 'word doc bookmark
    11.             .Selection.Text = clsClass.NoPermis
    12.  
    13.             .ActiveDocument.Bookmarks.Item("siDATE").Select() 'word doc bookmark
    14.             .Selection.Text = clsClass.DateInfrac
    15.  
    16.             .PrintOut(Copies:=1, Collate:=True, Background:=True, PrintToFile:=False)
    17.  
    18. 'Wait for document to print
    19.             Do While .BackgroundPrintingStatus > 0
    20.                 Application.DoEvents()
    21.             Loop
    22.  
    23.             .Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
    24.         End With


    For the 1st document it's ok.
    But then it kind of freeze. When I run it line by line (debugging), the application goes fine.
    But if I'm not in debug mode, something is going wrong. The only thing I can see is the Do While .BackgroundPrintingStatus loop.

    I have to move the mouse or do something else on my PC to make to program continue between each document to print.

    And my application window "flikkles" .. as if we were seeing a Word window open, but I want that to happen in the background.


    Thanks for helping !
    Last edited by dbelley_office; Mar 18th, 2005 at 09:23 AM. Reason: problem resolved.

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

    Re: Problem with Word-Bookmark with VB.NET

    Sounds like you would be better off making it a MailMerge type of document since all your doing is filling
    bookmarks and printing. A MailMerge is a better choice for your 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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    Re: Problem with Word-Bookmark with VB.NET

    I don't really know how to do a MailMerge. Never done that before.

    I can open a file template and put the value at specific location in my document with a mailmerge?

    I my example I was filling bookmarks and printing. But some other times, I need to save to a .doc file instead of printing.

    Would a MailMerger allow me to do that aswell ?

    Know a good example on a website?
    What is the big difference between a mailmerge and the way i was doing it?

    Thanks.
    Last edited by dbelley_office; Mar 17th, 2005 at 01:19 PM.

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

    Re: Problem with Word-Bookmark with VB.NET

    Not too much difference but the MM gives you better stability with bound fields versus bookmarks.
    Also, you have four options for what to do with the document after its populated - email, fax, print, and save to file.
    I was just helping another member on vb6 and a word mm in the vba forum. Same logic. I'll post the link in a minute.
    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

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

    Re: Problem with Word-Bookmark with VB.NET

    Here are two posts of mine from two threads dealing with automating a MM.
    http://www.vbforums.com/showpost.php...70&postcount=6
    http://www.vbforums.com/showpost.php...7&postcount=25

    Note: these are both VB6 code examples.[/color]
    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

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    Re: Problem with Word-Bookmark with VB.NET

    MM seems to work closely with datasource.
    But I don't have a datasource.

    On my VB form I have a listbox filled with the many person names.
    I have a textbox for a date.

    Lets say I have a myTemplate.dot file which contains the following text.
    Mr. <myNAME>
    The day of your interview is : <myDATE>

    -----
    So on my form when I click on PRINT.

    I want, to make the merge with the data on my form and my word template.

    I want to print a separate document for each names selected in the list.

    But how can I make the link between the flags <myNAME> and <myDATE> ?

    It was easy with the bookmark thing. But so slow between 2 documents.

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

    Re: Problem with Word-Bookmark with VB.NET

    Well, does the listbox get loaded from a datasource?
    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
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    Re: Problem with Word-Bookmark with VB.NET

    The listbox yes.

    But not the Date textbox. And that was only as example. On my true form there is 8-10 input fields.

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

    Re: Problem with Word-Bookmark with VB.NET

    Yes, then your better off with a word template document using bookmarks.
    What happens if you close the document (in code) before the doc is done printing?
    Maybe you could get rid of the loop?
    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

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    Re: Problem with Word-Bookmark with VB.NET

    No I can't really get rid of the loop.

    For my problem,

    Using this code:

    VB Code:
    1. Do While objWord.BackgroundPrintingStatus > 0
    2.         ' needs Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    3.         Sleep (250) ' pause quarter second
    4.     Loop

    Instead of this:

    VB Code:
    1. Do While objWord.BackgroundPrintingStatus > 0
    2.         Application.DoEvents()
    3.     Loop

    Helps a lot for speed between document. Now it doesn't freeze.

    But still ... I want this to happen in the background ... but I see a Word window showing for a very little while .. just very quickly flashing, but still I don't want that.

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

    Re: Problem with Word-Bookmark with VB.NET

    You could minimize Word in code so it wont flash.

    Also, I was meaning what happens if you take out the loop completely? Does it error when your code try to close the document?
    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

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    Re: Problem with Word-Bookmark with VB.NET





    See .. in the taskbar we see a Word window for like a quarter second. And my screen does as if it was too busy .. hard to refresh ..


    I don't know if there is a way to avoid that.


    The document doesn't print If I close it while it is in the queue.

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

    Re: Problem with Word-Bookmark with VB.NET

    Just before it prints set the Application.Visible = False and then when its done printing set it back to True.
    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

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    Re: Problem with Word-Bookmark with VB.NET

    Application.Visible = False ?


    What is "Application" ... my word?

    Can't be more invisible than that.

    VB Code:
    1. objWord = CreateObject("Word.Application")
    2.                     objWord.ActivePrinter = "TOSHIBA e-STUDIO16/20/25 PCL 6"
    3.                     objWord.Visible = False
    4.                     objWord.WindowState = Word.WdWindowState.wdWindowStateMinimize
    5.  
    6.                     .PrintOut(Copies:=1, Collate:=True, Background:=True, PrintToFile:=False)

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    Re: Problem with Word-Bookmark with VB.NET

    The fact that Word is added a short while in the taskbar makes the screen to repaint .. that causes my form appearence problem.

    I course printing 20 documents in a loop takes all the juice. But if I could avoid word to show up ... no repaint would occur and my WaitCursor would stay visible.

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

    Re: Problem with Word-Bookmark with VB.NET

    Word.Application.Visible

    Since you are using the Selection object to replace the test in the bookmarks you can not keep word invisible for the complete duration.
    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

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    Re: Problem with Word-Bookmark with VB.NET

    Really ?

    When using bookmarks the Word window has to show up?

    This is total crap then, who would want that. Especially when all example with bookmarks refers to background processing. I can't believe people accept that.

    I will have to quickly find a backup idea for my application.

    Thanks for your constant help.

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

    Re: Problem with Word-Bookmark with VB.NET

    Let me re-phrase that, you could use other techniques to fill in a bookmark other then the select method.
    Try setting the .Text property of the bookmark to your clsClass.NoPermis directly instead of selecting/highlighting it.

    VB Code:
    1. .ActiveDocument.Bookmarks.Item("siPOCKET").Text = clsClass.NoPermis
    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

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    Re: Problem with Word-Bookmark with VB.NET

    That property doesn't exist.



    Would I avoid all these problem with a MM ?


    I might want to change my processing steps and make a selection in the database for the word-fusion.
    Last edited by dbelley_office; Mar 17th, 2005 at 05:00 PM.

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

    Re: Problem with Word-Bookmark with VB.NET

    Sorry, its under the range namespace.
    VB Code:
    1. .ActiveDocument.Bookmarks.Item("siPOCKET").Range.Text = clsClass.NoPermis
    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

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    Re: Problem with Word-Bookmark with VB.NET

    Nah ! ..

    Still had the same problem even doing this.

    I have to go, but I will try to give a shot with MM later tonight only to see if it does the same when I print.

    If not, i might try to modify my program to use a datasource in a fair way.


    Thanks again for your ideas.

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    Re: Problem with Word-Bookmark with VB.NET

    I finally found why I have the Word Window opening.


    I had this:
    VB Code:
    1. objWord.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
    2. objWord.Quit()

    Now I have this

    VB Code:
    1. 'objWord.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
    2. objWord.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)

    It seems that closing a document without quiting word, makes the word application to show. Since the 2 lines were one after the other, that would explain why the word app was not really showing but just quickly flashing.

    But now it works.

    So it's better to Quit word with a DoNoSave then close a document with a DoNotSave and then Quit word.

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