VB.NET & werken met Word via COM

Pagina: 1
Acties:

  • MacWolf
  • Registratie: Januari 2004
  • Laatst online: 06-09-2024
Scenario: ik ben bezig met een applicatie aan het schrijven dat Word bestanden ontvangt van een klant en deze zal verwerken tot hoge kwaliteitsversies van het origineel. Van de hoge kwaliteitsversie wordt uiteindelijk een PDF gemaakt om af te drukken. De klant maakt gebruik van lage kwaliteitsversies, zodat het niet te veel vraagt van Word op het desbetreffende systeem.

Probleem: via COM lukt het me om een Word bestand te openen en bepaalde properties te wijzigen. Helaas lukt 1 ding me niet en ik begrijp niet waarom: ik krijg de wijzigingen niet bewaren, wat ik ook probeer. Helaas is COM niet uitzonderlijk gedocumenteerd en is de werking voor mij ook een beetje exotisch...

Code:
Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
   Private Function processWordDoc(ByVal order As Integer) As Boolean
        writeLog("Word document verwerken: " & order & ".doc")

        'Word algemeen
        Dim wordApp As New Word.Application
        Dim wordDoc As Word.Document

        'document properties
        Dim produktNr As Integer = -1

        'specifiek voor deze functie
        Dim intFieldCount As Integer = 0

        '...
        wordApp.Visible = True
        wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone

        Try
            wordDoc = wordApp.Documents.Open(wordDocDir & "\" & order & ".doc", False, False, False)
        Catch ex As Exception
            writeLog(ex)
            Exit Function
        End Try

        'controleer of alle benodigde comments aanwezig zijn in document
        Dim docCommentString As String = wordDoc.BuiltInDocumentProperties.Item("Comments").value
        Dim docComments() = docCommentString.Split(";")
        For Each comment As String In docComments
            'produkt veld
            If comment.StartsWith("produkt") Then
                intFieldCount += 1

                'moet iets worden verbeterd ... controle of er wel tekens na het '=' teken komen, of try/catch, 
                'of misschien iets anders ...
                If IsNumeric(comment.Substring(comment.IndexOf("=") + 1)) Then
                    produktNr = CInt(comment.Substring(comment.IndexOf("=") + 1))
                End If
            End If
        Next

        'controle of vereiste velden zijn ingevuld, zo niet, dan stappen we uit deze functie
        If Not intFieldCount = 1 Then
            writeLog("1 of meer vereiste velden zijn niet ingevuld in het Word bestand: ")
            If produktNr = -1 Then
                writeLog("* produkt")
                'mail sturen naar klant? zo ja, op een nette manier aan email adres komen (geen popMsg.FromEmail, dat zal niet goed werken)
            End If

            exitWord(wordApp, False)
            Exit Function
        End If

        'bekijk of achtergrondplaatje bestaat in database en op filesystem
        TblProductenTableAdapter.FillByNr(DsCmsRouw.tblProducten, produktNr)
        If DsCmsRouw.tblProducten.Rows.Count > 0 Then
            If Not My.Computer.FileSystem.FileExists(graphicDocDir & "\" & produktNr & ".jpg") Then
                writeLog("plaatje '" & produktNr & ".jpg' behorende bij produkt is niet gevonden in map: " & graphicDocDir)

                exitWord(wordApp, False)
                Exit Function
            End If
        End If

        'achtergrond wissen
        wordDoc.Background.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse

        'plaatje invoegen in Word bestand
        'rekening houden met colortype e.d. ?
        wordDoc.Background.Fill.ForeColor.RGB = RGB(255, 255, 255)
        wordDoc.Background.Fill.Transparency = 0.0#
        wordDoc.Background.Fill.UserPicture(graphicDocDir & "\" & produktNr & ".jpg")

        'bewaar bestand & sluit applicatie
        Try
            wordApp.ActiveDocument.SaveAs(wordDocDir & order & ".doc")
            wordApp.ActiveDocument.Close(Word.WdSaveOptions.wdSaveChanges, Word.WdOriginalFormat.wdWordDocument)
            exitWord(wordApp, True)
        Catch ex As Exception
            writeLog(ex)
        End Try
    End Function

    'sluit bestand en applicatie
    Private Function exitWord(ByVal wordApp As Word.Application, ByVal saveChanges As Boolean) As Boolean
        Try
            wordApp.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
            wordApp = Nothing
        Catch ex As Exception
            writeLog(ex)
        End Try
    End Function


Kan iemand me hiermee helpen? Waarom wordt het document niet bewaard, terwijl ik absoluut geen foutmelding terugkrijg?

[ Voor 12% gewijzigd door MacWolf op 23-02-2006 14:26 ]

Microsoft Windows: A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.


  • H!GHGuY
  • Registratie: December 2002
  • Niet online

H!GHGuY

Try and take over the world...

gebruik eens niet de ActiveDocument maar wordDoc.SaveAs(...)

probeer ook eens om Word visible te houden en alles op te volgen.
check dan ook de documentnaam die je gebruikt om op te slaan.

ASSUME makes an ASS out of U and ME


  • MacWolf
  • Registratie: Januari 2004
  • Laatst online: 06-09-2024
Helaas, werkt niet.

In eerste instantie gebruikte ik overigens ook gewoon wordDoc, maar had deze gewijzigd naar voorbeeld op een andere site ...

EDIT: Nog ff je tip gevolgd om te kijken of de bestandsnaam correct was, en daar lag inderdaad het probleem, een backslashje vergeten :(

Stom foutje ...

Visual Basic:
1
wordDoc.SaveAs(wordDocDir & "\" & order & ".doc")

[ Voor 15% gewijzigd door MacWolf op 23-02-2006 14:55 ]

Microsoft Windows: A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.


  • H!GHGuY
  • Registratie: December 2002
  • Niet online

H!GHGuY

Try and take over the world...

om zulke fouten tegen te gaan kijk je mss best even naar
System.IO.Path.Combine ;)

ASSUME makes an ASS out of U and ME