nevinl has asked for the wisdom of the Perl Monks concerning the following question:

Good evening all...

I've been trying to set the Windows default file path and the default windows printer for an automated Word application via OLE.

I've been successful with the rest of the application, but for whatever reason, I've not been able to set these too parameters...it seems that I either receive a: Can't modify non-lvalue subroutine call at .... or something nothing happens at all.

All of the searching online seem to use the traditional syntax within perl, vb, etc, but alas, I've not had any luck. One article said that \s had to be at the end, but by default, there are not present. Another article said that an Add had to be one prior, but that hasn't helped, either.

Portions were adapted from another node 495229, Activestate VB code, MSDN, and numerous articles online, but none referring to this l-value problem...even when using the same syntax...

What am I missing?

Thanks in advance for any help...!

$word = CreateObject Win32::OLE 'Word.Application' or die $!; $word->{DisplayAlerts} = 0; if (${$env_ref}{DEBUG_AUDIT_REPORT_VISIBLE}) { $word->{'Visible'} = 1; } $document = $word->Documents->Add; ## Change the default save location: ##$word->Options->DefaultFilePath(wdDocumentsPath); my ($path_current); my ($path_default) = '\\\\trhmc-fs04\\users04\\u02904'; my ($path_new) = 'C:\\Documents'; ## Outputs: Default save path: (\\trhmc-fs04\users04\u02904) print "Default save path: (". $word->Options->DefaultFilePath(wdDocume +ntsPath) . ")\n"; ## Outputs: Default save path: (\\trhmc-fs04\users04\u02904) $path_current = $word->Options->DefaultFilePath(wdDocumentsPath); print "Default save path: (". $path_default . ")\n"; ## Re-assign the default path: ## Outputs: Can't modify non-lvalue subroutine call at report_db_devel +opment.pl line 1983. #$word->Options->DefaultFilePath(wdDocumentsPath) = $path_new; #print "New save path: (". $word->Options->DefaultFilePath(wdDocuments +Path) . ")\n"; ## Re-assign the default path to the SAME thing ## Outputs: Can't modify non-lvalue subroutine call at report_db_devel +opment.pl line 1994. #$word->Options->DefaultFilePath(wdDocumentsPath) = $path_default; #print "New save path: (". $word->Options->DefaultFilePath(wdDocuments +Path) . ")\n"; ## Outputs: Default new path: (\\trhmc-fs04\users04\u02904) $word->Options->DefaultFilePath( { 'wdDocumentsPath' => $path_new, }); print "New save path: (". $word->Options->DefaultFilePath(wdDocumentsP +ath) . ")\n"; ## Outputs: Default new path: (\\trhmc-fs04\users04\u02904) $word->Options->DefaultFilePath( { 'Path' => $path_new, }); print "New save path: (". $word->Options->DefaultFilePath(wdDocumentsP +ath) . ")\n"; ## Outputs: Use of unitilized value in print at report_db_development. +pl line 2014. print $word->Options->DefaultFilePath; ## Outputs: Use of unitilized value in print at report_db_development. +pl line 2018. print $word->Options->{'DefaultFilePath'}; ## Outputs: Use of unitilized value in print at report_db_development. +pl line 2021. print $word->Options->DefaultFilePath(); ## Outputs: \\trhmc-fs04\users04\u02904 print $word->Options->DefaultFilePath(wdDocumentsPath); ## Outputs: Default save path: (\\trhmc-fs04\users04\u02904) $path_new = $word->Options->DefaultFilePath(wdDocumentsPath); print "Default save path: (". $word->Options->DefaultFilePath(wdDocume +ntsPath) . ")\n"; foreach $path_new ( 'C:\Documents', 'C:\\Documents', 'C:\Documents\\', 'C:\\Documents\\', "C:\\Documents", "C:\\Documents\\", ) { ## Cannot modify non-lvalue subroutine call at ... #$word->Options->DefaultFilePath(wdDocumentsPath) = $path_new; print "Default save path: (". $word->Options->DefaultFilePath(wdDo +cumentsPath) . ")\n"; $word->Options->DefaultFilePath( { 'Path' => $path_new, 'wdDocumentsPath' => $path_new, 'Path:=wdDocumentsPath' => $path_new, }); print "Default save path: (". $word->Options->DefaultFilePath(wdDo +cumentsPath) . ")\n"; } ## Outputs: \\TRHMC-IMGDIS1\ECRTJPLF on NE01: print $word->{ActivePrinter}; ## Outputs: \\TRHMC-IMGDIS1\ECRTJPLF on NE01: my ($default_printer) = $word->{ActivePrinter}; print $default_printer; ## Outputs: \\TRHMC-IMGDIS1\ECRTJPLF on NE01: $word->{ActivePrinter} = '\\trhmc-prt6\EDNLJP5L'; print "Default printer: $word->{ActivePrinter}\n";
Saved macro when recording within Word:
Options.DefaultFilePath(Path:=wdDocumentsPath) = _ "\\trhmc-fs04\users04\U02904\audit_basic\cgi-bin\" ActivePrinter = "PDFill PDF Writer" ActivePrinter = "\\TRHMC-IMGDIS1\ECRTJPLF" ActivePrinter = "\\trhmc-prt6\EDNLJP5L"
and the result of the Object Browser within VB:

