Results 1 to 15 of 15

Thread: Open document Word with macro (.docm)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    5

    Open document Word with macro (.docm)

    Hello

    Is it possible to open a word document with macro (.docm) with VB6?
    I have a merged document that opens in .doc but in .docm it is blank.
    Thank you

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,617

    Re: Open document Word with macro (.docm)

    it may be that a dialogue asking the user to allow macros is being shown, as it is not visible to be dismissed the document is just hanging waiting for response

    test by reducing the security level in the word application
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    5

    Re: Open document Word with macro (.docm)

    I have disabled all security in word.
    It doesn't even open .docx files, only .doc.
    The word opens but in gray, without any document.
    I don't know what could be the error

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    3,227

    Re: Open document Word with macro (.docm)

    How are you trying to open the docm?
    ShellExecute?
    Or with a COM-Call? In that Case which Object-Lib is referenced in VB6?
    Last edited by Zvoni; Jul 5th, 2022 at 06:31 AM.
    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    5

    Re: Open document Word with macro (.docm)

    the code is not mine, this is what it uses to open word documents (in a .bas module)
    Code:
    Option Explicit
    Dim mWord As Word.Application
    Dim miP As Integer
    
    Public Sub ObrirArxiu(ByVal ls_doc As String)
    On Error GoTo err_obrir
    
    If ls_doc <> "" Then
        Set mWord = New Word.Application
        mWord.Documents.Open ls_doc
        mWord.Documents(ls_doc).Activate
        mWord.Documents.Application.visible = True
        mWord.Documents.Application.WindowState = wdWindowStateMaximize
        Set mWord = Nothing
    End If
    
    Exit Sub
    err_obrir:
        MsgBox "S'ha produit un error al obrir el fitxer", vbExclamation, "Error obrir fitxer"
    End Sub

  6. #6
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    361

    Re: Open document Word with macro (.docm)

    Remove this line:
    Code:
    Set mWord = Nothing
    This line must be used when exiting the program.

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    7,921

    Re: Open document Word with macro (.docm)

    Quote Originally Posted by Argus19 View Post
    Remove this line:
    Code:
    Set mWord = Nothing
    This line must be used when exiting the program.
    Well, that's not absolutely true. The code he's got (including uninstantiating the mWord object) looks like it should open the .docm file. The VB6 app will just no longer have any control of it. And, it's sort of bad practice to do that.

    If you want to open it through VB6 automation, I'd recommend closing the actual .docm file when you're done with the automation. And, if you're not really interested in automation, then opening the .docm file with ShellExecute is a much better way to go. There are many examples in these forums on how to use ShellExecute.

    And just as an FYI, my primarily application opens and closes these .docm files everyday. A template with macros is one of the primary documents in the application. (It's actually an Excel template with macros (xltm), but the principle is exactly the same.) And, I open these both with VB6 automation and with ShellExecute.
    Last edited by Elroy; Jul 5th, 2022 at 11:17 AM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  8. #8
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    3,227

    Re: Open document Word with macro (.docm)

    Quote Originally Posted by Elroy;[URL="tel:5573279"
    5573279[/URL]]Well, that's not absolutely true. The code he's got (including uninstantiating the mWord object) looks like it should open the .docm file. The VB6 app will just no longer have any control of it. And, it's sort of bad practice to do that.

    If you want to open it through VB6 automation, I'd recommend closing the actual .docm file when you're done with the automation. And, if you're not really interested in automation, then opening the .docm file with ShellExecute is a much better way to go. There are many examples in these forums on how to use ShellExecute.

    And just as an FYI, my primarily application opens and closes these .docm files everyday. A template with macros is one of the primary documents in the application. (It's actually an Excel template with macros (xltm), but the principle is exactly the same.) And, I open these both with VB6 automation and with ShellExecute.
    Not if his Word-Library is too „old“
    remember, OP said he can open „doc“ but not „docx“
    Last edited by Zvoni; Jul 6th, 2022 at 01:43 AM.
    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    7,921

    Re: Open document Word with macro (.docm)

    Quote Originally Posted by Zvoni View Post
    Not of his Word-Library is too „old“
    remember, OP said he can open „doc“ but not „docx“
    Ahhh, yes, good point. Keoki, you need to make sure that Word 2007 or later is installed on the computer, or you won't be able to open these .docx, .docm, .dotx, nor .dotm files. That's when Microsoft rolled out their XML file saving method. Any version of Word older than 2007, and you're stuck with the older .doc/.dot files.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  10. #10
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    3,227

    Re: Open document Word with macro (.docm)

    Quote Originally Posted by Elroy View Post
    Ahhh, yes, good point. Keoki, you need to make sure that Word 2007 or later is installed on the computer, or you won't be able to open these .docx, .docm, .dotx, nor .dotm files. That's when Microsoft rolled out their XML file saving method. Any version of Word older than 2007, and you're stuck with the older .doc/.dot files.
    His line
    Code:
    Option Explicit
    Dim mWord As Word.Application  '--> This one
    Dim miP As Integer
    
    Public Sub ObrirArxiu(ByVal ls_doc As String)
    On Error GoTo err_obrir
    implies early binding, meaning a set reference to the Word-"Library"
    ....and we're back at "early vs. late binding"
    Elroy's point of having a "current" Office-product installed not withstanding
    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,617

    Re: Open document Word with macro (.docm)

    can you open the docx or docm files if you do it manually from windows?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    5

    Re: Open document Word with macro (.docm)

    Quote Originally Posted by westconn1 View Post
    can you open the docx or docm files if you do it manually from windows?
    yes open

  13. #13

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    5

    Re: Open document Word with macro (.docm)

    Quote Originally Posted by Zvoni View Post
    His line
    Code:
    Option Explicit
    Dim mWord As Word.Application  '--> This one
    Dim miP As Integer
    
    Public Sub ObrirArxiu(ByVal ls_doc As String)
    On Error GoTo err_obrir
    implies early binding, meaning a set reference to the Word-"Library"
    ....and we're back at "early vs. late binding"
    Elroy's point of having a "current" Office-product installed not withstanding
    The version of word is 2016. The library activate in VB6 is "Microsoft Word 16 Object Library"

  14. #14
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    3,227

    Re: Open document Word with macro (.docm)

    Does a shellexecute work?
    The only other thing coming to mind is the transition from MDI to SDI in Office, and if it would behave differently in vb6/vba
    Try (Untested)
    Code:
    Option Explicit
    Dim mWord As Word.Application
    Dim mDoc As Word.Document
    Dim miP As Integer
    
    Public Sub ObrirArxiu(ByVal ls_doc As String)
    On Error GoTo err_obrir
    
    If ls_doc <> "" Then
        Set mWord = New Word.Application
        Set mDoc = mWord.Documents.Open(ls_doc)  'Documents.Open has a lot of optional Arguments, IIRC there is even one to Answer "Yes" to Macro-Security
        mWord.Visible = True    
        mWord.WindowState = wdWindowStateMaximize
        'Set mWord = Nothing
    End If
    
    Exit Sub
    err_obrir:
        MsgBox "S'ha produit un error al obrir el fitxer", vbExclamation, "Error obrir fitxer"
    End Sub
    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,617

    Re: Open document Word with macro (.docm)

    The version of word is 2016. The library activate in VB6 is "Microsoft Word 16 Object Library"
    that object library refers to word 2019, whether this is relevant to your problem is unclear
    maybe you can try late binding to see if it changes anything

    change like
    Code:
    Dim mword As Object, mdoc As Object
    Private Sub Command1_Click()
    Set mword = CreateObject("word.application")
    Set mdoc = mword.documents.open("C:\Users\user\Documents\checklist.doc.docx")
    mword.Visible = True
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width