Macro command to open html file using Microsoft Word

Macro command to open html file using Microsoft Word

iamcdn79
Mentor Mentor
2,550 Views
36 Replies
Message 1 of 37

Macro command to open html file using Microsoft Word

iamcdn79
Mentor
Mentor

I just posted this 2 mins ago and it got deleted by someone in the forum page, so sorry if its not as detailed cause I am furious right now about it getting deleted

 

I am trying to create a macro that opens HTML files using Microsoft Word but get an error saying no program appears to be associated... 

 

Does anyone know the right command to open HTML files?

 

FUNCTION Main (STRING $Selected_NCProgram)

{
    // Execute preliminary commands
    KEEP SETUPSHEETS NCPROGRAM SELECTED
    TEXTINFO ACCEPT
    SPLITTER TABEXPLORE

    // Get the main project directory and construct the correct path
    STRING mainDirectory = dirname(project_pathname(0), "/")
    STRING projectSubfolder = project.Name + "" 
    STRING htmlFilePath = mainDirectory + "/" + projectSubfolder + "/SetupSheets_files/project_sheets/lineup sheet.html"
    
    // Attempt to open the specified .html file in the correct directory
    OLE FILEACTION 'open' $htmlFilePath
}

 

iamcdn79_1-1730813430496.png

 

 


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Accepted solutions (2)
2,551 Views
36 Replies
Replies (36)
Message 21 of 37

rafael.sansao
Advisor
Advisor

On my computer, everything works as expected, so I believe there may be a group policy restriction on your system.

Let’s try the following steps:

- Comment out this line: //DELETE FILE $ps1_path
- Run the macro.
- Open the Word file manually.
- In the project directory, locate the file named run.ps1, right-click it, and select "Run with PowerShell".

This should close the Word file. If it doesn’t, edit the "run.ps1" file and add this line at the end:
Pause

 

Then, run the file again by right-clicking and selecting "Run with PowerShell".

Please send me a screenshot of the PowerShell window if you encounter any issues.

Rafael Sansão

OptiNC - www.optinc.tech

EESignature

0 Likes
Message 22 of 37

iamcdn79
Mentor
Mentor

When I add the pause in the ps1 file and run it, it doesn't pause, a window quickly opens and closes so I can't see what it says. I had to use a screen recorder then pause it and take a screenshot

 

this is where I add the pause in the ps1 file 

 

iamcdn79_0-1731067265360.png

 

this is the screen shot of the ps1 being ran manually 

 

iamcdn79_1-1731068571458.png

 

 but it still doesn't close the Word automatically

 


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Message 23 of 37

rafael.sansao
Advisor
Advisor
Accepted solution

Try this:

