Macro command to open html file using Microsoft Word

Macro command to open html file using Microsoft Word

iamcdn79
Mentor Mentor
2,525 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,526 Views
36 Replies
Replies (36)
Message 2 of 37

rafael.sansao
Advisor
Advisor

Hello,

On Windows, right-click on the HTML file > Open with > Choose another app > Select Word > Always

This will set Word as the default application for opening HTML files on your computer, and thus, the macro should work correctly.

Rafael Sansão

OptiNC - www.optinc.tech

EESignature

0 Likes
Message 3 of 37

iamcdn79
Mentor
Mentor

It's already set to Word

 

iamcdn79_0-1730827422398.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 4 of 37

rafael.sansao
Advisor
Advisor

Something went wrong. When Word is set as the default program for opening HTML, the HTML file icon changes to the Word icon.
When you double-click on the HTML file, the file has to be opened with Word...

Restart your computer and try again...


You can also change the default program through Control Panel > Default Programs

Rafael Sansão

OptiNC - www.optinc.tech

EESignature

0 Likes
Message 5 of 37

iamcdn79
Mentor
Mentor

I think the directory code is incorrect. The macro code is looking into 'C:/Powermill/1792/1792 cavity/SetupSheets_files/project_sheets/lineup sheet.html' directory which doesn't exist for the lineup sheet.html file 

 

The correct directory should be 'C:/Powermill/1792/1792 cavity/SetupSheets_files/project_sheets' to open the file called lineup sheet.html

 

When I change it to this code it opens a new file explorer window of that directory showing the lineup sheet.html file but doesn't open that file 

 

 

FUNCTION Main (STRING $Selected_NCProgram)
{
    // Execute preliminary commands
    KEEP SETUPSHEETS NCPROGRAM SELECTED
    TEXTINFO ACCEPT
    SPLITTER TABEXPLORE

    // Get the main project directory and construct the exact path
    STRING mainDirectory = dirname(project_pathname(0), "/")
    STRING projectSubfolder = project.Name
    STRING htmlFilePath = mainDirectory + "/" + projectSubfolder + "/SetupSheets_files/project_sheets"
    
    // Open the specified .html file if it exists
    
        OLE FILEACTION 'open' $htmlFilePath
}

 


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 6 of 37

rafael.sansao
Advisor
Advisor

Try this:

FUNCTION Main (STRING $Selected_NCProgram)
{
    // Execute preliminary commands
    KEEP SETUPSHEETS NCPROGRAM SELECTED
    TEXTINFO ACCEPT
    SPLITTER TABEXPLORE

    // 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"
    
    // Open the specified .html file if it exists
    OLE FILEACTION 'open' $htmlFilePath
}

Rafael Sansão

OptiNC - www.optinc.tech

EESignature

0 Likes
Message 7 of 37

iamcdn79
Mentor
Mentor

Same error as before because I think its looking in the wrong dir.

 

The correct dir should be 'C:\Powermill\1792\1792 cavity\SetupSheets_files\project_sheets' but the error code is showing dir called 'C:\Powermill\1792\1792
cavity\SetupSheets_files\project_sheets\lineup sheet.html' which doesn't exist

 

iamcdn79_1-1730837385732.png

 

This is the dir it should be pointing too for the file called lineup sheet.html

 

iamcdn79_0-1730837319898.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 8 of 37

nguyenthinhvt95
Advocate
Advocate

Hi @iamcdn79 ,

I tested your macro and it's working well when opening an HTML file by Excel.

in my opinion, the macro is OK, the problem is Word not suitable to open HTML.

0 Likes
Message 9 of 37

iamcdn79
Mentor
Mentor

If you change it to Word instead of Excel to open HTML does it still work?


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 10 of 37

iamcdn79
Mentor
Mentor

My macro doesn't even work for Excel files when I change the extension to .xlsx and Excel is already the default pgm for that extension.

 

