VFP Open docx associated with excel file

RRIOS 0 Reputation points
2023-03-22T15:51:10.89+00:00

I have a VFP 9 system. It generates a spreadsheet with data coming from the database. I have a docx file that is associated with this worksheet. In the docx I have some cells that I use the {if{margefield ....}....} to be able to show or not some fields in certain positions.

The problem: when I run the VFP code I want the docx to be opened associated with that worksheet, but that doesn't happen. The docx opens and is not associated with the worksheet. If I open this docx through windows explorer it opens correctly associated with the worksheet.

Copy To Sys(5)+Sys(2003)+'\planilhabasehistorico.xls' Type Xl5
	Local oExcel As Excel.Application 
	try
		oExcel = Createobject('Excel.application') 
		oExcel.Visible = .T. 						&& Deixa o excel visível
		oExcel.WindowState= -4137					&& Janela maximizada
		*oExcel.Workbooks.Open(Sys(5)+Sys(2003)+'\'+ALLTRIM(str(thisform.text1.value,3))+thisform.text2.value+'_'+Thisform.txtsemestre.value+'.xls')	&& abre teste.xls
		oExcel.Workbooks.Open(Sys(5)+Sys(2003)+'\planilhabasehistorico.xls')			
		oExcel.Cells.Select()						&& selecionas todas as células
		oExcel.Selection.Locked = .F.				&& "desbloqueio" todas (vem bloqueadas por default)
		oExcel.Columns("A:A").Select()				&& Seleciono a coluna A (código)
		oExcel.Selection.Locked = .T.				&& Bloqueio somente a coluna A
		oExcel.Columns("B:B").AutoFit()				&& Autofit na coluna B (nome)
		*oExcel.Quit
		*RELEASE oExcel
		TRY
			m.nameword = Sys(5)+Sys(2003)+'\'+ALLTRIM(tbcontrole.cnomearqhistorico)
			*If FILE(m.nameword)
			*try
				Local oWord As Word.Application 
				oWord = Createobject("word.application")
				*?oword.Documents.Count && 0
				*oword.Documents.add()
				*?oword.Documents.Count && 1
				*?oword.Documents.Item(1).Name
				oWord.Documents.open(m.nameword)
				oWord.Visible=.T.
			*catch
			*	MESSAGEBOX('O arquivo '+m.nameword+' não existe Verifique !',48,'Aviso do Sistema')
			*endtry
		CATCH
			MESSAGEBOX('Não foi possivel CRIAR o arquivo '+Sys(5)+Sys(2003)+'\'+m.nameword+' Verifique !',48,'Aviso do Sistema')
		endtry
	CATCH
		MESSAGEBOX('Excel / Office não pode ser aberto, Verifique !',48,'Aviso do Sistema')
	endtry
Microsoft 365 and Office Development Other
Microsoft 365 and Office Word For business Windows
Microsoft 365 and Office Excel For business Windows
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.