Ignore an "external" error when opening a Word document - vba

I'm attempting to copy the contents of a word document, but the document has embedded vba which causes a compile error anytime I open it.
The error reads
"The code in this project must be updated for use on 64-bit systems."
I expect this error to occur, so how do I tell my vba code in Access to ignore the errors without changing the word doc?
My vba code so far - the DisplayAlerts = wdAlertsNone does not work, neither does setting it to False:
Private Sub cmdQuickLtr_Click()
Dim wApp As Word.Application
Set wApp = CreateObject("Word.Application")
wApp.DisplayAlerts = wdAlertsNone
Dim doc As Object
Set doc = wApp.Documents.Open(Me.tbLetterPath.Value)
doc.Content.Copy
doc.Close
'do something with the copied content
wApp.DisplayAlerts = wdAlertsAll
Set doc = Nothing
Set wApp = Nothing
End Sub
Thank you all in advance.

Related

How to fill a text box in Microsoft Word with data from a MS Access form?

Hello I am having trouble with trying to auto fill a word document with data that is calculated from my MS Access form.
I have been following along with this Youtube video: https://www.youtube.com/watch?v=k176qcg-YCo and when I run the code it opens word but it gets stuck in a loop, I believe. The function is called when the button is clicked, the code is provided below. Any help is highly appreciated!
Function fillwordform()
Dim appword As Word.Application
Dim doc As Word.Document
Dim Path As String
On Error Resume Next
Error.Clear
Set appword = GetObject(, "word.application")
If Err.Number <> 0 Then
Set appword = New Word.Application
appword.Visible = True
End If
Path = "C:\Users\Jay\Documents\Monthly Update.docx"
Set doc = appword.Documents.Open(Path, , True)
With doc
.FormFields("ItmRec").Result = Me.NVCItmRcv
appword.Visible = True
appword.Activate
End With
Set doc = Nothing
Set appword = Nothing
End Function
The "ItmRec" is the name of the text box im trying to fill in Word and the "Me.NVCItmRcv" is the name of the box that I want to pull the number from and put in the text box. If you need anymore details or anything please let me know! Again highly appreciate any help!

Override Document_New() in Office 2010 from VBA

I'm writing some VBA code (in Excel) to automatically produce reports at work. These reports need to have a specific format, which is easily available through our Company templates. It's all in MS Office 2010.
However, all of these templates have Document_New() subs, which sparks some VBA-code that I don't want initiated, and sadly don't have direct access to (company rules, hell yeah!).
Is there a way to override the Document_New() macro? Right now, my VBA code for opening a document, which sparks the Document_New() macro, is as follows:
Sub GenerateReport()
Dim WordApp As Object
Set WordApp = CreateObject("Word.Application")
WordApp.Documents.Add Template:="Letter.dot" ' the "error" is produced at this lie
Dim SaveStr As String
SaveStr = "KommRapp-" & Date & ".docx"
WordApp.ActiveDocument.SaveAs SaveStr
End Sub
Try Something like:
Sub OpenWordDocAndBlockMacros()
Dim WordApp As Object
Set WordApp = CreateObject("Word.Application")
'Just to see whats happening
WordApp.Visible = True
Dim InitialSecurityAutoSetting As MsoAutomationSecurity
'Save Automations Security Setting
SecurityAutoSetting = WordApp.Application.AutomationSecurity
WordApp.Application.AutomationSecurity = msoAutomationSecurityForceDisable
WordApp.Documents.Add Template:=" Place Template file path here"
'Do some stuff with the template
End Sub
I have tested it, using Excel VBA to open a Word.dotm which has a Sub called Document_New in the ThisDocument module and it worked.

Close a word document that is already open

