-
Jul 4th, 2022, 11:01 PM
#1
Thread Starter
New Member
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
-
Jul 5th, 2022, 03:27 AM
#2
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
-
Jul 5th, 2022, 06:01 AM
#3
Thread Starter
New Member
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
-
Jul 5th, 2022, 06:22 AM
#4
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.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
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
-
Jul 5th, 2022, 09:35 AM
#5
Thread Starter
New Member
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
-
Jul 5th, 2022, 10:04 AM
#6
Hyperactive Member
Re: Open document Word with macro (.docm)
Remove this line:
Code:
Set mWord = Nothing
This line must be used when exiting the program.
-
Jul 5th, 2022, 11:13 AM
#7
Re: Open document Word with macro (.docm)
Originally Posted by Argus19
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. To all, peace and happiness.
-
Jul 5th, 2022, 11:21 AM
#8
Re: Open document Word with macro (.docm)
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.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
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
-
Jul 5th, 2022, 11:30 AM
#9
Re: Open document Word with macro (.docm)
Originally Posted by Zvoni
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. To all, peace and happiness.
-
Jul 6th, 2022, 02:13 AM
#10
Re: Open document Word with macro (.docm)
Originally Posted by Elroy
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
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
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
-
Jul 6th, 2022, 03:33 AM
#11
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
-
Jul 6th, 2022, 06:05 AM
#12
Thread Starter
New Member
Re: Open document Word with macro (.docm)
Originally Posted by westconn1
can you open the docx or docm files if you do it manually from windows?
yes open
-
Jul 6th, 2022, 06:09 AM
#13
Thread Starter
New Member
Re: Open document Word with macro (.docm)
Originally Posted by Zvoni
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"
-
Jul 6th, 2022, 06:37 AM
#14
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
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
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
-
Jul 7th, 2022, 04:33 AM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|