locked
Word Bookmarks via COM object terribly slow with certain user profiles RRS feed

  • Question

  • All,

    I need your help with an interesting "phenomenon" that I am unable to solve.

    One of my current Powershell scripts is connecting to remote Windows servers, querying various data via WMI, and populating it to Excel and Word.

    In theory the script is doing great and everything is working as expected. However, I have many servers, where the script becomes terribly slow when populating data into Word bookmarks.

    After some research I were able to recreate this issue and to nail it down to Word Bookmarks. I have created a test script in my lab environment with a plain word file in order to proof this.

    What happens:

    If I have a Bookmark as single object in one Word line, everything is working fine. But if I have a Bookmark, plus something else like a text, or table, the script become significantly slow.

    For example:   

    • OK: <Bookmark>
    • Not OK:  <Bookmark> This is some text

    The most interesting observation is that it doesn't happen at all user profiles. The same script, at the same server, is running great as expected for some users, and is becoming terribly slow for other users.

    I first were focusing around Powershell but were able to decline this as root cause. I'm now back at Office - since it seems to work fine for some, and for other user profiles not.

    There maybe is a Word / Office Setting (user setting / option) which might cause this? There is maybe a bug and I have to apply a particular patch or hotfix? It's probably related to COM or file permissions?

    #########
    # Execute First Test - this works fine. Measure-Command results are OK. 
    # Word template has just Bookmarks and nothing else in the same row
    
    		Write-Host
    		$WordApp1 = New-Object -ComObject Word.Application -ErrorAction Stop	
    		$Word1 = $WordApp1.Documents.OpenNoRepairDialog("C:\Users\VeltPX\Desktop\ScriptingGuy\OK.dot")	
    		# Milliseconds are OK
    		(Measure-Command {$Word1.Bookmarks.Item("One").Range.Text="1" }).TotalMilliseconds 
    		(Measure-Command {$Word1.Bookmarks.Item("Two").Range.Text="2" }).TotalMilliseconds 
    		(Measure-Command {$Word1.Bookmarks.Item("Three").Range.Text="3"}).TotalMilliseconds 
    		Start-Sleep -Seconds 5		# Insert 5 Seconds Break - in the real script we would just execute other commands here
    		# Milliseconds are still OK
    		(Measure-Command {$Word1.Bookmarks.Item("Four").Range.Text="4"}).TotalMilliseconds 
    		(Measure-Command {$Word1.Bookmarks.Item("Five").Range.Text="5"}).TotalMilliseconds 
    		(Measure-Command {$Word1.Bookmarks.Item("Six").Range.Text="6"}).TotalMilliseconds 
    		(Measure-Command {$Word1.Bookmarks.Item("Seven").Range.Text="7"}).TotalMilliseconds 
    		$Word1.SaveAs([ref]"C:\Users\VeltPX\Desktop\ScriptingGuy\OK.bak")
    		$Word1.Close()
    		$WordApp1.Quit()											
    ########
    # Execute Second Test. Measure-Command results are Not OK. 
    # Word Template has Bookmark and text in one row
    
    		Write-Host
    		$WordApp2 = New-Object -ComObject Word.Application -ErrorAction Stop	
    		$Word2 = $WordApp2.Documents.OpenNoRepairDialog("C:\Users\VeltPX\Desktop\ScriptingGuy\NOK.dot")	
    		# Milliseconds are OK
    		(Measure-Command {$Word2.Bookmarks.Item("One").Range.Text="1" }).TotalMilliseconds 
    		(Measure-Command {$Word2.Bookmarks.Item("Two").Range.Text="2" }).TotalMilliseconds 
    		(Measure-Command {$Word2.Bookmarks.Item("Three").Range.Text="3"}).TotalMilliseconds 
    		Start-Sleep -Seconds 5		# Insert 5 Seconds Break - in the real script we would just execute other commands here
    		# Milliseconds start to drasticaly increase
    		(Measure-Command {$Word2.Bookmarks.Item("Four").Range.Text="4"}).TotalMilliseconds 
    		(Measure-Command {$Word2.Bookmarks.Item("Five").Range.Text="5"}).TotalMilliseconds 
    		(Measure-Command {$Word2.Bookmarks.Item("Six").Range.Text="6"}).TotalMilliseconds 
    		(Measure-Command {$Word2.Bookmarks.Item("Seven").Range.Text="7"}).TotalMilliseconds 
    		$Word2.SaveAs([ref]"C:\Users\VeltPX\Desktop\ScriptingGuy\NOK.bak")
    		$Word2.Close()	
    		$WordApp2.Quit()										
    
    ########

    The result is the following:

    958.3164

    333.1276

    365.2574

    310.553

    380.6998

    345.7621

    345.1371

    362.4962

    407.1185

    333.8541

    538.5475

    27433.6454    <----- LOOK HERE WHAT HAPPENED

    27674.4349    <----- LOOK HERE WHAT HAPPENED

    27665.2589    <----- LOOK HERE WHAT HAPPENED


    Every help is highly appreciated.

    Tuesday, October 20, 2015 11:59 AM

All replies

  • Hi,

    Have you tried to use any VBA in Outlook? Does the issue persist if using VBA?

    You can try to repair Office from Control Panel to check the result tool.

    Regards,

    Melon Chen
    TechNet Community Support


    Please mark the reply as an answer if you find it is helpful.

    If you have feedback for TechNet Support, contact tnmff@microsoft.com.
    Wednesday, October 21, 2015 6:02 AM
  • Hi!

    I have tried to repair Office with the Office Diagnostics and this didn't solve the issue (hasn't found any issues to fix).

    I have tried to start Winword with command line parameter "/r" which didn't solve the issue.

    I never wrote anything in VBA but tried to rebuild the script in VBA. Below you'll find the script block. Within this scribt block, the issue did not appear. So either my VBA script doesn't properly translate my PS script, or the problem does not affect VBA.

    It's maybe related to COM?

    Sub FillBookmarks()
    Dim i As Long
    Dim wrdDoc As Word.Document
    Set wrdDoc = Word.Documents.Open("C:\Bookmark Bug\VBA.docx")
    
    wrdDoc.Bookmarks.Item("One").Range.Text = "1"
    wrdDoc.Fields.Update
    
    wrdDoc.Bookmarks.Item("Two").Range.Text = "2"
    wrdDoc.Fields.Update
    
    wrdDoc.Bookmarks.Item("Three").Range.Text = "3"
    wrdDoc.Fields.Update
    
    For i = 1 To 1000000000
    Next
    
    wrdDoc.Bookmarks.Item("Four").Range.Text = "4"
    wrdDoc.Fields.Update
    
    wrdDoc.Bookmarks.Item("Five").Range.Text = "5"
    wrdDoc.Fields.Update
    
    wrdDoc.Bookmarks.Item("Six").Range.Text = "6"
    wrdDoc.Fields.Update
    
    wrdDoc.Bookmarks.Item("Seven").Range.Text = "7"
    wrdDoc.Fields.Update
    
    End Sub

    Wednesday, October 21, 2015 12:09 PM