Property DefaultFilePath(Path As WdDefaultFilePath) as String

Member of Word.Options Property ActivePrinter as String

Member of Word.Application

Replies are listed 'Best First'.
Re: Word wdDocumentsPath and ActivePrinter
by stiller (Friar) on Feb 09, 2008 at 09:43 UTC
    $word->Options->DefaultFilePath(wdDocumentsPath)

    is the syntax of a subroutine call, and

    $word->Options->DefaultFilePath(wdDocumentsPath) = $path_default;

    is an attempt to assign to a subroutine call. That is possible for specially prepared subroutines, like for example substr, but here the error message tells it is not ok.

    I don't have Word here now, but I suspect that something like

    $word->Options->{DefaultFilePath}->{wdDocumentsPath} = $path_default;

    e.g. setting an option rather than calling a sub, will do what you want. hth

      $word->Options->{DefaultFilePath}->{wdDocumentsPath} = $path_default;


      same output...I don't know if I understand the data structure, but it seems as though it is adding a branch to the original hash...not modifying an existing element?

      for instance:
      $word->Options->DefaultFilePath->{wdDocumentsPath} = $path_new; ---> output: Can't use an undefined value as a HASH reference at ....


      I thought that perhaps it was since I could not have access to regedit, maybe I couldn't modify the registry...but I'm able to modify it within Microsoft Word->options->Save

      Not that it matters, but this I've been trying it as console window and within a web application...
        Well, it's not the same output, allthough the net result is the same.

        I'm not very familiar with office automation, but I wonder if you have winword running in this last instance?

        Do you:

        use strict; use warnings; use Win32::OLE; # brings in constants like wdDocumentsPath use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 3; # reports OLE errors my $word = Win32::OLE->GetActiveObject('Word.Application') or Win32::OLE->new('Word.Application', 'Quit'); print "word is not running\n" unless defined $word;

        It's a subtle change in syntax thats needed I guess, but I can't find anything on google or here now that answers your particular question.

        good luck!

Re: Word wdDocumentsPath and ActivePrinter
by stiller (Friar) on Feb 10, 2008 at 11:55 UTC
    phew, I got my windows computer here now, and a solution.

    The syntax to set the path is

    $word->Options->SetProperty("DefaultFilePath", wdDocumentsPath, 'C:\\stiller');

    Turns out this change is permanent, so you might want to save the original path first, then setting it, before eventually resetting it to what it was before your script started:

    use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 3; # report OLE runtime errors my $word = (Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit')); print "word is not running \n" unless defined $word; $word->{DisplayAlerts} = 0; # keep the old default so we can reset later my $oldDocPath = $word->Options->DefaultFilePath(wdDocumentsPath); print "Default save path was: $oldDocPath \n"; my $newPath; my $wantedPath = "C:\\stiller"; if (-e $wantedPath){ $newPath = $word->Options->SetProperty("DefaultFilePath" , wdDocumentsPath, $wantedPath); } else { print "Wanted path $wantedPath does not exist \n"; exit; } print "Default save path now: " . $word->Options->DefaultFilePath(wdDocumentsPath) . "\n"; $word->Options->SetProperty("DefaultFilePath", wdDocumentsPath, "$oldD +ocPath"); print "Reset Default save path to: " . $word->Options->DefaultFilePath(wdDocumentsPath) . "\n";

    outputs:

    Default save path was: h:\ Default save path now: c:\stiller\ Reset Default save path to: h:\

    cheers