Search code examples
vb.netvisual-studiooffice-interop

different results in IDE when setting word cell to bold


We have a report application that fills a word template from information from our database, prior to sending it to clients etc.. The rest of the application runs with no problem but when I try and set table cells to bold it behaves differently in the VS IDE than when I run it externally.

I have tried different variations of the code below, but no matter what I do when I run this in the VS IDE it successfully sets the appropriate table cells to bold, but if I run it outside of the IDE the table cells are not bolded.

Dim oWord As Word.Application = Nothing
Dim oDoc As Word.Document = Nothing
Dim Table0 As Word.Table = Nothing


Private Sub SetBold(ByRef currentTable As Word.Table, ByVal Col1Bold As Boolean, ByVal Row1Bold As Boolean)
    With currentTable
        For c = 1 To .Columns.Count
            For r = 1 To .Rows.Count
                .Cell(r, c).Range.Font.Bold = If(c = 1 And Col1Bold = True, True, If(r = 1 And Row1Bold = True, True, False))
            Next
        Next
    End With

    End Sub

This article is talking about something similar (Different results between running inside Visual Studio and outside), but I have not been able to use it to resolve my problem.

I'm at a loss as to why this is happening, the rest of the application runs with no problem at all.


Solution

  • I don't know what is causing the issue, but I have found out that placing system.threading.thread.sleep(250) before the bold assignment fixes the problem. So it looks like it is something to do with timing and threads.