I'm trying to check whether a specific word document is open or not. If open, then close it and first then reopen again and if not then just open it.
The problem is that I always get a Bad file name 4160 error when it tries to close the document.
Would anyone plz tell me where exactly is the problem in my code.
Thank u in advance.
Dim wdApp As Object
Dim myDoc As Word.Document
Set wdApp = GetObject(, "Word.Application")
If IsFileOpen("C:\Letters\TemporaryLetter.docx") Then
wdApp.Documents("C:\Letters\TemporaryLetter.docx").Close
End If
With wdApp
.Visible = True
.WindowState = 2
End With
Set myDoc = wdApp.Documents.Open("C:\Letters\TemporaryLetter.docx")
Without seeing the function "IsFileOpen" that you are calling I"m guessing it requires the short name of the file (e.g. "TemporaryLetter.docx") not the full path?? Try:
If IsFileOpen("TemporaryLetter.docx") Then
wdApp.Documents("TemporaryLetter.docx").Close
End If
try this.
Set myDoc = word.Documents.Open ("C:\Letters\TemporaryLetter.docx")
If IsFileOpen(mydoc) Then
mydoc.Close(False //Here if you want to save your document or not)
End If

Call Word VBA MsgBox function from Excel

Is it possible to call Word's MsgBox function from Excel VBA? I am having trouble doing this as MsgBox is not a method of Word.Application.
I have tried this:
Dim objWord As Word.Application
Set objWord = New Word.Application
Dim objDoc As Word.Document
Set objDoc = objWord.Documents.Add
objWord.Visible = True
objWord.Activate
Call objWord.Run("MsgBox", "Hello World") ' Or:
Call objDoc.Run("MsgBox", "Hello World")
But I get the error "Object doesn't support this property or method", no matter which 'Call' line I include.
I am trying to do this so that I can display a message box in front of the open Word document saying that the document rendering has completed. If I just call Excel's MsgBox then I have to click on the flashing button in the Windows Taskbar before I can see the message box.
The word document is generated entirely by my Excel macro.
Is this possible?
I have some functions that export modules and re-import them. It is configured now for PPT but was hacked together from Excel functions that I found. There is also a way to use VBA to write VBA. Both are fairly advanced though. Probably both sets of functions are available here:
http://www.cpearson.com/excel/vbe.aspx
However, that's probably overkill. This is probably your best bet, to use a popup instead of a VBA msgBox.
Sub Test()
Dim wdApp As Object
Dim shl As Object
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
wdApp.Activate
Set shl = CreateObject("wscript.shell")
shl.Popup ("Hello, world!")
Set shl = Nothing
Set wdApp = Nothing
End Sub
See also here for more detail about how to control it's timeout/etc.
Whats the best way to display a message box with a timeout value from VBA?
It is! The problem with the way that you're doing it is that the run function of the word application looks for macros with that name and runs them. The way that I was able to accomplish this is by making a subroutine in the word doc that creates the message box.
Public Sub Testing()
Dim objWord As Word.Application
Set objWord = New Word.Application
Dim objDoc As Word.Document
Set objDoc = objWord.Documents.Open("C:\whatnot\Stuff.docm")
objWord.Visible = True
objWord.Activate
objWord.Run ("Testing")
End Sub
Then in the Word document:
Public Sub Testing()
MsgBox ("Hello World")
End Sub

Word vba document.readonly status incorrectly returns false

I have an excel project that checks word documents for a changed modify date and, if changed, it opens that document and imports the text from the word form fields into excel.
The routine in excel that opens and imports the word documents is as follows:
Sub CopyFromWord(pFile as String,aFile as string)
Dim wdApp As Object
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wdApp = CreateObject("Word.Application")
Else 'word is already running
End If
On Error Goto 0
Set wdDoc = wdApp.Documents.Open(Filename:="" & pFile & "", ReadOnly:=True)
wdApp.Visible = False
For Each c In wdDoc.bookmarks
'removed code that copies values from word doc fields to excel sheet
Next c
wdApp.Activedocument.Close SaveChanges:=False
End Sub
The word documents all started out as copies of the same file. There are a few thousand copies of the file, but each located in their own folder with a unique name.
The user finds the folder they need and opens the word document within it. It bring up a userform and then populates formfields in the document with the input to the userform. A command button then saves and exits the form.
Because the welcome message/userform loads automatically upon the document opening, I added the following code into the open event for the document:
Sub Document_Open()
If ThisDocument.ReadOnly = True then Exit Sub
msgbox "Welcome " & Environ$("Username") & ". Click OK to begin."
Userform1.show
End sub
This ensures when the excel project loops through all the files, if it finds one has changed, it needs to open the file (read only) so it can import the data without being interrupted with a userform / welcome message, close it, and carry on searching looping all files checking for changed modify-dates.
It should run constantly, however, about 20% of the time, a document will be opened read only by the excel code, but the welcome messagebox in the word document will show, indicating thisdocument.readonly incorrectly returned false.
If I debug the word document in this scenario, and do
? thisdocument.readonly
I get a "false" result. However, even the title bar of the word document ends with " (Read-Only)" so it has clearly been opened read-only, thus readonly should return True.
It is not specific to any documents, if I try to repeat opening them it seems to work the next time round (in that it correctly registers a read-only and exits the sub before the messagebox code). I cant find any kind of pattern and can't find any info online, I've been searching this for weeks!
May not be considered the answer, but following Tim William's suggestion, I managed to put together this which completely solves my problem. I struggled at first because I was trying to set the property too early. Complete code is as follows:
Sub CopyFromWord(pFile as String)
Dim wdApp As Object
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wdApp = CreateObject("Word.Application")
Else 'word is already running
End If
On Error Goto 0
'save current setting
secAutomation = wrdApp.Application.AutomationSecurity
'set Word to disable macros when a document is opened via vb:
wrdApp.Application.AutomationSecurity = msoAutomationSecurityForceDisable
'(without using wrdApp prefix it would only apply to the code's App i.e. Excel)
Set wdDoc = wdApp.Documents.Open(Filename:="" & pFile & "", ReadOnly:=True)
wdApp.Visible = False
For Each c In wdDoc.bookmarks
'removed code that copies values from word doc fields to excel sheet
Next c
'restore original setting before closing
wrdApp.Application.AutomationSecurity = secAutomation
wdApp.Activedocument.Close SaveChanges:=False
End Sub
Many thanks to Tim Williams for the link, and the guy who provided the code within the content of that link. This was such a help and is most appreciated.