iamcdn79_1-1730895898759.png

 

 

iamcdn79_0-1730895802466.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 11 of 37

rafael.sansao
Advisor
Advisor

Try this:

FUNCTION Main (STRING $Selected_NCProgram)
{
    // Execute preliminary commands
    KEEP SETUPSHEETS NCPROGRAM SELECTED
    TEXTINFO ACCEPT
    SPLITTER TABEXPLORE

    // 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 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
		OLE FILEACTION 'open' $cmd_path
		WAIT 1
		FILE DELETE $cmd_path
	}
}

Rafael Sansão

OptiNC - www.optinc.tech

EESignature

0 Likes
Message 12 of 37

iamcdn79
Mentor
Mentor

Yes! that opens, but i get an error on line 

 

 

 

FILE DELETE $cmd_path​

 

 

 

after the file opens.

 

Is it trying to delete the lineup sheet.html file or that directory that it exists? 

 

iamcdn79_0-1730901757860.png
 
Removing that line seems to run without issues.

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 13 of 37

rafael.sansao
Advisor
Advisor
Accepted solution
FUNCTION Main (STRING $Selected_NCProgram)
{
    // Execute preliminary commands
    KEEP SETUPSHEETS NCPROGRAM SELECTED
    TEXTINFO ACCEPT
    SPLITTER TABEXPLORE

    // 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 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
		OLE FILEACTION 'open' $cmd_path
		WAIT 1
		DELETE FILE $cmd_path
	}
}

Rafael Sansão

OptiNC - www.optinc.tech

EESignature

0 Likes
Message 14 of 37

iamcdn79
Mentor
Mentor

Here is the complete code

FUNCTION Main (STRING $Selected_NCProgram)
{

    IF ($powermill.Status.MultipleSelection.First) OR ($powermill.Status.MultipleSelection.Total == 0) {

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

    // 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 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
        }
    }
}

 

is there a way to close the microsoft word lineup sheet.html file if it is already one when running this code? 


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 15 of 37

rafael.sansao
Advisor
Advisor

You can try using Powershell to close the file, but you will need to select Powershell as the default program for executing ps1 files.

This will only work if your computer's group policy allows scripts to be executed.

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()'
			$ps1_content = $ps1_content + CRLF + '        break'
			$ps1_content = $ps1_content + CRLF + '    }'
			$ps1_content = $ps1_content + CRLF + '}'
			STRING $ps1_path = $mainDirectory + "\run.ps1"
			FILE OPEN $ps1_path FOR WRITE AS cmd
			FILE WRITE $ps1_content TO cmd
			FILE CLOSE cmd
            OLE FILEACTION 'open' $ps1_path
            WAIT 2
            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 16 of 37

iamcdn79
Mentor
Mentor

So I did all that and I don't get any errors but it doesan't close the word file linup sheet.html when it is open.

 

I think it could because when I always close it manually its asking me if it needs to be saved or not due to a macro I have which runs and changes values automatically everytime it opens that particular file.

 

Is there a way to add a no to not to save the file then close it?

 

iamcdn79_0-1730981777992.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 17 of 37

rafael.sansao
Advisor
Advisor

Replace:

$ps1_content = $ps1_content + CRLF + '        ' + $char + 'doc.Close()'

By:

$ps1_content = $ps1_content + CRLF + '        ' + $char + 'doc.Close(0)'

Rafael Sansão

OptiNC - www.optinc.tech

EESignature

0 Likes
Message 18 of 37

iamcdn79
Mentor
Mentor

No change


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 19 of 37

rafael.sansao
Advisor
Advisor
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 = $mainDirectory + "\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 = $mainDirectory + "\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 20 of 37

iamcdn79
Mentor
Mentor

No go.

 

Would a lockfile be preventing it from closing? When I have the file open there is a hidden file in the dir called "~$nep sheet.html"

 

iamcdn79_1-1730994116818.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