Share via

How can I "interpret or convert" an Object variable to a Word.Document ?

KeldSor 396 Reputation points
2023-07-06T06:43:12.44+00:00

In fact it's a FileSystemObject I want to interpret as an Word.Document in VBA !

How can I do that ?

Microsoft 365 and Office | Development | Other

2 answers

Sort by: Most helpful
  1. KeldSor 396 Reputation points
    2023-07-06T14:13:10.3133333+00:00

    @Olaf Helper

    OK, maybe it's not posible in VBA to 'view' one type of object as another object so I tried something else.

    I have this snippet of code:

    Sub prepareDoc(fn As String)
        Dim WordApp As New Word.Application
        Dim doc As Document
        Set doc = WordApp.Documents.Open(fn)      <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< (A)
        InsertAndUpdateFieldsInFooter doc
        doc.Save
        doc.Close
        Set doc = Nothing
        WordApp.Quit
        Set WordApp = Nothing
    End Sub
    
    

    I know for a fact that:

    1. "InsertAndUpdateFieldsInFooter doc" works as designed because I use it from an event I've set up in Word.
    2. My code for traversing the folder hieraki and returning med file names one by one of the Word files in the folder hieraki works too because I've tested that it returnes all the Word files names in the folder hieraki.

    The ONLY thing left now is to "open the returned file name in a hidden Word instance, do the editing of the footer, save the document and quit Word application.

    I think the code above is "by the book" and it should do that ...

    but the line marked (A) above wont let me do it - it starts an instanse with this dialog locking it all up:

    billede

    and I CANT do something about that in the "Security center ..." - I've tried, it makes NO difference !

    SOMETHING is wrong here - but what is it ?

    Was this answer helpful?

    0 comments No comments

  2. Olaf Helper 47,621 Reputation points
    2023-07-06T06:47:43.0966667+00:00

    Your requirements are not clear for me?

    A FileSystemObject is something very different then a Word.Document object.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.