Share via

Inserting VBA code in ThisDocument by VBA when doc's created ??

Anonymous
2019-11-05T18:45:25+00:00

I want to insert some VBA code into ThisDocument in the Open method.

The code should just call a function in the Normal.Dot.

Today I use this code in Access to create the document for the first time ( doc is Dim as Word.Document somewhere else)

    Set WRDobj = CreateObject("Word.Application")

    '        WRDobj.Visible = False

    Set doc = WRDobj.Documents.add

    doc.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = header

    doc.SaveAs PathOgFilnavn, wdFormatDocumentDefault

    WRDobj.Quit

    Set doc = Nothing

    Set WRDobj = Nothing

What shall I do to insert this:

Private Sub Document_Open()

    updatePathToMainFolder ActiveDocument

End Sub

into the Open-method by VBA in Access and save the document without any warnings ?

Moved from: More Office apps / Windows 10 / Unknown/other

Microsoft 365 and Office | Install, redeem, activate | For home | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
2019-11-06T09:12:31+00:00

Put the code in an autoopen macro in the template from which the document is created.  The code in that macro will run whenever such a document is opened, even one that was created from that template before the macro was created in it.

You probably should create the documents from a template other than the Normal.dotm template as if you put it in the Normal.dotm template, it will run when ALL documents that were created from that template are opened, not only those that are created from your data base.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2019-11-06T10:36:06+00:00

    Wav, it looks like exactly what I need - thx !

    In the first place I'll just do the coding in the Normal.dotm and see if it'll be a problem - if so I'll change it.

    EDIT: ... AND IT WORKS LIKE A CHARM - THX again !

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2019-11-06T08:45:52+00:00

    Oh, I now reallized that inserting (manually just to try) some VBA code in ThisDocument will change the extention to ".docm" - resulting in a new file - then I have to deal with all my existing docs ...  !!!!

    OK, that's no problem if I can insert the code at the CREATION of my furure docs - then I'll just have to change my existing docs - that's doable though tidious.

    MAYBE: Is there a way to do some coding in the Normal.dotm by whitch you can find out when a doc is opened but WITHOUT any VBA code in the document itself  - if so, then I can still use my existing docs without changing them ?

    Was this answer helpful?

    0 comments No comments