Open Word Window in VBA

Challis

New Member
Joined
Oct 22, 2017
Messages
21
Hi,
Below is an exert of a code that transfers data from Excel to word via a series of .bookmarks. The macro opens a word template (.docx), populates certain bookmarks and saves in a defined directory.
Everything is working fine, with exception, I cannot get the word window to open. When users are using the spreadsheet, they assume the macro has not worked as Word does not appear on their desktop. Is there a way to make word do this, so when user runs the code, word appears over the top of excel on their monitor.

Thanks

Code:
                    'F) Activate word
                    Set objWord = CreateObject("Word.Application")
                    Set objdoc = objWord.documents.Add
                    objWord.Visible = True
                    objWord.documents.Open Filename:=fpathtemplate & "\" & fnametemplate
                    With objWord.activedocument
                    .bookmarks("Date").Range.Text = wsRegister.Cells(btmrow, 11).Text
                    .bookmarks("ReportNumber").Range.Text = wsRegister.Cells(btmrow, 10).Text
                    .bookmarks("EquipmentNumber").Range.Text = wsRegister.Cells(btmrow, 7).Text
                    .bookmarks("Heading_2").Range.Text = wsRegister.Cells(btmrow, 7).Text
                    .bookmarks("Unit").Range.Text = wsRegister.Cells(btmrow, 6).Text
                    .bookmarks("Area").Range.Text = wsRegister.Cells(btmrow, 5).Text
                    .bookmarks("WorkOrder").Range.Text = wsRegister.Cells(btmrow, 3).Text
                    .bookmarks("Person").Range.Text = wsRegister.Cells(btmrow, 12).Text
                        Select Case wsRegister.Cells(btmrow, 9).Text
                        Case Is = "INT": .bookmarks("Heading_1").Range.Text = "Internal"
                        Case Is = "EXT": .bookmarks("Heading_1").Range.Text = "External"
                        End Select
                    .bookmarks("Extent").Range.Text = wsRegister.Cells(btmrow, 9).Text
                    .bookmarks("HazardLevel").Range.Text = Application.VLookup(wsRegister.Cells(btmrow, 7), Range("tableEQUIPMENTNUMBER"), 8, False)
                    End With
                    'G) Save document
                    objWord.activedocument.SaveAs Filename:=Fpath & "\" & FVariable & "\" & Fname
                    Set objWord = Nothing
                    End Select
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You have set the document to visible but you're killing your Word container object with the following...
Code:
Set objWord = Nothing
So maybe stop that. If you want Word to be in the foreground there's API code available. Google FnSetForegroundWindow. HTH. Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,198
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top