FUNCTION Main (STRING $Selected_NCProgram)
{
    // Get the main project directory and construct the exact path
    STRING $mainDirectory = REPLACE(project_pathname(0), "/", "\")
    STRING $htmlFilePath = $mainDirectory + "\SetupSheets_files\project_sheets\lineup sheet.html"
	
    IF ($powermill.Status.MultipleSelection.First) OR ($powermill.Status.MultipleSelection.Total == 0) {
		IF file_exists($htmlFilePath) {
			STRING $char = "$"
			STRING $ps1_content = $char + 'path = "' + $htmlFilePath + '"'
			$ps1_content = $ps1_content + CRLF + $char + 'word = [Runtime.InteropServices.Marshal]::GetActiveObject("Word.Application")'
			$ps1_content = $ps1_content + CRLF + 'foreach (' + $char + 'doc in ' + $char + 'word.Documents) {'
			$ps1_content = $ps1_content + CRLF + '    if (' + $char + 'doc.FullName -eq ' + $char + 'path) {'
			$ps1_content = $ps1_content + CRLF + '        ' + $char + 'doc.Close(0)'
			$ps1_content = $ps1_content + CRLF + '        break'
			$ps1_content = $ps1_content + CRLF + '    }'
			$ps1_content = $ps1_content + CRLF + '}'
			STRING $ps1_path = "C:\temp\run.ps1"
			FILE OPEN $ps1_path FOR WRITE AS cmd
			FILE WRITE $ps1_content TO cmd
			FILE CLOSE cmd
			OLE FILEACTION 'open' $ps1_path
			STRING $cmd_content = "@echo off" + CRLF + 'powershell -ExecutionPolicy Bypass -File "' + $ps1_path + '"'
			STRING $cmd_path = "C:\temp\run.cmd"
			FILE OPEN $cmd_path FOR WRITE AS cmd
			FILE WRITE $cmd_content TO cmd
			FILE CLOSE cmd
			OLE FILEACTION 'open' $cmd_path
			WAIT 2
			DELETE FILE $cmd_path
			DELETE FILE $ps1_path
		}
		// Execute preliminary commands
		KEEP SETUPSHEETS NCPROGRAM SELECTED
		TEXTINFO ACCEPT
		SPLITTER TABEXPLORE
	}

    IF NOT file_exists($htmlFilePath) {
		MESSAGE WARN "File not exists"
		MACRO ABORT
	} ELSE {
		STRING $cmd_content = "@echo off" + CRLF + 'start "" "' + $htmlFilePath + '"'
		STRING $cmd_path = $mainDirectory + "\run.cmd"
		FILE OPEN $cmd_path FOR WRITE AS cmd
		FILE WRITE $cmd_content TO cmd
		FILE CLOSE cmd

         //Check if it is the last selection or if there are no items selected
        IF ($powermill.Status.MultipleSelection.Last) OR ($powermill.Status.MultipleSelection.Total == 0) {
            OLE FILEACTION 'open' $cmd_path
            WAIT 1
            DELETE FILE $cmd_path
        }
    }
}

Rafael Sansão

OptiNC - www.optinc.tech

EESignature

0 Likes
Message 24 of 37

iamcdn79
Mentor
Mentor

Still the same thing. Do I have the right scripts enabled in the group policy?

 

iamcdn79_0-1731071236517.png

 


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Message 25 of 37

rafael.sansao
Advisor
Advisor

- Run Powershell as administrator
- Type: Set-ExecutionPolicy Unrestricted
- Click enter
- Type Y , click enter

Rafael Sansão

OptiNC - www.optinc.tech

EESignature

0 Likes
Message 26 of 37

iamcdn79
Mentor
Mentor

Is the red suppose to give this msg? 

iamcdn79_0-1731076996028.png

 


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Message 27 of 37

rafael.sansao
Advisor
Advisor

It shouldn't.
Type: Get-ExecutionPolicy -List
And show me the image

Rafael Sansão

OptiNC - www.optinc.tech

EESignature

0 Likes
Message 28 of 37

iamcdn79
Mentor
Mentor

iamcdn79_0-1731077591640.png

 


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Message 29 of 37

rafael.sansao
Advisor
Advisor

The error you're receiving indicates that the execution policy was successfully updated, but it's being overridden by a policy defined at a more specific scope. This means a policy set at a higher level (such as in Group Policy or at the `LocalMachine` level) is taking precedence.

To investigate and fix this, follow these steps:

### 1. Check the execution policies at all scopes:

Run the following command to list all the execution policy settings for different scopes:

```powershell
Get-ExecutionPolicy -List
```

This will display something like:

```
Scope ExecutionPolicy
----- -----------------
MachinePolicy AllSigned
UserPolicy AllSigned
Process Undefined
CurrentUser RemoteSigned
LocalMachine RemoteSigned
```

### 2. Identify where the override is happening:

- If **MachinePolicy** or **UserPolicy** is set to `AllSigned`, this indicates that a Group Policy is enforcing that setting.

### 3. How to resolve:

#### If you're an administrator:
- To fix this, you will need to change the Group Policy setting that is enforcing the execution policy. If a Group Policy is set to `AllSigned` or another policy, it will take precedence.

To modify Group Policy settings:

1. Open the **Local Group Policy Editor** (`gpedit.msc`).
2. Navigate to:
```
Computer Configuration > Administrative Templates > Windows Components > Windows PowerShell
```
3. Check the settings for the policy **"Allow all Windows PowerShell scripts to run"**. If needed, adjust it to allow script execution.

#### If you're not an administrator or don't want to modify Group Policy:
- Unfortunately, the Group Policy set at a higher level cannot be overridden by user-level settings unless you have administrator permissions to modify the policy.

If adjusting the execution policy is necessary, you may need to request the administrator to modify the GPO settings or use the `Set-ExecutionPolicy` command with administrative privileges.

Rafael Sansão

OptiNC - www.optinc.tech

EESignature

0 Likes
Message 30 of 37

iamcdn79
Mentor
Mentor

@rafael.sansao wrote:

 

### 3. How to resolve:

#### If you're an administrator:
- To fix this, you will need to change the Group Policy setting that is enforcing the execution policy. If a Group Policy is set to `AllSigned` or another policy, it will take precedence.

To modify Group Policy settings:

1. Open the **Local Group Policy Editor** (`gpedit.msc`).
2. Navigate to:
```
Computer Configuration > Administrative Templates > Windows Components > Windows PowerShell
```
3. Check the settings for the policy **"Allow all Windows PowerShell scripts to run"**. If needed, adjust it to allow script execution.

 

 


I don't have this option

 

iamcdn79_0-1731078654068.png

 


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Message 31 of 37

iamcdn79
Mentor
Mentor

does it maybe mean for this when right-clicking "Turn on Script Execution"? 

 

iamcdn79_0-1731078816347.png

 


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Message 32 of 37

rafael.sansao
Advisor
Advisor

Apparently so. You have to try it!

Rafael Sansão

OptiNC - www.optinc.tech

EESignature

0 Likes
Message 33 of 37

iamcdn79
Mentor
Mentor

That was the problem, changing it to "allow all scripts" from "allow only signed scripts" macro code works now.

 

Now, does this make me more susceptible to viruses, malware, hacker attacks by enabling this powershell script?


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Message 34 of 37

iamcdn79
Mentor
Mentor

I changed the setting to "Allow local scripts and remote signed scripts" which gives me a better level of security than "Allow All" and the code still runs correctly


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

Message 35 of 37

iamcdn79
Mentor
Mentor

Thanks for all your help Rafael, appreciate it. 


Intel Core i9 13900KF CPU
128 GB Kingston Beast DDR4 SDRAM
PNY RTX A2000 6GB Video Card
WD 1 TB SSD Hard Drive
Windows 11 Pro

0 Likes
Message 36 of 37

rafael.sansao
Advisor
Advisor

You're welcome, we're here to help each other.

Rafael Sansão

OptiNC - www.optinc.tech

EESignature

0 Likes
Message 37 of 37

realestatejot
Community Visitor
Community Visitor
 

To open an HTML file using Microsoft Word via a macro, you can use the following VBA (Visual Basic for Applications) command. First, open Microsoft Word and press Alt + F11 to open the VBA editor. Then, insert a new module and paste this code:

Save and run the macro to open the specified HTML file in Microsoft Word. This method allows easy integration of HTML files within Word documents for editing or formatting.

0 Likes